-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (88 loc) · 3.09 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
"use strict";
var fs = require("fs");
var gm = require("gm");
var path = require("path");
var util = require("util");
var createTargetDirectories = require("./lib/create-target-directories");
var getFilesCount = require("./lib/get-files-count");
var getSafeFileName = require("./lib/get-safe-file-name");
var getUniqueFilePath = require("./lib/get-unused-file-path");
var normalizeFiles = require("./lib/normalize-files");
var resizeMehtods = require("./lib/resize-methods");
var validate = require("./lib/validate");
var validateContentType = require("./lib/validate-content-type");
var moveFile = function (file, newPath, options, cb) {
var oldPath = file.path;
if (options.method && resizeMehtods[options.method] && validateContentType(file)) {
var image = gm(oldPath);
return resizeMehtods[options.method](
image,
newPath,
options,
cb
);
}
fs.rename(oldPath, newPath, cb);
};
var processFile = function (file, type, cb) {
var options = this;
if (!options[type]) return cb();
if (file.size === 0) return cb();
file.processedPath = [];
file.processedName = [];
options[type].forEach(function (options) {
var suggestedFilePath = path.join(options.target,
getSafeFileName(file.name, file.type)
);
getUniqueFilePath(suggestedFilePath, function (newPath) {
moveFile(file, newPath, options, cb);
file.processedPath.push(newPath);
file.processedName.push(newPath.replace(/.*\//, ""));
});
});
};
var done = function (files, options, next) {
var processed = 0;
var filesCount = getFilesCount(files, options);
return function () {
processed += 1;
if (processed == filesCount) {
normalizeFiles(files);
next();
}
};
};
var resizeFiles = function (req, res, next) {
if (!req.files || !Object.keys(req.files).length) return next();
var options = this;
var files = req.files;
var types = Object.keys(req.files);
var processFileLocal = processFile.bind(options);
var cb = done(req.files, options, next);
types.map(function (type) {
if (util.isArray(files[type])) {
var filesOfType = files[type];
if (util.isArray((filesOfType[0]))) {
filesOfType = filesOfType[0];
}
filesOfType.forEach(function (file) {
processFileLocal(file, type, cb);
});
} else {
processFileLocal(files[type], type, cb);
}
});
};
var wrapOptions = function (options) {
Object.keys(options).forEach(function (key) {
if (!util.isArray(options[key])) options[key] = [options[key]];
});
};
module.exports = function (options) {
wrapOptions(options);
if (!validate(options)) {
throw new Error("UploadResizer options invalid: ");
}
createTargetDirectories(options);
return resizeFiles.bind(options);
};