Skip to content

Commit 83a2827

Browse files
committed
add support to promise
1 parent e76d571 commit 83a2827

File tree

5 files changed

+291
-22
lines changed

5 files changed

+291
-22
lines changed

lib/providers/filesystem/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var fs = require('fs'),
1818
File = require('./file').File,
1919
Container = require('./container').Container;
2020

21+
var utils = require('./../../utils');
22+
2123
module.exports.storage = module.exports; // To make it consistent with pkgcloud
2224

2325
module.exports.File = File;
@@ -95,6 +97,8 @@ function populateMetadata(stat, props) {
9597
}
9698

9799
FileSystemProvider.prototype.getContainers = function(cb) {
100+
cb = cb || utils.createPromiseCallback();
101+
98102
var self = this;
99103
fs.readdir(self.root, function(err, files) {
100104
var containers = [];
@@ -119,15 +123,20 @@ FileSystemProvider.prototype.getContainers = function(cb) {
119123
}
120124
});
121125
});
126+
127+
return cb.promise;
122128
};
123129

124130
FileSystemProvider.prototype.createContainer = function(options, cb) {
131+
cb = cb || utils.createPromiseCallback();
132+
125133
var self = this;
126134
var name = options.name;
127135
var dir = path.join(this.root, name);
128136
validateName(name, cb) && fs.mkdir(dir, options, function(err) {
129137
if (err) {
130-
return cb && cb(err);
138+
cb && cb(err);
139+
return;
131140
}
132141
fs.stat(dir, function(err, stat) {
133142
var container = null;
@@ -139,9 +148,13 @@ FileSystemProvider.prototype.createContainer = function(options, cb) {
139148
cb && cb(err, container);
140149
});
141150
});
151+
152+
return cb.promise;
142153
};
143154

144155
FileSystemProvider.prototype.destroyContainer = function(containerName, cb) {
156+
cb = cb || utils.createPromiseCallback();
157+
145158
if (!validateName(containerName, cb)) return;
146159

147160
var dir = path.join(this.root, containerName);
@@ -160,9 +173,13 @@ FileSystemProvider.prototype.destroyContainer = function(containerName, cb) {
160173
}
161174
});
162175
});
176+
177+
return cb.promise;
163178
};
164179

165180
FileSystemProvider.prototype.getContainer = function(containerName, cb) {
181+
cb = cb || utils.createPromiseCallback();
182+
166183
var self = this;
167184
if (!validateName(containerName, cb)) return;
168185
var dir = path.join(this.root, containerName);
@@ -175,6 +192,8 @@ FileSystemProvider.prototype.getContainer = function(containerName, cb) {
175192
}
176193
cb && cb(err, container);
177194
});
195+
196+
return cb.promise;
178197
};
179198

180199
// File related functions
@@ -252,6 +271,9 @@ FileSystemProvider.prototype.getFiles = function(container, options, cb) {
252271
cb = options;
253272
options = false;
254273
}
274+
275+
cb = cb || utils.createPromiseCallback();
276+
255277
var self = this;
256278
if (!validateName(container, cb)) return;
257279
var dir = path.join(this.root, container);
@@ -278,9 +300,13 @@ FileSystemProvider.prototype.getFiles = function(container, options, cb) {
278300
}
279301
});
280302
});
303+
304+
return cb.promise;
281305
};
282306

283307
FileSystemProvider.prototype.getFile = function(container, file, cb) {
308+
cb = cb || utils.createPromiseCallback();
309+
284310
var self = this;
285311
if (!validateName(container, cb)) return;
286312
if (!validateName(file, cb)) return;
@@ -294,6 +320,8 @@ FileSystemProvider.prototype.getFile = function(container, file, cb) {
294320
}
295321
cb && cb(err, f);
296322
});
323+
324+
return cb.promise;
297325
};
298326

299327
FileSystemProvider.prototype.getUrl = function(options) {
@@ -303,9 +331,13 @@ FileSystemProvider.prototype.getUrl = function(options) {
303331
};
304332

305333
FileSystemProvider.prototype.removeFile = function(container, file, cb) {
334+
cb = cb || utils.createPromiseCallback();
335+
306336
if (!validateName(container, cb)) return;
307337
if (!validateName(file, cb)) return;
308338

309339
var filePath = path.join(this.root, container, file);
310340
fs.unlink(filePath, cb);
341+
342+
return cb.promise;
311343
};

lib/storage-service.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var factory = require('./factory');
88
var handler = require('./storage-handler');
9+
var utils = require('./utils');
910

1011
var storage = require('pkgcloud').storage;
1112
var debug = require('debug')('loopback:storage:service');
@@ -64,8 +65,11 @@ function map(obj) {
6465
* @callback {Function} callback Callback function
6566
* @param {Object|String} err Error string or object
6667
* @param {Object[]} containers An array of container metadata objects
68+
* @promise
6769
*/
6870
StorageService.prototype.getContainers = function(cb) {
71+
cb = cb || utils.createPromiseCallback();
72+
6973
this.client.getContainers(function(err, containers) {
7074
if (err) {
7175
cb(err, containers);
@@ -75,6 +79,8 @@ StorageService.prototype.getContainers = function(cb) {
7579
}));
7680
}
7781
});
82+
83+
return cb.promise;
7884
};
7985

8086
/**
@@ -85,29 +91,38 @@ StorageService.prototype.getContainers = function(cb) {
8591
* @callback {Function} cb Callback function
8692
* @param {Object|String} err Error string or object
8793
* @param {Object} container Container metadata object
94+
* @promise
8895
*/
8996

9097
StorageService.prototype.createContainer = function(options, cb) {
9198
options = options || {};
99+
cb = cb || utils.createPromiseCallback();
100+
92101
if ('object' === typeof options && !(options instanceof storage.Container)) {
93102
options.Name = options.name; // Amazon expects Name
94103
var Container = factory.getProvider(this.provider).storage.Container;
95104
options = new Container(this.client, options);
96105
}
97106
debug('Creating container with options %o', options);
98-
return this.client.createContainer(options, function(err, container) {
107+
this.client.createContainer(options, function(err, container) {
99108
return cb(err, map(container));
100109
});
110+
111+
return cb.promise;
101112
};
102113

103114
/**
104115
* Destroy an existing storage service container.
105116
* @param {String} container Container name.
106117
* @callback {Function} callback Callback function.
107118
* @param {Object|String} err Error string or object
119+
* @promise
108120
*/
109121
StorageService.prototype.destroyContainer = function(container, cb) {
110-
return this.client.destroyContainer(container, cb);
122+
cb = cb || utils.createPromiseCallback();
123+
124+
this.client.destroyContainer(container, cb);
125+
return cb.promise;
111126
};
112127

113128
/**
@@ -116,15 +131,20 @@ StorageService.prototype.destroyContainer = function(container, cb) {
116131
* @callback {Function} callback Callback function.
117132
* @param {Object|String} err Error string or object
118133
* @param {Object} container Container metadata object
134+
* @promise
119135
*/
120136
StorageService.prototype.getContainer = function(container, cb) {
121-
return this.client.getContainer(container, function(err, container) {
137+
cb = cb || utils.createPromiseCallback();
138+
139+
this.client.getContainer(container, function(err, container) {
122140
if (err && err.code === 'ENOENT') {
123141
err.statusCode = err.status = 404;
124142
return cb(err);
125143
}
126144
return cb(err, map(container));
127145
});
146+
147+
return cb.promise;
128148
};
129149

130150
/**
@@ -178,14 +198,18 @@ StorageService.prototype.downloadStream = function(container, file, options) {
178198
* @callback {Function} cb Callback function
179199
* @param {Object|String} err Error string or object
180200
* @param {Object[]} files An array of file metadata objects
201+
* @promise
181202
*/
182203
StorageService.prototype.getFiles = function(container, options, cb) {
183204
if (typeof options === 'function' && !cb) {
184205
// options argument is not present
185206
cb = options;
186207
options = {};
187208
}
188-
return this.client.getFiles(container, options, function(err, files) {
209+
210+
cb = cb || utils.createPromiseCallback();
211+
212+
this.client.getFiles(container, options, function(err, files) {
189213
if (err) {
190214
cb(err, files);
191215
} else {
@@ -194,6 +218,8 @@ StorageService.prototype.getFiles = function(container, options, cb) {
194218
}));
195219
}
196220
});
221+
222+
return cb.promise;
197223
};
198224

199225
/**
@@ -203,15 +229,20 @@ StorageService.prototype.getFiles = function(container, options, cb) {
203229
* @callback {Function} cb Callback function
204230
* @param {Object|String} err Error string or object
205231
* @param {Object} file File metadata object
232+
* @promise
206233
*/
207234
StorageService.prototype.getFile = function(container, file, cb) {
208-
return this.client.getFile(container, file, function(err, f) {
235+
cb = cb || utils.createPromiseCallback();
236+
237+
this.client.getFile(container, file, function(err, f) {
209238
if (err && err.code === 'ENOENT') {
210239
err.statusCode = err.status = 404;
211240
return cb(err);
212241
}
213242
return cb(err, map(f));
214243
});
244+
245+
return cb.promise;
215246
};
216247

217248
/**
@@ -220,9 +251,13 @@ StorageService.prototype.getFile = function(container, file, cb) {
220251
* @param {String} file File name
221252
* @callback {Function} cb Callback function
222253
* @param {Object|String} err Error string or object
254+
* @promise
223255
*/
224256
StorageService.prototype.removeFile = function(container, file, cb) {
225-
return this.client.removeFile(container, file, cb);
257+
cb = cb || utils.createPromiseCallback();
258+
259+
this.client.removeFile(container, file, cb);
260+
return cb.promise;
226261
};
227262

228263
/**
@@ -232,6 +267,7 @@ StorageService.prototype.removeFile = function(container, file, cb) {
232267
* @param {Response} res Response object
233268
* @param {Object} [options] Options for upload
234269
* @param {Function} cb Callback function
270+
* @promise
235271
*/
236272
StorageService.prototype.upload = function(container, req, res, options, cb) {
237273
debug('Configuring upload with options %o', options);
@@ -247,6 +283,9 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
247283
cb = options;
248284
options = {};
249285
}
286+
287+
cb = cb || utils.createPromiseCallback();
288+
250289
if (this.getFilename && !options.getFilename) {
251290
options.getFilename = this.getFilename;
252291
}
@@ -269,7 +308,9 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
269308
options.container = container;
270309
}
271310
debug('Upload configured with options %o', options);
272-
return handler.upload(this.client, req, res, options, cb);
311+
312+
handler.upload(this.client, req, res, options, cb);
313+
return cb.promise;
273314
};
274315

275316
/**
@@ -279,9 +320,13 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
279320
* @param {Request} req HTTP request
280321
* @param {Response} res HTTP response
281322
* @param {Function} cb Callback function
323+
* @promise
282324
*/
283325
StorageService.prototype.download = function(container, file, req, res, cb) {
284-
return handler.download(this.client, req, res, container, file, cb);
326+
cb = cb || utils.createPromiseCallback();
327+
328+
handler.download(this.client, req, res, container, file, cb);
329+
return cb.promise;
285330
};
286331

287332
StorageService.modelName = 'storage';

lib/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
exports.createPromiseCallback = createPromiseCallback;
4+
5+
function createPromiseCallback() {
6+
var cb;
7+
var promise = new Promise(function(resolve, reject) {
8+
cb = function(err, data) {
9+
if (err) return reject(err);
10+
return resolve(data);
11+
};
12+
});
13+
cb.promise = promise;
14+
return cb;
15+
}

0 commit comments

Comments
 (0)