Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
if (global.GENTLY) require = GENTLY.hijack(require);
2
3
var util = require('util'),
4
fs = require('fs'),
5
EventEmitter = require('events').EventEmitter,
6
crypto = require('crypto');
7
8
function File(properties) {
9
EventEmitter.call(this);
10
11
this.size = 0;
12
this.path = null;
13
this.name = null;
14
this.type = null;
15
this.hash = null;
16
this.lastModifiedDate = null;
17
18
this._writeStream = null;
19
20
for (var key in properties) {
21
this[key] = properties[key];
22
}
23
24
if(typeof this.hash === 'string') {
25
this.hash = crypto.createHash(properties.hash);
26
} else {
27
this.hash = null;
28
}
29
}
30
module.exports = File;
31
util.inherits(File, EventEmitter);
32
33
File.prototype.open = function() {
34
this._writeStream = new fs.WriteStream(this.path);
35
};
36
37
File.prototype.toJSON = function() {
38
var 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;
50
}
51
return json;
52
};
53
54
File.prototype.write = function(buffer, cb) {
55
var self = this;
56
if (self.hash) {
57
self.hash.update(buffer);
58
}
59
this._writeStream.write(buffer, function() {
60
self.lastModifiedDate = new Date();
61
self.size += buffer.length;
62
self.emit('progress', self.size);
63
cb();
64
});
65
};
66
67
File.prototype.end = function(cb) {
68
var self = this;
69
if (self.hash) {
70
self.hash = self.hash.digest('hex');
71
}
72
this._writeStream.end(function() {
73
self.emit('end');
74
cb();
75
});
76
};
77
78