-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufs-wabs.js
122 lines (103 loc) · 3.78 KB
/
ufs-wabs.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
if (Meteor.isServer) {
var WABS = Npm.require('azure-storage');
}
/**
* GridFS store
* @param options
* @constructor
*/
UploadFS.store.WABS = function (options) {
// Set default options
options = Object.assign({
chunkSize: 1024 * 255,
collectionName: 'uploadfs'
}, options);
// Check options
if (!Match.test(options.chunkSize, Number)) {
throw new TypeError('chunkSize is not a number');
}
if (!Match.test(options.collectionName, String)) {
throw new TypeError('collectionName is not a string');
}
// Create the store
var store = new UploadFS.Store(options);
if (Meteor.isServer) {
// Determine which folder (key prefix) in the bucket to use
var folder = options.folder;
if (typeof folder === "string" && folder.length) {
if (folder.slice(0, 1) === "/") {
folder = folder.slice(1);
}
if (folder.slice(-1) !== "/") {
folder += "/";
}
} else {
folder = "";
}
var container = options.container;
if (!container){
throw new Error('UploadFS.store.WABS you must specify the "container" option');
}
// Create WABS service
var WABSBlobService = WABS.createBlobService(options.storageAccountOrConnectionString,options.storageAccessKey);
//XXX: Is this necessary?
WABSBlobService.createContainerIfNotExists(container,function(){});
/**
* Removes the file
* @param fileId
* @param callback
*/
store.delete = function (fileId, callback) {
if (typeof callback !== 'function') {
callback = function (err) {
err && console.error(`ufs: cannot delete file "${ fileId }" at ${ path } (${ err.message })`);
}
}
WABSBlobService.deleteBlob(container, folder + fileId, function(error) {
callback(error, !error);
});
};
/**
* Returns the file read stream
* @param fileId
* @return {*}
*/
store.getReadStream = function (fileId) {
return WABSBlobService.createReadStream(container,folder + fileId);
};
/**
* Returns the file write stream
* @param fileId
* @return {*}
*/
store.getWriteStream = function (fileId, file) {
var writeStream = WABSBlobService.createWriteStreamToBlockBlob(container, folder + fileId);
// The filesystem does not emit the "end" event only close - so we
// manually send the end event
writeStream.on('close', function() {
WABSBlobService.getBlobProperties(container, folder + fileId, function (error, properties) {
if (error) {
writeStream.emit('error', error);
} else {
var size;
if (options.rangeStart) {
var endOffset = properties.contentLength - 1;
var end = options.rangeEnd ? Math.min(options.rangeEnd, endOffset) : endOffset;
size = end - options.rangeStart + 1;
} else {
size = properties.contentLength;
}
// Emit end and return the fileId, size, and updated date
writeStream.emit('stored', {
fileId: fileId,
size: size,
storedAt: new Date()
});
}
});
});
return writeStream;
};
}
return store;
};