|
1 | 1 | var EventEmitter = require('events').EventEmitter, |
2 | | - path = require('path'), |
3 | | - fs = require('fs'), |
4 | | - formidable = require('formidable'), |
5 | | - imageMagick = require('imagemagick'), |
6 | | - mkdirp = require('mkdirp'), |
7 | | - _ = require('lodash'); |
| 2 | + path = require('path'), |
| 3 | + fs = require('fs'), |
| 4 | + formidable = require('formidable'), |
| 5 | + imageMagick = require('imagemagick'), |
| 6 | + mkdirp = require('mkdirp'), |
| 7 | + _ = require('lodash'); |
8 | 8 |
|
9 | 9 | module.exports = function (options) { |
10 | 10 |
|
11 | | - var FileInfo = require('./fileinfo')( |
12 | | - _.extend({ |
13 | | - baseDir: options.uploadDir |
14 | | - }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) |
15 | | - ); |
16 | | - |
17 | | - var UploadHandler = function (req, res, callback) { |
18 | | - EventEmitter.call(this); |
19 | | - this.req = req; |
20 | | - this.res = res; |
21 | | - this.callback = callback; |
22 | | - }; |
23 | | - require('util').inherits(UploadHandler, EventEmitter); |
24 | | - |
25 | | - UploadHandler.prototype.noCache = function () { |
26 | | - this.res.set({ |
27 | | - 'Pragma': 'no-cache', |
28 | | - 'Cache-Control': 'no-store, no-cache, must-revalidate', |
29 | | - 'Content-Disposition': 'inline; filename="files.json"' |
30 | | - }); |
31 | | - }; |
32 | | - |
33 | | - UploadHandler.prototype.get = function () { |
34 | | - this.noCache(); |
35 | | - var files = []; |
36 | | - fs.readdir(options.uploadDir(), _.bind(function (err, list) { |
37 | | - _.each(list, function (name) { |
38 | | - var stats = fs.statSync(options.uploadDir() + '/' + name), |
39 | | - fileInfo; |
40 | | - if (stats.isFile()) { |
41 | | - fileInfo = new FileInfo({ |
42 | | - name: name, |
43 | | - size: stats.size |
44 | | - }); |
45 | | - this.initUrls(fileInfo); |
46 | | - files.push(fileInfo); |
47 | | - } |
48 | | - }, this); |
49 | | - this.callback({files: files}); |
50 | | - }, this)); |
51 | | - }; |
52 | | - |
53 | | - UploadHandler.prototype.post = function () { |
54 | | - var self = this, |
55 | | - form = new formidable.IncomingForm(), |
56 | | - tmpFiles = [], |
57 | | - files = [], |
58 | | - map = {}, |
59 | | - counter = 1, |
60 | | - redirect, |
61 | | - finish = _.bind(function () { |
62 | | - if (!--counter) { |
63 | | - _.each(files, function (fileInfo) { |
64 | | - this.initUrls(fileInfo); |
65 | | - this.emit('end', fileInfo); |
66 | | - }, this); |
67 | | - this.callback({files: files}, redirect); |
68 | | - } |
69 | | - }, this); |
70 | | - |
71 | | - this.noCache(); |
72 | | - |
73 | | - form.uploadDir = options.tmpDir; |
74 | | - form |
75 | | - .on('fileBegin', function (name, file) { |
76 | | - tmpFiles.push(file.path); |
77 | | - var fileInfo = new FileInfo(file); |
78 | | - fileInfo.safeName(); |
79 | | - map[path.basename(file.path)] = fileInfo; |
80 | | - files.push(fileInfo); |
81 | | - self.emit('begin', fileInfo); |
82 | | - }) |
83 | | - .on('field', function (name, value) { |
84 | | - if (name === 'redirect') { |
85 | | - redirect = value; |
86 | | - } |
87 | | - }) |
88 | | - .on('file', function (name, file) { |
89 | | - var fileInfo = map[path.basename(file.path)]; |
90 | | - if (fs.existsSync(file.path)) { |
91 | | - fileInfo.size = file.size; |
92 | | - if (!fileInfo.validate()) { |
93 | | - fs.unlink(file.path); |
94 | | - return; |
95 | | - } |
96 | | - |
97 | | - var generatePreviews = function () { |
98 | | - if (options.imageTypes.test(fileInfo.name)) { |
99 | | - _.each(options.imageVersions, function (value, version) { |
100 | | - // creating directory recursive |
101 | | - if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) |
102 | | - mkdirp.sync(options.uploadDir() + '/' + version + '/'); |
103 | | - |
104 | | - counter++; |
105 | | - var opts = options.imageVersions[version]; |
106 | | - imageMagick.resize({ |
107 | | - width: opts.width, |
108 | | - height: opts.height, |
109 | | - srcPath: options.uploadDir() + '/' + fileInfo.name, |
110 | | - dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, |
111 | | - customArgs: opts.imageArgs || ['-auto-orient'] |
112 | | - }, finish); |
113 | | - }); |
114 | | - } |
115 | | - } |
116 | | - |
117 | | - if (!fs.existsSync(options.uploadDir() + '/')) |
118 | | - mkdirp.sync(options.uploadDir() + '/'); |
119 | | - |
120 | | - counter++; |
121 | | - fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { |
122 | | - if (!err) { |
123 | | - generatePreviews(); |
124 | | - finish(); |
125 | | - } else { |
126 | | - var is = fs.createReadStream(file.path); |
127 | | - var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); |
128 | | - is.on('end', function (err) { |
129 | | - if (!err) { |
130 | | - fs.unlinkSync(file.path); |
131 | | - generatePreviews(); |
132 | | - } |
133 | | - finish(); |
134 | | - }); |
135 | | - is.pipe(os); |
136 | | - } |
137 | | - }); |
138 | | - } |
139 | | - }) |
140 | | - .on('aborted', function () { |
141 | | - _.each(tmpFiles, function (file) { |
142 | | - var fileInfo = map[path.basename(file)]; |
143 | | - self.emit('abort', fileInfo); |
144 | | - fs.unlink(file); |
145 | | - }); |
146 | | - }) |
147 | | - .on('error', function (e) { |
148 | | - self.emit('error', e); |
149 | | - }) |
150 | | - .on('progress', function (bytesReceived, bytesExpected) { |
151 | | - if (bytesReceived > options.maxPostSize) |
152 | | - self.req.connection.destroy(); |
153 | | - }) |
154 | | - .on('end', finish) |
155 | | - .parse(self.req); |
156 | | - }; |
157 | | - |
158 | | - UploadHandler.prototype.destroy = function () { |
159 | | - var self = this, |
160 | | - fileName = path.basename(decodeURIComponent(this.req.url)); |
161 | | - |
162 | | - fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { |
163 | | - _.each(options.imageVersions, function (value, version) { |
164 | | - fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); |
165 | | - }); |
166 | | - self.emit('delete', fileName); |
167 | | - self.callback({success: !ex}); |
168 | | - }); |
169 | | - }; |
170 | | - |
171 | | - UploadHandler.prototype.initUrls = function (fileInfo) { |
172 | | - var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); |
173 | | - fileInfo.setUrl(null, baseUrl + options.uploadUrl()); |
174 | | - fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); |
175 | | - _.each(options.imageVersions, function (value, version) { |
176 | | - if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { |
177 | | - fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); |
178 | | - } |
179 | | - }, this); |
180 | | - }; |
181 | | - |
182 | | - return UploadHandler; |
| 11 | + var FileInfo = require('./fileinfo')( |
| 12 | + _.extend({ |
| 13 | + baseDir: options.uploadDir |
| 14 | + }, _.pick(options, 'minFileSize', 'maxFileSize', 'acceptFileTypes')) |
| 15 | + ); |
| 16 | + |
| 17 | + var UploadHandler = function (req, res, callback) { |
| 18 | + EventEmitter.call(this); |
| 19 | + this.req = req; |
| 20 | + this.res = res; |
| 21 | + this.callback = callback; |
| 22 | + }; |
| 23 | + require('util').inherits(UploadHandler, EventEmitter); |
| 24 | + |
| 25 | + UploadHandler.prototype.noCache = function () { |
| 26 | + this.res.set({ |
| 27 | + 'Pragma': 'no-cache', |
| 28 | + 'Cache-Control': 'no-store, no-cache, must-revalidate', |
| 29 | + 'Content-Disposition': 'inline; filename="files.json"' |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + UploadHandler.prototype.get = function () { |
| 34 | + this.noCache(); |
| 35 | + var files = []; |
| 36 | + fs.readdir(options.uploadDir(), _.bind(function (err, list) { |
| 37 | + _.each(list, function (name) { |
| 38 | + var stats = fs.statSync(options.uploadDir() + '/' + name), |
| 39 | + fileInfo; |
| 40 | + if (stats.isFile()) { |
| 41 | + fileInfo = new FileInfo({ |
| 42 | + name: name, |
| 43 | + size: stats.size |
| 44 | + }); |
| 45 | + this.initUrls(fileInfo); |
| 46 | + files.push(fileInfo); |
| 47 | + } |
| 48 | + }, this); |
| 49 | + this.callback({files: files}); |
| 50 | + }, this)); |
| 51 | + }; |
| 52 | + |
| 53 | + UploadHandler.prototype.post = function () { |
| 54 | + var self = this, |
| 55 | + form = new formidable.IncomingForm(), |
| 56 | + tmpFiles = [], |
| 57 | + files = [], |
| 58 | + map = {}, |
| 59 | + counter = 1, |
| 60 | + redirect, |
| 61 | + finish = _.bind(function () { |
| 62 | + if (!--counter) { |
| 63 | + _.each(files, function (fileInfo) { |
| 64 | + this.initUrls(fileInfo); |
| 65 | + fileInfo = _.extend(fileInfo, self.req.fields); |
| 66 | + this.emit('end', fileInfo); |
| 67 | + }, this); |
| 68 | + this.callback({files: files}, redirect); |
| 69 | + } |
| 70 | + }, this); |
| 71 | + |
| 72 | + this.noCache(); |
| 73 | + |
| 74 | + form.uploadDir = options.tmpDir; |
| 75 | + form |
| 76 | + .on('fileBegin', function (name, file) { |
| 77 | + tmpFiles.push(file.path); |
| 78 | + var fileInfo = new FileInfo(file); |
| 79 | + fileInfo.safeName(); |
| 80 | + map[path.basename(file.path)] = fileInfo; |
| 81 | + files.push(fileInfo); |
| 82 | + self.emit('begin', fileInfo); |
| 83 | + }) |
| 84 | + .on('field', function (name, value) { |
| 85 | + if (name === 'redirect') { |
| 86 | + redirect = value; |
| 87 | + } |
| 88 | + if ( !self.req.fields ) |
| 89 | + self.req.fields = {}; |
| 90 | + self.req.fields[name] = value; |
| 91 | + }) |
| 92 | + .on('file', function (name, file) { |
| 93 | + var fileInfo = map[path.basename(file.path)]; |
| 94 | + if (fs.existsSync(file.path)) { |
| 95 | + fileInfo.size = file.size; |
| 96 | + if (!fileInfo.validate()) { |
| 97 | + fs.unlink(file.path); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + var generatePreviews = function () { |
| 102 | + if (options.imageTypes.test(fileInfo.name)) { |
| 103 | + _.each(options.imageVersions, function (value, version) { |
| 104 | + // creating directory recursive |
| 105 | + if (!fs.existsSync(options.uploadDir() + '/' + version + '/')) |
| 106 | + mkdirp.sync(options.uploadDir() + '/' + version + '/'); |
| 107 | + |
| 108 | + counter++; |
| 109 | + var opts = options.imageVersions[version]; |
| 110 | + imageMagick.resize({ |
| 111 | + width: opts.width, |
| 112 | + height: opts.height, |
| 113 | + srcPath: options.uploadDir() + '/' + fileInfo.name, |
| 114 | + dstPath: options.uploadDir() + '/' + version + '/' + fileInfo.name, |
| 115 | + customArgs: opts.imageArgs || ['-auto-orient'] |
| 116 | + }, finish); |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (!fs.existsSync(options.uploadDir() + '/')) |
| 122 | + mkdirp.sync(options.uploadDir() + '/'); |
| 123 | + |
| 124 | + counter++; |
| 125 | + fs.rename(file.path, options.uploadDir() + '/' + fileInfo.name, function (err) { |
| 126 | + if (!err) { |
| 127 | + generatePreviews(); |
| 128 | + finish(); |
| 129 | + } else { |
| 130 | + var is = fs.createReadStream(file.path); |
| 131 | + var os = fs.createWriteStream(options.uploadDir() + '/' + fileInfo.name); |
| 132 | + is.on('end', function (err) { |
| 133 | + if (!err) { |
| 134 | + fs.unlinkSync(file.path); |
| 135 | + generatePreviews(); |
| 136 | + } |
| 137 | + finish(); |
| 138 | + }); |
| 139 | + is.pipe(os); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + }) |
| 144 | + .on('aborted', function () { |
| 145 | + _.each(tmpFiles, function (file) { |
| 146 | + var fileInfo = map[path.basename(file)]; |
| 147 | + self.emit('abort', fileInfo); |
| 148 | + fs.unlink(file); |
| 149 | + }); |
| 150 | + }) |
| 151 | + .on('error', function (e) { |
| 152 | + self.emit('error', e); |
| 153 | + }) |
| 154 | + .on('progress', function (bytesReceived, bytesExpected) { |
| 155 | + if (bytesReceived > options.maxPostSize) |
| 156 | + self.req.connection.destroy(); |
| 157 | + }) |
| 158 | + .on('end', finish) |
| 159 | + .parse(self.req); |
| 160 | + }; |
| 161 | + |
| 162 | + UploadHandler.prototype.destroy = function () { |
| 163 | + var self = this, |
| 164 | + fileName = path.basename(decodeURIComponent(this.req.url)); |
| 165 | + |
| 166 | + fs.unlink(options.uploadDir() + '/' + fileName, function (ex) { |
| 167 | + _.each(options.imageVersions, function (value, version) { |
| 168 | + fs.unlink(options.uploadDir() + '/' + version + '/' + fileName); |
| 169 | + }); |
| 170 | + self.emit('delete', fileName); |
| 171 | + self.callback({success: !ex}); |
| 172 | + }); |
| 173 | + }; |
| 174 | + |
| 175 | + UploadHandler.prototype.initUrls = function (fileInfo) { |
| 176 | + var baseUrl = (options.ssl ? 'https:' : 'http:') + '//' + (options.hostname || this.req.get('Host')); |
| 177 | + fileInfo.setUrl(null, baseUrl + options.uploadUrl()); |
| 178 | + fileInfo.setUrl('delete', baseUrl + this.req.originalUrl); |
| 179 | + _.each(options.imageVersions, function (value, version) { |
| 180 | + if (fs.existsSync(options.uploadDir() + '/' + version + '/' + fileInfo.name)) { |
| 181 | + fileInfo.setUrl(version, baseUrl + options.uploadUrl() + '/' + version); |
| 182 | + } |
| 183 | + }, this); |
| 184 | + }; |
| 185 | + |
| 186 | + return UploadHandler; |
183 | 187 | } |
184 | 188 |
|
0 commit comments