This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
fsFile-server.js
359 lines (317 loc) · 11.7 KB
/
fsFile-server.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
/**
* @method FS.File.prototype.beginStorage
* @public
* @return {undefined}
*
*/
FS.File.prototype.beginStorage = function() {
var self = this;
// Save the binary to a single chunk temp file,
// so that it is available when FileWorker calls saveCopies.
// This will also trigger file handling from event listeners.
self.createReadStream().pipe(FS.TempStore.createWriteStream(self));
}
/**
* Notes a details about a storage adapter failure within the file record
* @param {string} storeName
* @param {number} maxTries
* @return {undefined}
* @todo deprecate this
*/
FS.File.prototype.logCopyFailure = function(storeName, maxTries) {
var self = this;
// hasStored will update from the fileRecord
if (self.hasStored(storeName)) {
throw new Error("logCopyFailure: invalid storeName");
}
// Make sure we have a temporary file saved since we will be
// trying the save again.
FS.TempStore.ensureForFile(self);
var now = new Date();
var currentCount = (self.failures && self.failures.copies && self.failures.copies[storeName] && typeof self.failures.copies[storeName].count === "number") ? self.failures.copies[storeName].count : 0;
maxTries = maxTries || 5;
var modifier = {};
modifier.$set = {};
modifier.$set['failures.copies.' + storeName + '.lastAttempt'] = now;
if (currentCount === 0) {
modifier.$set['failures.copies.' + storeName + '.firstAttempt'] = now;
}
modifier.$set['failures.copies.' + storeName + '.count'] = currentCount + 1;
modifier.$set['failures.copies.' + storeName + '.doneTrying'] = (currentCount + 1 >= maxTries);
self.update(modifier);
};
/**
* Has this store permanently failed?
* @param {String} storeName The name of the store
* @return {boolean} Has this store failed permanently?
* @todo deprecate this
*/
FS.File.prototype.failedPermanently = function(storeName) {
var self = this;
return !!(self.failures &&
self.failures.copies &&
self.failures.copies[storeName] &&
self.failures.copies[storeName].doneTrying);
};
/**
* @method FS.File.prototype.createReadStream
* @public
* @param {String} [storeName]
* @returns {stream.Readable} Readable NodeJS stream
*
* Returns a readable stream. Where the stream reads from depends on the FS.File instance and whether you pass a store name.
*
* * If you pass a `storeName`, a readable stream for the file data saved in that store is returned.
* * If you don't pass a `storeName` and data is attached to the FS.File instance (on `data` property, which must be a DataMan instance), then a readable stream for the attached data is returned.
* * If you don't pass a `storeName` and there is no data attached to the FS.File instance, a readable stream for the file data currently in the temporary store (`FS.TempStore`) is returned.
*
*/
FS.File.prototype.createReadStream = function(storeName) {
var self = this;
// If we dont have a store name but got Buffer data?
if (!storeName && self.data) {
FS.debug && console.log("fileObj.createReadStream creating read stream for attached data");
// Stream from attached data if present
return self.data.createReadStream();
} else if (!storeName && FS.TempStore && FS.TempStore.exists(self)) {
FS.debug && console.log("fileObj.createReadStream creating read stream for temp store");
// Stream from temp store - its a bit slower than regular streams?
return FS.TempStore.createReadStream(self);
} else {
// Stream from the store using storage adapter
if (self.isMounted()) {
var storage = self.collection.storesLookup[storeName] || self.collection.primaryStore;
FS.debug && console.log("fileObj.createReadStream creating read stream for store", storage.name);
// return stream
return storage.adapter.createReadStream(self);
} else {
throw new Meteor.Error('File not mounted');
}
}
};
/**
* @method FS.File.prototype.createWriteStream
* @public
* @param {String} [storeName]
* @returns {stream.Writeable} Writeable NodeJS stream
*
* Returns a writeable stream. Where the stream writes to depends on whether you pass in a store name.
*
* * If you pass a `storeName`, a writeable stream for (over)writing the file data in that store is returned.
* * If you don't pass a `storeName`, a writeable stream for writing to the temp store for this file is returned.
*
*/
FS.File.prototype.createWriteStream = function(storeName) {
var self = this;
// We have to have a mounted file in order for this to work
if (self.isMounted()) {
if (!storeName && FS.TempStore && FS.FileWorker) {
// If we have worker installed - we pass the file to FS.TempStore
// We dont need the storeName since all stores will be generated from
// TempStore.
// This should trigger FS.FileWorker at some point?
FS.TempStore.createWriteStream(self);
} else {
// Stream directly to the store using storage adapter
var storage = self.collection.storesLookup[storeName] || self.collection.primaryStore;
return storage.adapter.createWriteStream(self);
}
} else {
throw new Meteor.Error('File not mounted');
}
};
/**
* @method FS.File.prototype.copy Makes a copy of the file and underlying data in all stores.
* @public
* @returns {FS.File} The new FS.File instance
*/
FS.File.prototype.copy = function() {
var self = this;
if (!self.isMounted()) {
throw new Error("Cannot copy a file that is not associated with a collection");
}
// Get the file record
var fileRecord = self.collection.files.findOne({_id: self._id}, {transform: null}) || {};
// Remove _id and copy keys from the file record
delete fileRecord._id;
// Insert directly; we don't have access to "original" in this case
var newId = self.collection.files.insert(fileRecord);
var newFile = self.collection.findOne(newId);
// Copy underlying files in the stores
var mod, oldKey;
for (var name in newFile.copies) {
if (newFile.copies.hasOwnProperty(name)) {
oldKey = newFile.copies[name].key;
if (oldKey) {
// We need to ask the adapter for the true oldKey because
// right now gridfs does some extra stuff.
// TODO GridFS should probably set the full key object
// (with _id and filename) into `copies.key`
// so that copies.key can be passed directly to
// createReadStreamForFileKey
var sourceFileStorage = self.collection.storesLookup[name];
if (!sourceFileStorage) {
throw new Error(name + " is not a valid store name");
}
oldKey = sourceFileStorage.adapter.fileKey(self);
// delete so that new fileKey will be generated in copyStoreData
delete newFile.copies[name].key;
mod = mod || {};
mod["copies." + name + ".key"] = copyStoreData(newFile, name, oldKey);
}
}
}
// Update keys in the filerecord
if (mod) {
newFile.update({$set: mod});
}
return newFile;
};
Meteor.methods({
// Does a HEAD request to URL to get the type, updatedAt,
// and size prior to actually downloading the data.
// That way we can do filter checks without actually downloading.
'_cfs_getUrlInfo': function (url, options) {
check(url, String);
check(options, Object);
this.unblock();
var response = HTTP.call("HEAD", url, options);
var headers = response.headers;
var result = {};
if (headers['content-type']) {
result.type = headers['content-type'];
}
if (headers['content-length']) {
result.size = +headers['content-length'];
}
if (headers['last-modified']) {
result.updatedAt = new Date(headers['last-modified']);
}
return result;
}
});
// TODO maybe this should be in cfs-storage-adapter
function _copyStoreData(fileObj, storeName, sourceKey, callback) {
if (!fileObj.isMounted()) {
throw new Error("Cannot copy store data for a file that is not associated with a collection");
}
var storage = fileObj.collection.storesLookup[storeName];
if (!storage) {
throw new Error(storeName + " is not a valid store name");
}
// We want to prevent beforeWrite and transformWrite from running, so
// we interact directly with the store.
var destinationKey = storage.adapter.fileKey(fileObj);
var readStream = storage.adapter.createReadStreamForFileKey(sourceKey);
var writeStream = storage.adapter.createWriteStreamForFileKey(destinationKey);
writeStream.once('stored', function(result) {
callback(null, result.fileKey);
});
writeStream.once('error', function(error) {
callback(error);
});
readStream.pipe(writeStream);
}
var copyStoreData = Meteor.wrapAsync(_copyStoreData);
/**
* @method FS.File.prototype.copyData Copies the content of a store directly into another store.
* @public
* @param {string} sourceStoreName
* @param {string} targetStoreName
* @param {boolean=} move
*/
FS.File.prototype.copyData = function(sourceStoreName, targetStoreName, move){
move = !!move;
/**
* @type {Object.<string,*>}
*/
var sourceStoreValues = this.copies[sourceStoreName];
/**
* @type {string}
*/
var copyKey = cloneDataToStore(this, sourceStoreName, targetStoreName, move);
/**
* @type {Object.<string,*>}
*/
var targetStoreValues = {};
for (var v in sourceStoreValues) {
if (sourceStoreValues.hasOwnProperty(v)) {
targetStoreValues[v] = sourceStoreValues[v]
}
}
targetStoreValues.key = copyKey;
targetStoreValues.createdAt = new Date();
targetStoreValues.updatedAt = new Date();
/**
*
* @type {modifier}
*/
var modifier = {};
modifier.$set = {};
modifier.$set["copies."+targetStoreName] = targetStoreValues;
if(move){
modifier.$unset = {};
modifier.$unset["copies."+sourceStoreName] = "";
}
this.update(modifier);
};
/**
* @method FS.File.prototype.moveData Moves the content of a store directly into another store.
* @public
* @param {string} sourceStoreName
* @param {string} targetStoreName
*/
FS.File.prototype.moveData = function(sourceStoreName, targetStoreName){
this.copyData(sourceStoreName, targetStoreName, true);
};
// TODO maybe this should be in cfs-storage-adapter
/**
*
* @param {FS.File} fileObj
* @param {string} sourceStoreName
* @param {string} targetStoreName
* @param {boolean} move
* @param callback
* @private
*/
function _copyDataFromStoreToStore(fileObj, sourceStoreName, targetStoreName, move, callback) {
if (!fileObj.isMounted()) {
throw new Error("Cannot copy store data for a file that is not associated with a collection");
}
/**
* @type {FS.StorageAdapter}
*/
var sourceStorage = fileObj.collection.storesLookup[sourceStoreName];
/**
* @type {FS.StorageAdapter}
*/
var targetStorage = fileObj.collection.storesLookup[targetStoreName];
if (!sourceStorage) {
throw new Error(sourceStoreName + " is not a valid store name");
}
if (!targetStorage) {
throw new Error(targetStorage + " is not a valid store name");
}
// We want to prevent beforeWrite and transformWrite from running, so
// we interact directly with the store.
var sourceKey = sourceStorage.adapter.fileKey(fileObj);
var targetKey = targetStorage.adapter.fileKey(fileObj);
var readStream = sourceStorage.adapter.createReadStreamForFileKey(sourceKey);
var writeStream = targetStorage.adapter.createWriteStreamForFileKey(targetKey);
writeStream.safeOnce('stored', function(result) {
if(move && sourceStorage.adapter.remove(fileObj)===false){
callback("Copied to store:" + targetStoreName
+ " with fileKey: "
+ result.fileKey
+ ", but could not delete from source store: "
+ sourceStoreName);
}else{
callback(null, result.fileKey);
}
});
writeStream.once('error', function(error) {
callback(error);
});
readStream.pipe(writeStream);
}
var cloneDataToStore = Meteor.wrapAsync(_copyDataFromStoreToStore);