import path from 'path' ; import { Request , Response } from 'express' ; import { Storage } from '@google-cloud/storage' ; import _validateGeneratedFile from './lib/validateGeneratedFile' ; let config = { } ; try { config = require ( path . join ( __dirname , '..' , 'config.json' ) ) ; } catch ( e ) { console . error ( 'Missing config.json; see README' ) ; } if ( config . SENTRY_DSN ) { const Raven = require ( 'raven' ) ; Raven . config ( config . SENTRY_DSN ) . install ( ) ; } const storage = new Storage ( { projectId : config . PROJECT } ) ; param { object } event param { function } The export function validateGeneratedFile ( req , res ) { res . set ( 'Access-Control-Allow-Origin' , '*' ) ; res . set ( 'Access-Control-Allow-Methods' , 'POST' ) ; const url = req . query . url ; if ( ! url ) { res . status ( 500 ) . send ( 'URL not specified' ) ; } _validateGeneratedFile ( url , ( report ) => { const bucket = storage . bucket ( config . STORAGE_BUCKET ) ; const objectName = `${ Date . now ( ) }_${ encodeURIComponent ( url ) }` ; const file = bucket . file ( objectName ) ; const stream = file . createWriteStream ( { gzip : true , metadata : { contentType : 'text/plain; charset=utf-8' } } ) ; stream . on ( 'error' , ( err ) => { res . status ( 500 ) . send ( err . message ) ; } ) ; stream . on ( 'finish' , ( ) => { res . status ( 200 ) . send ( encodeURIComponent ( objectName ) ) ; } ) ; stream . end ( JSON . stringify ( report ) ) ; } ) ; } ;