-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFile.js
62 lines (53 loc) · 1.22 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
//
// File
//
// http://www.w3.org/TR/FileAPI/#dfn-file
// https://developer.mozilla.org/en/DOM/File
(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 ('string' === typeof input) {
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");
}
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;
}());