Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream模式下多文件上传 如何先通过validate验证下参数 再决定上传否 #5311

Open
C16103112 opened this issue Apr 29, 2024 · 7 comments

Comments

@C16103112
Copy link

let parts = this.ctx.multipart({ autoFields: true });
let files = [];
let stream;
// field 能提前获取到值吗
const field = parts.field

while ((stream = await parts()) != null) {
// xxx
}

@C16103112
Copy link
Author

想先对其他上传的字段做个验证 再决定接受是否上传的文件 stream模式下能做到吗?

@atian25
Copy link
Member

atian25 commented Apr 30, 2024

await parts 就是一个个的拿的,但你验证后会做些啥?如果是为了中断请求节省资源,好像也提升不了多少。
直接在前端校验 field 是不是更好一点?

@C16103112
Copy link
Author

await parts 就是一个个的拿的,但你验证后会做些啥?如果是为了中断请求节省资源,好像也提升不了多少。
直接在前端校验 field 是不是更好一点?

上传到s3的名字会拿field。比如version,type。接口不仅给前端使用,其他服务端可能集成在ci里。

@atian25
Copy link
Member

atian25 commented Apr 30, 2024

我的意思是,直接 file mode 存到临时文件后,一起处理也可以吧,你这里的原始需求是啥

@C16103112
Copy link
Author

我的意思是,直接 file mode 存到临时文件后,一起处理也可以吧,你这里的原始需求是啥

Streams能做到file mode一样一起处理 的吗

@atian25
Copy link
Member

atian25 commented Apr 30, 2024

当然可以,file mode 底层就是 stream mode,你看下源码就知道了。
只不过 stream 很麻烦,一不小心就容易泄漏导致 hangup,如果没有特殊的极致性能诉求,建议直接 file mode 省事

@svenjia
Copy link

svenjia commented May 7, 2024

看了busboy的代码,好像能这样校验。

let parts = this.ctx.multipart({ autoFields: true,
    checkField: (name, value) => {
      if (name === '_csrf' && !checkCSRF(ctx, value)) {
        var err =  new Error('invalid csrf token')
        err.status = 400
        return err
      }
    }
 });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants