Monday 12 June 2017

Resize an image and upload to server returns in an error code

This is my code:

var dimensionsArray = []
dimensionsArray.push(new cardSet("L", 1000, 500, 200, 100))
dimensionsArray.push(new cardSet("M", 500, 200, 100, 50))
dimensionsArray.push(new cardSet("S", 200, 100, 50, 20))

function cardSet(name, dimSetWidth, dimSetHeight, dimPreviewWidth, dimPreviewHeight){
    this.name = name
    this.dimSetWidth = dimSetWidth 
    this.dimSetHeight = dimSetHeight
    this.dimPreviewWidth = dimPreviewWidth
    this.dimPreviewHeight = dimPreviewHeight
}
function insertBeforeLastOccurrence(strToSearch, strToFind, strToInsert) {
    var n = strToSearch.lastIndexOf(strToFind);
    if (n < 0) return strToSearch;
    return strToSearch.substring(0,n) + strToInsert + strToSearch.substring(n);    
}
exports.resizeCardSet = functions.storage.object().onChange(event => {
    let file = event.data
    var filePath = event.data.name
    if (file.resourceState === 'not_exists') {
    return;
  }
      if(!filePath.startsWith('admin/')) {
    return;
  }
const fileBucket = event.data.bucket
const bucket = gcs.bucket(fileBucket);
const fileName = filePath.split('/').pop()
const tempFilePath = []
const dimensions = []
const promisesDownloads = []
var q = 0

while (q < dimensionsArray.length){
    let downloadTo = insertBeforeLastOccurrence(`/tmp/${fileName}`, ".png", dimensionsArray[q].name)
    dimensions.push(downloadTo)
    console.log(downloadTo)
    promisesDownloads.push(bucket.file(filePath).download({
  destination: dimensions[q]
}))
    q += 1
}
console.log("1")
return Promise.all([promisesDownloads]).then(() => {
    console.log("2")
   var promises = []
   var downloadDestinations = []
      if (filePath.startsWith('admin/generatenewcardset/')) {
        filePath = filePath.substring(25)
        var setNumber = filePath.match(/\d/g);
        setNumber = setNumber.join("");
        let nameFile = filePath.replace(/[0-9]/g, '');
        filePath = 'deckcards/' + setNumber + '/' + nameFile
        if (filePath.indexOf('Set') > -1)
            {
              var q = 0
                while (q < dimensionsArray.length){
                    let downloadTo = 'deckcards/' + setNumber + '/' + nameFile + dimensionsArray[q].name
                    downloadDestinations.push(downloadTo)
                    promises.push(spawn('convert', [dimensions[q], '-resize',  dimensionsArray[q].dimSetHeight + 'x' + dimensionsArray[q].dimSetWidth + '>', dimensions[q]]))
                    q += 1
                }
            }else{
                return
            }
    }
    else{
        return
    }
    console.log("3")

return Promise.all([promises]).then(() => {
    console.log("4")
      var promisesB = []
      var i = 0
      console.log(promises[0])
      for (file in dimensions){
    console.log('Thumbnails created at', dimensions[i])
    promisesB.push(bucket.upload(dimensions[i], {
      destination: downloadDestinations[i]
    }))      
    i += 1
      }
    return Promise.all([promisesB]).then(() => {
        let file = bucket.file(event.data.name)
        return file.delete()
    })
  })
})
})

I want to upload 1 image in admin/generatenewcardset and that image should be stored inside an map.

Only I can upload an image in that map, the image always begins with an number, followed by Set in this example (so: 1Set, 2Set...). I want to store in in deckcards/{numberofset}/Set{dimension}, so it could be: deckcards/2/SetM

Now when I try to upload an image, this is the output:

enter image description here

This is the full error:

ChildProcessError: `convert /tmp/1SetL.png -resize 500x1000> /tmp/1SetL.png` failed with code 1
    at ChildProcess.<anonymous> (/user_code/node_modules/child-process-promise/lib/index.js:132:23)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Socket.<anonymous> (internal/child_process.js:334:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:498:12)

Thanks!



via J. Doe

No comments:

Post a Comment