moderate-images/functions/index.js
/**
 * Blurs the given image located in the given bucket using ImageMagick.
 */
Blurs the given image located in the given bucket using ImageMagick.
Blurs the given image located in the given bucket using ImageMagick .
function blurImage(filePath, bucketName, metadata) {
  const tempLocalFile = path.join(os.tmpdir(), filePath);
  const tempLocalDir = path.dirname(tempLocalFile);
  const bucket = gcs.bucket(bucketName);
  return mkdirp(tempLocalDir).then(() => {
    console.log('Temporary directory has been created', tempLocalDir);
    return bucket.file(filePath).download({destination: tempLocalFile});
  }).then(() => {
    console.log('The file has been downloaded to', tempLocalFile);
    return spawn('convert', [tempLocalFile, '-channel', 'RGBA', '-blur', '0x8', tempLocalFile]);
  }).then(() => {
    console.log('Blurred image created at', tempLocalFile);
    return bucket.upload(tempLocalFile, {
      destination: filePath,
      metadata: {metadata: metadata}, 
    });
  }).then(() => {
    console.log('Blurred image uploaded to Storage at', filePath);
    fs.unlinkSync(tempLocalFile);
    return console.log('Deleted local file', filePath);
  });
}
function blurImage ( filePath , bucketName , metadata ) { const tempLocalFile = path . join ( os . tmpdir ( ) , filePath ) ; const tempLocalDir = path . dirname ( tempLocalFile ) ; const bucket = gcs . bucket ( bucketName ) ; return mkdirp ( tempLocalDir ) . then ( ( ) => { console . log ( 'Temporary directory has been created' , tempLocalDir ) ; return bucket . file ( filePath ) . download ( { destination : tempLocalFile } ) ; } ) . then ( ( ) => { console . log ( 'The file has been downloaded to' , tempLocalFile ) ; return spawn ( 'convert' , [ tempLocalFile , '-channel' , 'RGBA' , '-blur' , '0x8' , tempLocalFile ] ) ; } ) . then ( ( ) => { console . log ( 'Blurred image created at' , tempLocalFile ) ; return bucket . upload ( tempLocalFile , { destination : filePath , metadata : { metadata : metadata } , } ) ; } ) . then ( ( ) => { console . log ( 'Blurred image uploaded to Storage at' , filePath ) ; fs . unlinkSync ( tempLocalFile ) ; return console . log ( 'Deleted local file' , filePath ) ; } ) ; }
Blurs the given image located in the given bucket using ImageMagick.
Blurs the given image located in the given bucket using ImageMagick .
// Create the temp directory where the storage file will be downloaded. // Download file from bucket. // Blur the image using ImageMagick. // Uploading the Blurred image. // Keeping custom metadata.