49 lines
837 B
JavaScript
49 lines
837 B
JavaScript
import { WriteStream } from 'node:fs'
|
|
import { EventEmitter } from 'node:events'
|
|
|
|
export default class File extends EventEmitter {
|
|
#stream = null
|
|
|
|
size = 0
|
|
path = null
|
|
name = null
|
|
type = null
|
|
lastModifiedDate = null
|
|
|
|
constructor(props = {}) {
|
|
super()
|
|
|
|
for (let key in props) {
|
|
this[key] = props[key]
|
|
}
|
|
}
|
|
|
|
open() {
|
|
this.#stream = new WriteStream(this.path)
|
|
}
|
|
|
|
toJSON() {
|
|
return {
|
|
size: this.size,
|
|
path: this.path,
|
|
name: this.name,
|
|
type: this.type,
|
|
mtime: this.lastModifiedDate,
|
|
length: this.length,
|
|
filename: this.name,
|
|
mime: this.type
|
|
}
|
|
}
|
|
|
|
write(buffer) {
|
|
this.#stream.write(buffer, _ => {
|
|
this.size += buffer.length
|
|
})
|
|
}
|
|
|
|
end(callback) {
|
|
this.lastModifiedDate = new Date()
|
|
this.#stream.end(callback)
|
|
}
|
|
}
|