-
Notifications
You must be signed in to change notification settings - Fork 6
/
File.js
66 lines (56 loc) · 1.71 KB
/
File.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
///////////////////////////////////////////////////////////////////////////////////////////////
// File for Circuit Node.js
// Based on npm File module
///////////////////////////////////////////////////////////////////////////////////////////////
(function () {
'use strict';
var fs = require('fs'),
path = require('path'),
mime = require('mime');
// string
// stream
// buffer
function File(input) {
var self = this;
function updateStat(stat) {
self.stat = stat;
self.lastModifiedDate = self.stat.mtime;
self.size = self.stat.size;
}
if (typeof input === 'string') {
self.path = input;
} else {
Object.keys(input).forEach(function (k) {
self[k] = input[k];
});
}
self.name = self.name || path.basename(self.path || '');
if (!self.name) {
throw new Error('No name');
}
if (mime.getType) {
self.type = self.type || mime.getType(self.name);
} else if (mime.lookup) {
self.type = self.type || mime.lookup(self.name);
}
if (!self.path) {
if (self.buffer) {
self.size = self.buffer.length;
} else if (!self.stream) {
throw new Error('No input, nor stream, nor buffer.');
}
return;
}
if (!self.jsdom) {
return;
}
if (!self.async) {
updateStat(fs.statSync(self.path));
} else {
fs.stat(self.path, function (err, stat) {
updateStat(stat);
});
}
}
module.exports = File;
}());