diff --git a/functions/modules/svg-generator.js b/functions/modules/svg-generator.js index d0c44ac..df4cdee 100644 --- a/functions/modules/svg-generator.js +++ b/functions/modules/svg-generator.js @@ -7,11 +7,29 @@ const { getStorage } = require( 'firebase-admin/storage' ) // SVG to JPEG const { convert } = require("convert-svg-to-jpeg") +// Existing file checker +const checkIfFilesExist = async ( svg, jpeg, path ) => { + + const [ [ svgExists ], [ jpegExists ] ] = await Promise.all( [ svg.exists(), jpeg.exists() ] ) + if( svgExists || jpegExists ) throw new Error( `${ svgExists ? 'SVG' : '' } ${ jpegExists ? ' and JPEG' : '' } already present at ${ path }. This should never happen!` ) + + +} + module.exports = async function svgFromAttributes( attributes=[], path='' ) { + // Validations if( !path.length ) throw new Error( 'svgFromAttributes missing path' ) if( !attributes.length ) throw new Error( 'svgFromAttributes missing attributes' ) + // Create file references and check whether they already exist + const storage = getStorage() + const bucket = storage.bucket() + const svgFile = bucket.file( `${path}.svg` ) + const rasterFile = bucket.file( `${path}.jpg` ) + await checkIfFilesExist( svgFile, rasterFile, path ) + + // Get properties const { value: primary_color } = attributes.find( ( { trait_type } ) => trait_type == "outfit color" ) const { value: accent_color } = attributes.find( ( { trait_type } ) => trait_type == "outfit accent color" ) const { value: backpack_color } = attributes.find( ( { trait_type } ) => trait_type == "backpack color" ) @@ -122,17 +140,8 @@ module.exports = async function svgFromAttributes( attributes=[], path='' ) { const bakedRaster = await convert( bakedSvg, { } ) - // Store file on firebase - const storage = getStorage() - const bucket = storage.bucket() - - // Make file reference - const svgFile = bucket.file( `${path}.svg` ) - const rasterFile = bucket.file( `${path}.jpg` ) - - // Delete testnet file if it already exists - // await svgFile.delete().catch( f => 'this is fine' ) - // await rasterFile.delete().catch( f => 'this is fine' ) + // Double check that files do not yet exist (in case of weird race condition) + await checkIfFilesExist( svgFile, rasterFile, path ) // Save files await svgFile.save( bakedSvg ) diff --git a/functions/modules/testnet.js b/functions/modules/testnet.js index 0931d99..07e1f5a 100644 --- a/functions/modules/testnet.js +++ b/functions/modules/testnet.js @@ -22,8 +22,7 @@ app.get( '/testnetapi/rocketeer/:id', async ( req, res ) => { } catch( e ) { // Log error for debugging - console.error( `Testnet api error for ${ id }: `, Object.keys( e ) ) - console.log( e ) + console.error( `Testnet api error for ${ id }: `, e ) // Return error to frontend return res.json( { error: e.mesage || e.toString() } )