-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathattachments.js
359 lines (324 loc) · 14.6 KB
/
attachments.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2011-2013 Firebase.co - http://www.firebase.co
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var im = require('imagemagick');
var fs = require('fs');
var path = require('path');
var async = require('async');
var existsFn = fs.exists || path.exists;
// keeps a global registry of storage providers
var providersRegistry = {};
var supportedDecodingFormats = [
'PNG',
'GIF',
'TIFF',
'JPEG'
];
function findProvider(name) {
var provider = providersRegistry[name];
if(!provider) throw new Error('Storage Provider "' + name + '" can not be found');
return provider;
}
function findImageMagickFormats(options, callback) {
var opts = { read: true };
if (typeof options === 'function') {
callback = options;
} else if (options.read || options.write || options.multi || options.blob ) {
opts = options;
} else {
callback(new Error("Options have to contain one or more of 'read', 'write', 'multi', 'blob'"));
}
im.convert(['-list','format'], function(err, stdout, stderr) {
if (err) return callback(err);
if (stderr && stderr.search(/\S/) >= 0) return callback(new Error(stderr));
if (stdout && stdout.search(/\S/) >= 0) {
// capture groups:
// 0: all
// 1: format
// 2: if '*' = native blob support; if ' ' (whitespace) none. Not set with graphicsmagick - therefore optional in regex
// 3: module
// 4: if 'r' = read support; if '-' none
// 5: if 'w' = write support; if '-' none
// 6: if '+' = support for multiple images; if '-' none
// 7: description
var regex = /^\s*([^\*\s]+)(\*|\s)?\s(\S+)\s+([-r])([-w])([-+])\s+(.*)$/;
var lines = stdout.split("\n");
var comps = [];
var formats = [];
var i, currentLine;
for (i in lines) {
currentLine = lines[i];
comps = regex.exec(currentLine);
if (comps) {
if ((!opts.read || comps[4] === 'r') &&
(!opts.write || comps[5] === 'w') &&
(!opts.multi || comps[6] === '+') &&
(!opts.blob || comps[2] === '*')) {
formats.push(comps[1]);
}
}
}
return callback(null,formats);
} else {
return callback(new Error("No format supports the requested operation(s): "
+ Object.keys(opts).toString()
+ " . Check 'convert -list format'"));
}
});
}
var plugin = function(schema, options) {
options = options || {};
if(typeof(options.directory) !== 'string') throw new Error('option "directory" is required');
if(typeof(options.properties) !== 'object') throw new Error('option "properties" is required');
if(typeof(options.storage) !== 'object') throw new Error('option "storage" is required');
var storageOptions = options.storage;
storageOptions.schema = schema;
if(typeof(storageOptions.providerName) !== 'string') throw new Error('option "storage.providerName" is required');
var providerPrototype = findProvider(storageOptions.providerName);
var providerOptions = storageOptions.options || {};
var providerInstance = new providerPrototype(providerOptions);
var propertyNames = Object.keys(options.properties);
propertyNames.forEach(function(propertyName) {
var propertyOptions = options.properties[propertyName];
if(!propertyOptions) throw new Error('property "' + propertyName + '" requires an specification');
var styles = propertyOptions.styles || {};
var styleNames = Object.keys(styles);
if(styleNames.length == 0) throw new Error('property "' + propertyName + '" needs to define at least one style');
var addOp = {};
var propSchema = addOp[propertyName] = {};
styleNames.forEach(function(styleName) {
propSchema[styleName] = {
size: Number // Size of the File
, oname: String // Original name of the file
, mtime: Date
, ctime: Date
, path: String // Storage Path
, defaultUrl: String // Default (non-secure, most of the time public) Url
, format: String // Format of the File(provided by identify).
, depth: Number
, dims: { // Dimensions of the Image
h: Number, // Height
w: Number // Width
}
};
});
// Add the Property
schema.add(addOp);
}); // for each property name
// Finally we set the method 'attach'
// => propertyName: String. Name of the property to attach the file to.
// => attachmentInfo: {
// path: String(required). Path to the file in the file system.
// name: String(optional). Original Name of the file.
// mime: String(optional). Mime type of the file.
// category: String(optional). A top-level grouping for the files.
schema.methods.attach = function(propertyName, attachmentInfo, cb) {
var selfModel = this;
if(propertyNames.indexOf(propertyName) == -1) return cb(new Error('property "' + propertyName + '" was not registered as an attachment property'));
var propertyOptions = options.properties[propertyName];
var styles = propertyOptions.styles || {};
if(!attachmentInfo || typeof(attachmentInfo) !== 'object') return cb(new Error('attachmentInfo is not valid'));
if(typeof(attachmentInfo.path) !== 'string') return cb(new Error('attachmentInfo has no valid path'));
if(!attachmentInfo.name) {
// No original name provided? We infer it from the path
attachmentInfo.name = path.basename(attachmentInfo.path);
}
existsFn(attachmentInfo.path, function(exists) {
if(!exists) return cb(new Error('file to attach at path "' + attachmentInfo.path + '" does not exists'));
fs.stat(attachmentInfo.path, function(err, stats) {
if(!stats.isFile()) return cb(new Error('path to attach from "' + attachmentInfo.path + '" is not a file'));
im.identify(attachmentInfo.path, function(err, atts) {
// if 'identify' fails, that probably means the file is not an image.
var canTransform = !!atts && supportedDecodingFormats.indexOf(atts.format) != -1;
var fileExt = path.extname(attachmentInfo.path);
var styles = propertyOptions.styles || {};
var styleNames = Object.keys(styles);
var tasks = [];
var stylesToReset = []; // names of the style that needs to be reset at the end of the process.
styleNames.forEach(function(styleName) {
var styleOptions = styles[styleName] || {};
var finishConversion = function(styleFilePath, atts, cb) {
var ext = path.extname(styleFilePath);
var filenameId = options.filenameId ? selfModel[options.filenameId] : selfModel.id;
var storageStylePath;
if(attachmentInfo.category) {
storageStylePath = '/' + options.directory + '/' + attachmentInfo.category + '/' + filenameId + '-' + styleName + ext;
} else {
storageStylePath = '/' + options.directory + '/' + filenameId + '-' + styleName + ext;
}
fs.stat(styleFilePath, function(err, stats) {
if(err) return cb(err);
cb(null, {
style: {
name: styleName,
options: styleOptions
},
filename: styleFilePath,
stats: stats,
propertyName: propertyName,
model: selfModel,
path: storageStylePath,
defaultUrl: null, // let the storage assign this
features: atts
});
});
};
var optionKeys = Object.keys(styleOptions);
var transformationNames = [];
optionKeys.forEach(function(transformationName) {
if(transformationName.indexOf('$') != 0) { // if is not special command, add it as an special transformation argument
transformationNames.push(transformationName);
}
});
if(optionKeys.length != 0) {
if(canTransform) {
var styleFileExt = styleOptions['$format'] ? ('.' + styleOptions['$format']) : fileExt;
var styleFileName = path.basename(attachmentInfo.path, fileExt);
styleFileName += '-' + styleName + styleFileExt;
var styleFilePath = path.join(path.dirname(attachmentInfo.path), styleFileName);
var convertArgs = [attachmentInfo.path]; // source file name
// add all the transformations args
transformationNames.forEach(function(transformationName) {
convertArgs.push('-' + transformationName);
if (styleOptions[transformationName] instanceof Array) {
styleOptions[transformationName].forEach(function (arg) {
convertArgs.push(arg);
});
} else {
convertArgs.push(styleOptions[transformationName]);
}
});
convertArgs.push(styleFilePath);
tasks.push(function(cb) {
// invoke 'convert'
im.convert(convertArgs, function(err, stdout, stderr) {
if(err) return cb(err);
// run identify in the styled image
im.identify(styleFilePath, function(err, atts) {
if(err) return cb(err);
finishConversion(styleFilePath, atts, cb);
});
});
}); // tasks.push
} else {
stylesToReset.push(styleName);
}// if can decode
} else {
// keep the file as original
tasks.push(function(cb) {
finishConversion(attachmentInfo.path, atts, cb);
});
}
}); // for each style
async.parallel(tasks, function(err, convertResults) {
if(err) return cb(err);
//console.log(convertResults);
tasks = [];
convertResults.forEach(function(convertResult) {
tasks.push(function(cb) {
// tell the provider to create or replace the attachment
providerInstance.createOrReplace(convertResult, function(err, attachment) {
if(err) return cb(err);
cb(null, attachment);
});
});
});
async.parallel(tasks, function(err, storageResults) {
if(err) return cb(err);
// Finally Update the Model
var propModel = selfModel[propertyName];
if(storageResults.length > 0) { // only update the model if a transformation was performed.
storageResults.forEach(function(styleStorage) {
var modelStyle = propModel[styleStorage.style.name];
modelStyle.defaultUrl = styleStorage.defaultUrl;
modelStyle.path = styleStorage.path;
modelStyle.size = styleStorage.stats.size;
modelStyle.mime = styleStorage.mime;
modelStyle.ctime = styleStorage.stats.ctime;
modelStyle.mtime = styleStorage.stats.mtime;
modelStyle.oname = attachmentInfo.name; // original name of the file
if(atts) {
modelStyle.format = styleStorage.features.format;
modelStyle.depth = styleStorage.features.depth;
modelStyle.dims.h = styleStorage.features.height;
modelStyle.dims.w = styleStorage.features.width;
}
});
}
stylesToReset.forEach(function(resetStyleName) {
var path = [propertyName, resetStyleName].join('.');
selfModel.set(path, null);
});
cb(null);
});
});
});
});
});
}; // method attach
};
// Prototype for Storage Providers
function StorageProvider(options) {
this.options = options;
}
StorageProvider.prototype.update = function(attachment, cb) {
throw new Error('method update implemented');
};
plugin.StorageProvider = StorageProvider;
// Method to Register Storage Providers
plugin.registerStorageProvider = function(name, provider) {
if(typeof(name) !== 'string') throw new Error('storage engine name is required');
if(provider && provider._super == StorageProvider) throw new Error('provider is not valid. it does not inherits from StorageEngine');
providersRegistry[name] = provider;
}
// Register a Known Decoding Format(e.g 'PNG')
plugin.registerDecodingFormat = function(name) {
supportedDecodingFormats.push(name);
}
/*
* Use this to register all formats for which your local ImageMagick installation supports
* read operations.
*/
plugin.registerImageMagickDecodingFormats = function() {
plugin.registerImageMagickFormats({ read: true });
}
/*
* You can register formats based on certain modes or a combination of those:
* 'read' : true|false
* 'write': true|false
* 'multi': true|false
* 'blob' : true|false
* options is optional and defaults to { read: true }. If several modes with value true are given,
* only formats supporting all of them are included.
*/
plugin.registerImageMagickFormats = function(options, callback) {
if (!callback) {
callback = function(error, formats) {
if (error) throw new Error(error);
else if (formats && formats.length > 0) {
supportedDecodingFormats = formats;
} else {
throw new Error("No formats supported for decoding!");
}
};
}
findImageMagickFormats(options, callback);
}
// Export the Plugin for mongoose.js
module.exports = plugin;