request/lib/file.js

62 lines
946 B
JavaScript
Raw Normal View History

2023-10-25 18:45:16 +08:00
import { WriteStream } from 'node:fs'
import { EventEmitter } from 'node:events'
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
export default class File extends EventEmitter {
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
#stream = null
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
size = 0
path = null
name = null
type = null
lastModifiedDate = null
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
constructor(props = {}){
super()
2020-09-16 20:07:28 +08:00
2023-10-25 18:45:16 +08:00
for (var key in props) {
this[key] = props[key]
}
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
open() {
this.#stream = new WriteStream(this.path)
2020-09-16 20:07:28 +08:00
}
2023-10-25 18:45:16 +08:00
toJSON() {
return {
size: this.size,
path: this.path,
name: this.name,
type: this.type,
mtime: this.lastModifiedDate,
length: this.length,
filename: this.filename,
mime: this.mime
}
}
write(buffer, cb) {
this.#stream.write(buffer, _ =>{
this.lastModifiedDate = new Date()
this.size += buffer.length
this.emit('progress', this.size)
cb()
})
}
end(cb) {
this.#stream.end(() => {
this.emit('end')
cb()
})
2020-09-16 20:07:28 +08:00
}
}
2023-10-25 18:45:16 +08:00