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

[建议]让ctx.getFileStream 支持bytesRead #2860

Closed
ahuigo opened this issue Jul 30, 2018 · 6 comments
Closed

[建议]让ctx.getFileStream 支持bytesRead #2860

ahuigo opened this issue Jul 30, 2018 · 6 comments

Comments

@ahuigo
Copy link

ahuigo commented Jul 30, 2018

根据 getFileStream 示例
我们在使用egg 上传文件时,使用的是busboy 的FileStream(ReadableStream) 而不是fs 的ReadStream.
所以FileStream 缺少bytesRead.

bytesRead 访问示例:

       // app/controller/test.js
        const rs = await this.ctx.getFileStream()
        const ws = fs.createWriteStream(uploadPath, { flags: 'a' })
        const bytesRead = await new Promise((resolve, reject) => {
          const ps = rs.pipe(ws);
          ps.on('finish', () => {
             resolve(ws.bytesWritten);
             console.log(ws.bytesWritten); //有值
             console.log(rs.bytesRead); //undefined
          })
          ps.on('error', reject)
        })

虽然通过ws/ps 的bytesWritten 可以得到rs.bytesRead,
但是对象属性不完整会给用户带来困惑。(比如我误用FileStream.bytesRead 而调试了很久)

@egg-bot
Copy link

egg-bot commented Jul 30, 2018

Translation of this issue:


[Recommended] Let FileStream support bytesRead

According to [getFileStream example] (http://mirror.eggjs.org/zh-cn/basics/controller.html)
When we use egg to upload a file, we use busboy's FileStream(ReadableStream) instead of fs' ReadStream.
So FileStream is missing bytesRead.

Example of bytesRead access:

        Const rs = await this.ctx.getFileStream()
        Const ws = fs.createWriteStream(uploadPath, { flags: 'a' })
        Const bytesRead = await new Promise((resolve, reject) => {
          Const ps = rs.pipe(ws);
          Ps.on('finish', () => {
             Resolve(ws.bytesWritten);
             Console.log(ws.bytesWritten); //valued
             Console.log(rs.bytesRead); //undefined
          })
          Ps.on('error', reject)
        })

Although rs.bytesRead can be obtained through the bytesWritten of ws/ps, ** but the incomplete object properties will be confusing to the user**. (For example, I misused FileStream.bytesRead and debugged it for a long time)

Will you support bytesRead in the future?

@popomore
Copy link
Member

这个本来就不是文件 io,为何以 FileStream 为准,可以去 https://github.com/nodejs/node 问是否支持。

@ahuigo ahuigo changed the title [建议]让FileStream 支持bytesRead [建议]让ctx.getFileStream 支持bytesRead Jul 30, 2018
@ahuigo
Copy link
Author

ahuigo commented Jul 30, 2018

@popomore 这个不是node 的FileStream, 而是busboy 的FileStream。 我发到busboy 这里了 mscdex/busboy#173

@atian25
Copy link
Member

atian25 commented Jul 30, 2018

直接给那边提个 PR 吧

@fengmk2
Copy link
Member

fengmk2 commented Jul 30, 2018

其实不需要也行的,你可以监听 data 事件,就知道了。

@ahuigo
Copy link
Author

ahuigo commented Jul 30, 2018

其实这个属性可有可无吧。只是有则完备一些吧。

我上面的实例应该是目前最简单的方案了吧。
监听data 也需要要自己count bytesSize 吧

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

5 participants