-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFileController.js
95 lines (80 loc) · 2.04 KB
/
FileController.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
/**
* FileController
*
* @description :: Server-side logic for managing files
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
/**
* `FileController.upload()`
*
* Upload file(s) to the server's disk.
*/
upload: function (req, res) {
// e.g.
// 0 => infinite
// 240000 => 4 minutes (240,000 miliseconds)
// etc.
//
// Node defaults to 2 minutes.
res.setTimeout(0);
req.file('avatar')
.upload({
// You can apply a file upload limit (in bytes)
maxBytes: 1000000
}, function whenDone(err, uploadedFiles) {
if (err) return res.serverError(err);
else return res.json({
files: uploadedFiles,
textParams: req.allParams()
});
});
},
/**
* `FileController.s3upload()`
*
* Upload file(s) to an S3 bucket.
*
* NOTE:
* If this is a really big file, you'll want to change
* the TCP connection timeout. This is demonstrated as the
* first line of the action below.
*/
s3upload: function (req, res) {
// e.g.
// 0 => infinite
// 240000 => 4 minutes (240,000 miliseconds)
// etc.
//
// Node defaults to 2 minutes.
res.setTimeout(0);
req.file('avatar').upload({
adapter: require('skipper-s3'),
bucket: process.env.BUCKET,
key: process.env.KEY,
secret: process.env.SECRET
}, function whenDone(err, uploadedFiles) {
if (err) return res.serverError(err);
else return res.json({
files: uploadedFiles,
textParams: req.allParams()
});
});
},
/**
* FileController.download()
*
* Download a file from the server's disk.
*/
download: function (req, res) {
var Path = require('path');
var fs = require('fs');
// If a relative path was provided, resolve it relative
// to the cwd (which is the top-level path of this sails app)
fs.createReadStream(Path.resolve(req.param('path')))
.on('error', function (err) {
return res.serverError(err);
})
.pipe(res);
}
};