request/lib/file.js

49 lines
837 B
JavaScript
Raw Permalink 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 {
#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-26 19:02:46 +08:00
constructor(props = {}) {
2023-10-25 18:45:16 +08:00
super()
2020-09-16 20:07:28 +08:00
2023-10-27 19:16:32 +08:00
for (let key in props) {
2023-10-25 18:45:16 +08:00
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-26 19:02:46 +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,
2023-10-27 19:16:32 +08:00
filename: this.name,
mime: this.type
2023-10-25 18:45:16 +08:00
}
}
2023-10-27 19:16:32 +08:00
write(buffer) {
2023-10-26 19:02:46 +08:00
this.#stream.write(buffer, _ => {
2023-10-25 18:45:16 +08:00
this.size += buffer.length
})
}
2023-10-26 19:02:46 +08:00
2023-10-27 19:16:32 +08:00
end(callback) {
this.lastModifiedDate = new Date()
this.#stream.end(callback)
2020-09-16 20:07:28 +08:00
}
}