forked from tanhauhau/express-easy-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (147 loc) · 4.49 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var fs = require('fs');
var exists = require('file-exists-promise');
var path = require('path');
var packer = require('zip-stream');
var clean = require('var-clean').clean;
var readDir = require('fs-readdir-recursive');
var Promise = require('es6-promise').Promise;
function ZIPError() {}
ZIPError.prototype = Object.create(Error.prototype);
function Ignored(obj) {
this.file = obj;
}
Ignored.prototype.constructor = Ignored;
function ZIPResult(err, ignored) {
this.err = err;
this.ignored = ignored;
}
ZIPResult.prototype.constructor = ZIPResult;
function zipEntry(zip, dat, opt) {
return new Promise(function(resolve, reject) {
zip.entry(dat, opt, function(err, data) {
if (err) reject(err);
else resolve(data);
});
});
}
function _ZIP(opt) {
var _this = this;
return new Promise(function(resolve, reject) {
opt = opt || {};
opt.filename = clean.cleanOnlyString(opt.filename) || 'attachment.zip';
opt.files = opt.files || [];
_this.header('Content-Type', 'application/zip');
_this.header('Content-Disposition', 'attachment; filename="' + opt.filename + '"');
var zip = new packer();
zip.pipe(_this); // res is a writable stream
function addFilePath(filepath, opt) {
return new Promise(function(resolve, reject) {
//check if file exists or not
exists(filepath)
.then(function(stat) {
try {
if (stat.isFile()) {
//if it is a file
zipEntry(zip, fs.createReadStream(filepath), opt)
.then(resolve, reject);
} else {
//if it is a directory
var files = readDir(filepath);
each(files, function iteratee(file) {
var childOpt = {
name: path.join(opt.name, file),
comment: opt.comment,
date: opt.date,
mode: opt.mode,
type: opt.type
};
return addFilePath(path.join(filepath, file), childOpt);
}).then(resolve, reject);
}
} catch (e) {
reject(e);
}
}, function() { //if not exists
return reject(new ZIPError());
});
});
}
function addFileContent(data, opt) {
return new Promise(function(resolve, reject) {
data = clean.cleanOnlyString(data);
if (data !== undefined) {
zipEntry(zip, data, opt)
.then(resolve, reject);
} else {
reject(new ZIPError());
}
});
}
function addFile(file) {
return new Promise(function(resolve, reject) {
var fileOpt = {
name: clean.cleanOnlyString(file.name) || 'noname',
comment: clean.cleanOnlyString(file.comment) || '',
date: file.date,
mode: file.mode,
type: file.type
};
var promise = (file.path !== undefined) ?
addFilePath(file.path, fileOpt) :
addFileContent(file.content, fileOpt);
promise.then(resolve, function(e) {
if (e instanceof ZIPError) {
e = new Ignored(file);
}
reject(e);
});
});
}
each(opt.files, addFile)
.then(function(data) {
zip.finalize();
if (data.err instanceof Array && data.err.length > 0) {
reject(data.err);
} else {
resolve({ size: zip.getBytesWritten(), ignored: data.ignored });
}
}, function(err) {
_this.sendStatus(500);
reject(err);
});
});
}
function ZIP(req, res, next) {
res.zip = _ZIP;
next();
}
function each(arr, iter) {
var result = [], e = [];
function goodPromise(promise) {
return new Promise(function(resolve) {
promise.then(function(data) {
if (data instanceof ZIPResult) {
//add to the array
Array.prototype.push.apply(e, data.err);
Array.prototype.push.apply(result, data.ignored);
}
resolve();
}, function(err) {
if (err instanceof Ignored) {
result.push(err.file);
} else {
e.push(err);
}
resolve();
});
});
}
var ready = Promise.resolve(null);
arr.forEach(function(file) {
ready = ready.then(function() {
return goodPromise(iter(file));
});
});
return ready.then(function() { return Promise.resolve(new ZIPResult(e, result)); });
}
exports = module.exports = function() { return ZIP };