Javascript 调整图像大小并上载到服务器会返回错误代码

Javascript 调整图像大小并上载到服务器会返回错误代码,javascript,node.js,firebase,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Functions,这是我的代码: 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, dimSe

这是我的代码:

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()
    })
  })
})
})
var-dimensionsArray=[]
尺寸推拉(新卡片组(“L”、1000、500、200、100))
尺寸滑动推压(新卡片组(“M”、500、200、100、50))
尺寸推拉(新卡片组(“S”、200、100、50、20))
功能卡片集(名称、dimSetWidth、dimSetHeight、dimPreviewWidth、DIMPreviewWight){
this.name=name
this.dimSetWidth=dimSetWidth
this.dimSetHeight=dimSetHeight
this.dimPreviewWidth=dimPreviewWidth
this.dimPreviewHeight=dimPreviewHeight
}
函数insertBeforeLastOccurrence(strToSearch、strToFind、strToInsert){
var n=strToSearch.lastIndexOf(strToFind);
如果(n<0)返回strToSearch;
返回strToSearch.substring(0,n)+strToInsert+strToSearch.substring(n);
}
exports.resizeCardSet=functions.storage.object().onChange(事件=>{
让file=event.data
var filePath=event.data.name
如果(file.resourceState==='不存在'){
返回;
}
如果(!filePath.startsWith('admin/')){
返回;
}
const fileBucket=event.data.bucket
const bucket=gcs.bucket(fileBucket);
const fileName=filePath.split('/').pop()
常量tempFilePath=[]
常量维度=[]
const promisesDownloads=[]
var q=0
而(q<尺寸射线长度){
让downloadTo=insertBeforeLastOccurrence(`/tmp/${fileName}`,“.png”,dimensionsArray[q].name)
尺寸。推送(下载到)
console.log(下载到)
promisesDownloads.push(bucket.file(filePath).下载({
目的地:尺寸[q]
}))
q+=1
}
控制台日志(“1”)
返回承诺。所有([promisesDownloads])。然后(()=>{
控制台日志(“2”)
var承诺=[]
var downloadDestinations=[]
if(filePath.startsWith('admin/generatenewcardset/')){
filePath=filePath.substring(25)
var setNumber=filePath.match(/\d/g);
setNumber=setNumber.join(“”);
让nameFile=filePath.replace(/[0-9]/g');
filePath='deckcards/'+setNumber+'/'+nameFile
if(filePath.indexOf('Set')>-1)
{
var q=0
而(q<尺寸射线长度){
let downloadTo='deckcards/'+setNumber+'/'+nameFile+dimensionsArray[q]。name
downloadDestinations.push(下载到)
push(生成('convert',[dimensions[q],'-resize',dimensionsArray[q]。dimSetHeight+'x'+dimensionsArray[q]。dimSetWidth+'>',dimensions[q]]))
q+=1
}
}否则{
返回
}
}
否则{
返回
}
控制台日志(“3”)
返回承诺。所有([承诺])然后(()=>{
控制台日志(“4”)
var promisesB=[]
变量i=0
console.log(承诺[0])
用于(以尺寸表示的文件){
console.log('在创建缩略图',维度[i])
承诺推送(桶上载)(尺寸[i]{
目的地:下载目的地[i]
}))      
i+=1
}
全部([promisesB])然后(()=>{
let file=bucket.file(event.data.name)
返回文件。删除()
})
})
})
})
我想在admin/generatenewcardset中上传1个图像,该图像应该存储在另一个地图中,在3个不同的图像中

只有我可以上传该地图中的图像,该图像始终以数字开头,在本例中后跟Set(因此:1Set,2Set…)。我想存储在deckcards/{numberofset}/Set{dimension}中,所以它可以是:deckcards/2/SetM

现在,当我尝试上载图像时,以下是输出:

这是全部错误:

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)
ChildProcessError:`convert/tmp/1SetL.png-resize 500x1000>/tmp/1SetL.png`失败,代码为1
在这个过程中。(/user\u code/node\u modules/child process promise/lib/index.js:132:23)
两点钟(events.js:106:13)
在ChildProcess.emit(events.js:191:7)
在maybeClose(内部/子进程js:877:16)
在插座上。(内部/子进程js:334:11)
在emitOne(events.js:96:13)
在Socket.emit(events.js:188:7)
at Pipe.\u handle.close[as\u onclose](net.js:498:12)

谢谢

你在这里展示了很多代码,很多事情可能会出错。您确定有任何文件正在正确下载吗?您是否验证了内容是否完全符合您的预期?尝试将代码精简到最低限度:下载单个文件,验证其内容是否正确,以及单个转换是否有效@是的,有点长。。。我尝试了另一种方法,这里的代码少得多:只有这里,图像是0字节。。。您可以很容易地复制该代码。你能看一下吗?谢谢