【问题标题】:Optional file uploads with Sails.js使用 Sails.js 的可选文件上传
【发布时间】:2019-09-08 11:46:37
【问题描述】:

是否可以让 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- 的相同症状目录适配器。

以下代码在 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】:

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

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

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

    Ration 示例应用程序here 导致了此解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-03
      • 2015-02-20
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 2016-01-28
      • 2017-06-06
      • 1970-01-01
      相关资源
      最近更新 更多