1

是否可以让 Sails操作在请求后几秒钟内接受可选文件上传而不吐出以下堆栈跟踪?

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它是一个空数组,其余的操作代码运行没有问题,但我不希望日志中充满这种垃圾。

4

1 回答 1

0

事实证明这真的很简单,只需从 npm 中拉入sails-hook-uploads模块并将原始问题中的Promise-wrapped cruft 替换为类似这样的内容

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

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

由 Ration 示例应用程序引导到此解决方案,此处

于 2019-04-18T07:35:11.367 回答