1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | var factory = require('./factory');
|
8 | var handler = require('./storage-handler');
|
9 |
|
10 | var storage = require('pkgcloud').storage;
|
11 |
|
12 | module.exports = StorageService;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function StorageService(options) {
|
31 | if (!(this instanceof StorageService)) {
|
32 | return new StorageService(options);
|
33 | }
|
34 | this.provider = options.provider;
|
35 | this.client = factory.createClient(options);
|
36 |
|
37 | if ('function' === typeof options.getFilename) {
|
38 | this.getFilename = options.getFilename;
|
39 | }
|
40 | if (options.acl) {
|
41 | this.acl = options.acl;
|
42 | }
|
43 | if (options.allowedContentTypes) {
|
44 | this.allowedContentTypes = options.allowedContentTypes;
|
45 | }
|
46 | if (options.maxFileSize) {
|
47 | this.maxFileSize = options.maxFileSize;
|
48 | }
|
49 | }
|
50 |
|
51 | function map(obj) {
|
52 | return obj;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | StorageService.prototype.getContainers = function(cb) {
|
62 | this.client.getContainers(function(err, containers) {
|
63 | if (err) {
|
64 | cb(err, containers);
|
65 | } else {
|
66 | cb(err, containers.map(function(c) {
|
67 | return map(c);
|
68 | }));
|
69 | }
|
70 | });
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | StorageService.prototype.createContainer = function(options, cb) {
|
84 | options = options || {};
|
85 | if ('object' === typeof options && !(options instanceof storage.Container)) {
|
86 | options.Name = options.name;
|
87 | var Container = factory.getProvider(this.provider).storage.Container;
|
88 | options = new Container(this.client, options);
|
89 | }
|
90 | return this.client.createContainer(options, function(err, container) {
|
91 | return cb(err, map(container));
|
92 | });
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | StorageService.prototype.destroyContainer = function(container, cb) {
|
102 | return this.client.destroyContainer(container, cb);
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | StorageService.prototype.getContainer = function(container, cb) {
|
113 | return this.client.getContainer(container, function(err, container) {
|
114 | if (err && err.code === 'ENOENT') {
|
115 | err.statusCode = err.status = 404;
|
116 | return cb(err);
|
117 | }
|
118 | return cb(err, map(container));
|
119 | });
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | StorageService.prototype.uploadStream = function(container, file, options) {
|
132 | if (typeof options === 'function') {
|
133 | options = {};
|
134 | }
|
135 | options = options || {};
|
136 | if (container) {
|
137 | options.container = container;
|
138 | }
|
139 | if (file) {
|
140 | options.remote = file;
|
141 | }
|
142 |
|
143 | return this.client.upload(options);
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | StorageService.prototype.downloadStream = function(container, file, options) {
|
156 | if (typeof options === 'function') {
|
157 | options = {};
|
158 | }
|
159 | options = options || {};
|
160 | if (container) {
|
161 | options.container = container;
|
162 | }
|
163 | if (file) {
|
164 | options.remote = file;
|
165 | }
|
166 |
|
167 | return this.client.download(options);
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | StorageService.prototype.getFiles = function(container, options, cb) {
|
179 | if (typeof options === 'function' && !cb) {
|
180 |
|
181 | cb = options;
|
182 | options = {};
|
183 | }
|
184 | return this.client.getFiles(container, options, function(err, files) {
|
185 | if (err) {
|
186 | cb(err, files);
|
187 | } else {
|
188 | cb(err, files.map(function(f) {
|
189 | return map(f);
|
190 | }));
|
191 | }
|
192 | });
|
193 | };
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | StorageService.prototype.getFile = function(container, file, cb) {
|
204 | return this.client.getFile(container, file, function(err, f) {
|
205 | return cb(err, map(f));
|
206 | });
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | StorageService.prototype.removeFile = function(container, file, cb) {
|
217 | return this.client.removeFile(container, file, cb);
|
218 | };
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | StorageService.prototype.upload = function(req, res, options, cb) {
|
228 | if (!cb && 'function' === typeof options) {
|
229 | cb = options;
|
230 | options = {};
|
231 | }
|
232 | if (this.getFilename && !options.getFilename) {
|
233 | options.getFilename = this.getFilename;
|
234 | }
|
235 | if (this.acl && !options.acl) {
|
236 | options.acl = this.acl;
|
237 | }
|
238 | if (this.allowedContentTypes && !options.allowedContentTypes) {
|
239 | options.allowedContentTypes = this.allowedContentTypes;
|
240 | }
|
241 | if (this.maxFileSize && !options.maxFileSize) {
|
242 | options.maxFileSize = this.maxFileSize;
|
243 | }
|
244 | return handler.upload(this.client, req, res, options, cb);
|
245 | };
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | StorageService.prototype.download = function(container, file, req, res, cb) {
|
256 | return handler.download(this.client, req, res, container, file, cb);
|
257 | };
|
258 |
|
259 | StorageService.modelName = 'storage';
|
260 |
|
261 | StorageService.prototype.getContainers.shared = true;
|
262 | StorageService.prototype.getContainers.accepts = [];
|
263 | StorageService.prototype.getContainers.returns = {
|
264 | arg: 'containers',
|
265 | type: 'array',
|
266 | root: true,
|
267 | };
|
268 | StorageService.prototype.getContainers.http =
|
269 | {verb: 'get', path: '/'};
|
270 |
|
271 | StorageService.prototype.getContainer.shared = true;
|
272 | StorageService.prototype.getContainer.accepts = [
|
273 | {arg: 'container', type: 'string'},
|
274 | ];
|
275 | StorageService.prototype.getContainer.returns = {
|
276 | arg: 'container',
|
277 | type: 'object', root: true,
|
278 | };
|
279 | StorageService.prototype.getContainer.http =
|
280 | {verb: 'get', path: '/:container'};
|
281 |
|
282 | StorageService.prototype.createContainer.shared = true;
|
283 | StorageService.prototype.createContainer.accepts = [
|
284 | {arg: 'options', type: 'object', http: {source: 'body'}},
|
285 | ];
|
286 | StorageService.prototype.createContainer.returns = {
|
287 | arg: 'container',
|
288 | type: 'object', root: true,
|
289 | };
|
290 | StorageService.prototype.createContainer.http =
|
291 | {verb: 'post', path: '/'};
|
292 |
|
293 | StorageService.prototype.destroyContainer.shared = true;
|
294 | StorageService.prototype.destroyContainer.accepts = [
|
295 | {arg: 'container', type: 'string'},
|
296 | ];
|
297 | StorageService.prototype.destroyContainer.returns = {};
|
298 | StorageService.prototype.destroyContainer.http =
|
299 | {verb: 'delete', path: '/:container'};
|
300 |
|
301 | StorageService.prototype.getFiles.shared = true;
|
302 | StorageService.prototype.getFiles.accepts = [
|
303 | {arg: 'container', type: 'string'},
|
304 | ];
|
305 | StorageService.prototype.getFiles.returns = {arg: 'files', type: 'array', root: true};
|
306 | StorageService.prototype.getFiles.http =
|
307 | {verb: 'get', path: '/:container/files'};
|
308 |
|
309 | StorageService.prototype.getFile.shared = true;
|
310 | StorageService.prototype.getFile.accepts = [
|
311 | {arg: 'container', type: 'string'},
|
312 | {arg: 'file', type: 'string'},
|
313 | ];
|
314 | StorageService.prototype.getFile.returns = {arg: 'file', type: 'object', root: true};
|
315 | StorageService.prototype.getFile.http =
|
316 | {verb: 'get', path: '/:container/files/:file'};
|
317 |
|
318 | StorageService.prototype.removeFile.shared = true;
|
319 | StorageService.prototype.removeFile.accepts = [
|
320 | {arg: 'container', type: 'string'},
|
321 | {arg: 'file', type: 'string'},
|
322 | ];
|
323 | StorageService.prototype.removeFile.returns = {};
|
324 | StorageService.prototype.removeFile.http =
|
325 | {verb: 'delete', path: '/:container/files/:file'};
|
326 |
|
327 | StorageService.prototype.upload.shared = true;
|
328 | StorageService.prototype.upload.accepts = [
|
329 | {arg: 'req', type: 'object', 'http': {source: 'req'}},
|
330 | {arg: 'res', type: 'object', 'http': {source: 'res'}},
|
331 | ];
|
332 | StorageService.prototype.upload.returns = {arg: 'result', type: 'object'};
|
333 | StorageService.prototype.upload.http =
|
334 | {verb: 'post', path: '/:container/upload'};
|
335 |
|
336 | StorageService.prototype.download.shared = true;
|
337 | StorageService.prototype.download.accepts = [
|
338 | {arg: 'container', type: 'string', 'http': {source: 'path'}},
|
339 | {arg: 'file', type: 'string', 'http': {source: 'path'}},
|
340 | {arg: 'req', type: 'object', 'http': {source: 'req'}},
|
341 | {arg: 'res', type: 'object', 'http': {source: 'res'}},
|
342 | ];
|
343 | StorageService.prototype.download.http =
|
344 | {verb: 'get', path: '/:container/download/:file'};
|