|
1 |
| - |
2 | 1 | /*
|
3 | 2 | # CloudBoost - Core Engine that powers Bakend as a Service
|
4 |
| -# (c) 2014 HackerBay, Inc. |
| 3 | +# (c) 2014 HackerBay, Inc. |
5 | 4 | # CloudBoost may be freely distributed under the Apache 2 License
|
6 | 5 | */
|
7 | 6 |
|
8 |
| -module.exports = function() { |
| 7 | +module.exports = function() { |
9 | 8 |
|
10 | 9 | //get file from gridfs
|
11 |
| - global.app.get('/appfile/:appId/icon', function (req, res) { |
| 10 | + global.app.get('/appfile/:appId/icon', function(req, res) { |
12 | 11 |
|
13 | 12 | console.log("++++ Stream file from gridfs++++++");
|
14 | 13 |
|
15 | 14 | var appId = req.params.appId;
|
16 |
| - var fileName = appId; |
| 15 | + var fileName = appId; |
17 | 16 |
|
18 |
| - global.mongoService.document.getFile(appId,fileName).then(function(file){ |
| 17 | + global.mongoService.document.getFile(appId, fileName).then(function(file) { |
| 18 | + if (!file) |
| 19 | + res.send(); |
19 | 20 |
|
20 |
| - var fileStream=global.mongoService.document.getFileStreamById(appId,file._id); |
| 21 | + var fileStream = global.mongoService.document.getFileStreamById(appId, file._id); |
21 | 22 |
|
22 | 23 | res.set('Content-Type', file.contentType);
|
23 |
| - res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"'); |
| 24 | + res.set('Content-Disposition', 'attachment; filename="' + file.filename + '"'); |
| 25 | + |
| 26 | + fileStream.on("error", function(err) { |
| 27 | + res.send(500, "Got error while processing stream " + err.message); |
| 28 | + res.end(); |
| 29 | + }); |
24 | 30 |
|
25 |
| - fileStream.on("error", function(err) { |
26 |
| - res.send(500, "Got error while processing stream " + err.message); |
27 |
| - res.end(); |
28 |
| - }); |
29 |
| - |
30 | 31 | fileStream.on('end', function() {
|
31 |
| - res.end(); |
| 32 | + res.end(); |
32 | 33 | });
|
33 | 34 |
|
34 | 35 | fileStream.pipe(res);
|
35 | 36 |
|
36 |
| - },function(error){ |
| 37 | + }, function(error) { |
37 | 38 | return res.status(500).send(error);
|
38 |
| - }); |
| 39 | + }); |
39 | 40 |
|
40 | 41 | });
|
41 |
| -} |
42 |
| - |
| 42 | +} |
0 commit comments