使用Sails.js上传可选文件

问题描述 投票:0回答:1

是否有可能让Sails action接受可选的文件上传,而不会在请求后几秒钟吐出下面的堆栈跟踪?

Upstream (file upload: `image`) emitted an error: { Error: EMAXBUFFER: An upstream (`NOOP_image`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.

Note that this error might be occurring due to an earlier file upload that is finally timing out after an unrelated server error.
    at Timeout.<anonymous> (/home/jarrod/workspace/cuckold/Cuckold-API/node_modules/skipper/lib/private/Upstream/Upstream.js:86:15)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
  code: 'EMAXBUFFER',
  message: 'EMAXBUFFER: An upstream (`NOOP_image`) timed out before it was plugged into a receiver. It was still unused after waiting 4500ms. You can configure this timeout by changing the `maxTimeToBuffer` option.\n\nNote that this error might be occurring due to an earlier file upload that is finally timing out after an unrelated server error.' }

为了完整性我使用自定义Skipper适配器来转储minio中的文件,这是我基于skipper-s3适配器,但我也看到使用默认store-files-in-.tmp-directory适配器的相同症状。

以下代码正常运行,并在HTTP请求中提供时接受并存储单个文件上载,但如果省略image字段,则在上述请求后4.5秒内将上述内容打印到控制台。

有关行动中最有趣的部分如下:

module.exports = {
  friendlyName: 'Create or update a news article',

  files: ['image'],

  inputs: {
    id: {
      type: 'number',
      description: 'ID of article to edit (omit for new articles)'
    },

    content: {
      type: 'string',
      description: 'Markdown-formatted content of news artcle',
      required: true
    },

    image: {
      type: 'ref'
    },
  },

  exits: {
    success: { }
  },

  fn: async function (inputs, exits) {
    let id = inputs.id;
    let article;

    console.log('About to grab upload(s)')
    let uploadedFiles = await (new Promise((resolve, reject) => {
      this.req.file('image').upload({/* ... */}, (err, uploadedFiles) => {
        console.log('Upload stuff callback', [err, uploadedFiles])
        if (err) {
          sails.log.error(`Unable to store uploads for news article`, err.stack)
          return reject(new Error('Unable to store uploads')); // Return a vague message to the client
        }
        resolve(uploadedFiles)
      });
    }));

    console.log('uploadedFiles', uploadedFiles)
    if (uploadedFiles && uploadedFiles.length) {
      uploadedFiles = uploadedFiles.map(f => f.fd)
    } else {
      uploadedFiles = null
    }

    // Do some database updating

    return exits.success({ article });
  }
};

如果HTTP请求中没有上传,那么uploadedFiles是一个空数组,其余的操作代码运行没有问题,但我不希望日志充满这种瑕疵。

sails.js sails-skipper
1个回答
0
投票

事实证明它很简单,只需从npm插入sails-hook-uploads模块并用原始问题替换Promise-wrapped cruft

let uploadedFiles = await sails.upload(inputs.image, {/* custom adapter config */});

似乎按预期工作,但如果请求中未提供上传,则不会记录上传未及时截获的警告。

由Ration示例应用程序here引导此解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.