6
6
7
7
var factory = require ( './factory' ) ;
8
8
var handler = require ( './storage-handler' ) ;
9
+ var utils = require ( './utils' ) ;
9
10
10
11
var storage = require ( 'pkgcloud' ) . storage ;
11
12
var debug = require ( 'debug' ) ( 'loopback:storage:service' ) ;
@@ -64,8 +65,11 @@ function map(obj) {
64
65
* @callback {Function } callback Callback function
65
66
* @param {Object|String } err Error string or object
66
67
* @param {Object[] } containers An array of container metadata objects
68
+ * @promise
67
69
*/
68
70
StorageService . prototype . getContainers = function ( cb ) {
71
+ cb = cb || utils . createPromiseCallback ( ) ;
72
+
69
73
this . client . getContainers ( function ( err , containers ) {
70
74
if ( err ) {
71
75
cb ( err , containers ) ;
@@ -75,6 +79,8 @@ StorageService.prototype.getContainers = function(cb) {
75
79
} ) ) ;
76
80
}
77
81
} ) ;
82
+
83
+ return cb . promise ;
78
84
} ;
79
85
80
86
/**
@@ -85,29 +91,38 @@ StorageService.prototype.getContainers = function(cb) {
85
91
* @callback {Function } cb Callback function
86
92
* @param {Object|String } err Error string or object
87
93
* @param {Object } container Container metadata object
94
+ * @promise
88
95
*/
89
96
90
97
StorageService . prototype . createContainer = function ( options , cb ) {
91
98
options = options || { } ;
99
+ cb = cb || utils . createPromiseCallback ( ) ;
100
+
92
101
if ( 'object' === typeof options && ! ( options instanceof storage . Container ) ) {
93
102
options . Name = options . name ; // Amazon expects Name
94
103
var Container = factory . getProvider ( this . provider ) . storage . Container ;
95
104
options = new Container ( this . client , options ) ;
96
105
}
97
106
debug ( 'Creating container with options %o' , options ) ;
98
- return this . client . createContainer ( options , function ( err , container ) {
107
+ this . client . createContainer ( options , function ( err , container ) {
99
108
return cb ( err , map ( container ) ) ;
100
109
} ) ;
110
+
111
+ return cb . promise ;
101
112
} ;
102
113
103
114
/**
104
115
* Destroy an existing storage service container.
105
116
* @param {String } container Container name.
106
117
* @callback {Function } callback Callback function.
107
118
* @param {Object|String } err Error string or object
119
+ * @promise
108
120
*/
109
121
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 ;
111
126
} ;
112
127
113
128
/**
@@ -116,15 +131,20 @@ StorageService.prototype.destroyContainer = function(container, cb) {
116
131
* @callback {Function } callback Callback function.
117
132
* @param {Object|String } err Error string or object
118
133
* @param {Object } container Container metadata object
134
+ * @promise
119
135
*/
120
136
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 ) {
122
140
if ( err && err . code === 'ENOENT' ) {
123
141
err . statusCode = err . status = 404 ;
124
142
return cb ( err ) ;
125
143
}
126
144
return cb ( err , map ( container ) ) ;
127
145
} ) ;
146
+
147
+ return cb . promise ;
128
148
} ;
129
149
130
150
/**
@@ -178,14 +198,18 @@ StorageService.prototype.downloadStream = function(container, file, options) {
178
198
* @callback {Function } cb Callback function
179
199
* @param {Object|String } err Error string or object
180
200
* @param {Object[] } files An array of file metadata objects
201
+ * @promise
181
202
*/
182
203
StorageService . prototype . getFiles = function ( container , options , cb ) {
183
204
if ( typeof options === 'function' && ! cb ) {
184
205
// options argument is not present
185
206
cb = options ;
186
207
options = { } ;
187
208
}
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 ) {
189
213
if ( err ) {
190
214
cb ( err , files ) ;
191
215
} else {
@@ -194,6 +218,8 @@ StorageService.prototype.getFiles = function(container, options, cb) {
194
218
} ) ) ;
195
219
}
196
220
} ) ;
221
+
222
+ return cb . promise ;
197
223
} ;
198
224
199
225
/**
@@ -203,15 +229,20 @@ StorageService.prototype.getFiles = function(container, options, cb) {
203
229
* @callback {Function } cb Callback function
204
230
* @param {Object|String } err Error string or object
205
231
* @param {Object } file File metadata object
232
+ * @promise
206
233
*/
207
234
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 ) {
209
238
if ( err && err . code === 'ENOENT' ) {
210
239
err . statusCode = err . status = 404 ;
211
240
return cb ( err ) ;
212
241
}
213
242
return cb ( err , map ( f ) ) ;
214
243
} ) ;
244
+
245
+ return cb . promise ;
215
246
} ;
216
247
217
248
/**
@@ -220,9 +251,13 @@ StorageService.prototype.getFile = function(container, file, cb) {
220
251
* @param {String } file File name
221
252
* @callback {Function } cb Callback function
222
253
* @param {Object|String } err Error string or object
254
+ * @promise
223
255
*/
224
256
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 ;
226
261
} ;
227
262
228
263
/**
@@ -232,6 +267,7 @@ StorageService.prototype.removeFile = function(container, file, cb) {
232
267
* @param {Response } res Response object
233
268
* @param {Object } [options] Options for upload
234
269
* @param {Function } cb Callback function
270
+ * @promise
235
271
*/
236
272
StorageService . prototype . upload = function ( container , req , res , options , cb ) {
237
273
debug ( 'Configuring upload with options %o' , options ) ;
@@ -247,6 +283,9 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
247
283
cb = options ;
248
284
options = { } ;
249
285
}
286
+
287
+ cb = cb || utils . createPromiseCallback ( ) ;
288
+
250
289
if ( this . getFilename && ! options . getFilename ) {
251
290
options . getFilename = this . getFilename ;
252
291
}
@@ -269,7 +308,9 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
269
308
options . container = container ;
270
309
}
271
310
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 ;
273
314
} ;
274
315
275
316
/**
@@ -279,9 +320,13 @@ StorageService.prototype.upload = function(container, req, res, options, cb) {
279
320
* @param {Request } req HTTP request
280
321
* @param {Response } res HTTP response
281
322
* @param {Function } cb Callback function
323
+ * @promise
282
324
*/
283
325
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 ;
285
330
} ;
286
331
287
332
StorageService . modelName = 'storage' ;
0 commit comments