|
1 | 1 | /* eslint-disable no-underscore-dangle */
|
2 |
| -const util = require('util'); |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
3 | 5 | const fs = require('fs');
|
4 |
| -const { EventEmitter } = require('events'); |
5 | 6 | const crypto = require('crypto');
|
| 7 | +const { EventEmitter } = require('events'); |
6 | 8 |
|
7 |
| -function File(properties) { |
8 |
| - EventEmitter.call(this); |
| 9 | +class File extends EventEmitter { |
| 10 | + constructor(properties) { |
| 11 | + super(); |
9 | 12 |
|
10 |
| - this.size = 0; |
11 |
| - this.path = null; |
12 |
| - this.name = null; |
13 |
| - this.type = null; |
14 |
| - this.hash = null; |
15 |
| - this.lastModifiedDate = null; |
| 13 | + this.size = 0; |
| 14 | + this.path = null; |
| 15 | + this.name = null; |
| 16 | + this.type = null; |
| 17 | + this.hash = null; |
| 18 | + this.lastModifiedDate = null; |
| 19 | + |
| 20 | + this._writeStream = null; |
16 | 21 |
|
17 |
| - this._writeStream = null; |
| 22 | + // eslint-disable-next-line guard-for-in, no-restricted-syntax |
| 23 | + for (const key in properties) { |
| 24 | + this[key] = properties[key]; |
| 25 | + } |
18 | 26 |
|
19 |
| - // eslint-disable-next-line guard-for-in, no-restricted-syntax |
20 |
| - for (const key in properties) { |
21 |
| - this[key] = properties[key]; |
| 27 | + if (typeof this.hash === 'string') { |
| 28 | + this.hash = crypto.createHash(properties.hash); |
| 29 | + } else { |
| 30 | + this.hash = null; |
| 31 | + } |
22 | 32 | }
|
23 | 33 |
|
24 |
| - if (typeof this.hash === 'string') { |
25 |
| - this.hash = crypto.createHash(properties.hash); |
26 |
| - } else { |
27 |
| - this.hash = null; |
| 34 | + open() { |
| 35 | + this._writeStream = new fs.WriteStream(this.path); |
28 | 36 | }
|
29 |
| -} |
30 |
| -module.exports = File; |
31 |
| -util.inherits(File, EventEmitter); |
32 | 37 |
|
33 |
| -File.prototype.open = function onpenStream() { |
34 |
| - this._writeStream = new fs.WriteStream(this.path); |
35 |
| -}; |
| 38 | + toJSON() { |
| 39 | + const json = { |
| 40 | + size: this.size, |
| 41 | + path: this.path, |
| 42 | + name: this.name, |
| 43 | + type: this.type, |
| 44 | + mtime: this.lastModifiedDate, |
| 45 | + length: this.length, |
| 46 | + filename: this.filename, |
| 47 | + mime: this.mime, |
| 48 | + }; |
| 49 | + if (this.hash && this.hash !== '') { |
| 50 | + json.hash = this.hash; |
| 51 | + } |
| 52 | + return json; |
| 53 | + } |
36 | 54 |
|
37 |
| -File.prototype.toJSON = function toJSON() { |
38 |
| - const json = { |
39 |
| - size: this.size, |
40 |
| - path: this.path, |
41 |
| - name: this.name, |
42 |
| - type: this.type, |
43 |
| - mtime: this.lastModifiedDate, |
44 |
| - length: this.length, |
45 |
| - filename: this.filename, |
46 |
| - mime: this.mime, |
47 |
| - }; |
48 |
| - if (this.hash && this.hash !== '') { |
49 |
| - json.hash = this.hash; |
| 55 | + toString() { |
| 56 | + return `File: ${this.name}, Path: ${this.path}`; |
50 | 57 | }
|
51 |
| - return json; |
52 |
| -}; |
53 | 58 |
|
54 |
| -File.prototype.toString = function toString() { |
55 |
| - return `File: ${this.name}, Path: ${this.path}`; |
56 |
| -}; |
| 59 | + writeFn(buffer, cb) { |
| 60 | + if (this.hash) { |
| 61 | + this.hash.update(buffer); |
| 62 | + } |
57 | 63 |
|
58 |
| -File.prototype.write = function writeFn(buffer, cb) { |
59 |
| - if (this.hash) { |
60 |
| - this.hash.update(buffer); |
61 |
| - } |
| 64 | + if (this._writeStream.closed) { |
| 65 | + cb(); |
| 66 | + return; |
| 67 | + } |
62 | 68 |
|
63 |
| - if (this._writeStream.closed) { |
64 |
| - cb(); |
65 |
| - return; |
| 69 | + this._writeStream.write(buffer, () => { |
| 70 | + this.lastModifiedDate = new Date(); |
| 71 | + this.size += buffer.length; |
| 72 | + this.emit('progress', this.size); |
| 73 | + cb(); |
| 74 | + }); |
66 | 75 | }
|
67 | 76 |
|
68 |
| - this._writeStream.write(buffer, () => { |
69 |
| - this.lastModifiedDate = new Date(); |
70 |
| - this.size += buffer.length; |
71 |
| - this.emit('progress', this.size); |
72 |
| - cb(); |
73 |
| - }); |
74 |
| -}; |
75 |
| - |
76 |
| -File.prototype.end = function endFn(cb) { |
77 |
| - if (this.hash) { |
78 |
| - this.hash = this.hash.digest('hex'); |
| 77 | + endFn(cb) { |
| 78 | + if (this.hash) { |
| 79 | + this.hash = this.hash.digest('hex'); |
| 80 | + } |
| 81 | + this._writeStream.end(() => { |
| 82 | + this.emit('end'); |
| 83 | + cb(); |
| 84 | + }); |
79 | 85 | }
|
80 |
| - this._writeStream.end(() => { |
81 |
| - this.emit('end'); |
82 |
| - cb(); |
83 |
| - }); |
84 |
| -}; |
| 86 | +} |
| 87 | + |
| 88 | +exports.File = File; |
0 commit comments