forked from apostrophecms/uploadfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
108 lines (98 loc) · 2.99 KB
/
sample.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// An extremely simple app that accepts uploaded files
// and stores them in either a local folder or s3,
// depending on which backend you choose.
var express = require('express');
var uploadfs = require('./uploadfs.js')();
// For the local backend
var uploadsPath = __dirname + '/public/uploads';
var uploadsLocalUrl = '/uploads';
var options = {
backend: 'local',
uploadsPath: uploadsPath,
uploadsUrl: 'http://localhost:3000' + uploadsLocalUrl,
// Required if you use imageSizes and copyImageIn
tempPath: __dirname + '/temp',
imageSizes: [
{
name: 'small',
width: 320,
height: 320
},
{
name: 'medium',
width: 640,
height: 640
},
{
name: 'large',
width: 1140,
height: 1140
}
]
};
// Or use the S3 backend
// var options = {
// backend: 's3',
// // Get your credentials at aws.amazon.com
// secret: 'xxx',
// key: 'xxx',
// // You need to create your bucket first before using it here
// // Go to aws.amazon.com
// bucket: 'getyourownbucketplease',
// // I recommend creating your buckets in a region with
// // read-after-write consistency (not us-standard)
// region: 'us-west-2',
// // Required if you use imageSizes and copyImageIn
// tempPath: __dirname + '/temp',
// imageSizes: [
// {
// name: 'small',
// width: 320,
// height: 320
// },
// {
// name: 'medium',
// width: 640,
// height: 640
// },
// {
// name: 'large',
// width: 1140,
// height: 1140
// }
// ]
// };
uploadfs.init(options, createApp);
function createApp(err) {
if (err) {
console.log(err);
process.exit(1);
}
var app = express();
// For the local backend: serve the uploaded files at /uploads.
// With the s3 backend you don't need this of course, s3 serves
// the files for you.
app.use(uploadsLocalUrl, express.static(uploadsPath));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.get('/', function(req, res) {
res.send('<form method="POST" enctype="multipart/form-data">' +
'<input type="file" name="photo" /> <input type="submit" value="Upload Photo" />' +
'</form>');
});
app.post('/', function(req, res) {
uploadfs.copyImageIn(req.files.photo.path, '/profiles/me', function(e, info) {
if (e) {
res.send('An error occurred: ' + e);
} else {
res.send('<h1>All is well. Here is the image in three sizes plus the original.</h1>' +
'<div><img src="' + uploadfs.getUrl() + info.basePath + '.small.' + info.extension + '" /></div>' +
'<div><img src="' + uploadfs.getUrl() + info.basePath + '.medium.' + info.extension + '" /></div>' +
'<div><img src="' + uploadfs.getUrl() + info.basePath + '.large.' + info.extension + '" /></div>' +
'<div><img src="' + uploadfs.getUrl() + info.basePath + '.' + info.extension + '" /></div>');
}
});
});
app.listen(3000);
console.log('Listening at http://localhost:3000')
}