-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
72 lines (66 loc) · 2.03 KB
/
index.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
const Boom = require('boom');
const Joi = require('joi');
const s3put = require('s3put');
const aug = require('aug');
const defaultOptions = {
endpoint: '/upload', // endpoint where images should be POSTed
contentTypes: [], // list of mime-types the endpoint accepts
bucket: false,
profile: false,
quality: 100, // images aren't compressed by default
maxUploadSize: 1 // in mb
};
const register = async(server, options) => {
options = aug({}, defaultOptions, options);
if (!options.profile) {
if (!options.access_key && !options.secret_key) {
throw new Error('You must specify either a profile or an access/secret key to use AWS.');
}
}
if (!options.bucket) {
throw new Error('You must specify a bucket name that you want to upload to.');
}
// one route with query params for resize/crop
server.route({
config: {
payload: {
output: 'stream',
maxBytes: options.maxUploadSize * (1024 * 1024) // convert to bytes for hapi
},
validate: {
payload: {
file: Joi.any().required()
}
},
},
path: options.endpoint,
method: 'POST',
handler: async(request, h) => {
// make sure payload is palatable to s3put:
const file = request.payload.file;
if (!file.hapi.filename) {
throw Boom.badData('must be a file');
}
if (options.contentTypes.length && options.contentTypes.indexOf(file.hapi.headers['content-type']) === -1) {
throw Boom.unsupportedMediaType('content-type not allowed');
}
file.path = file.hapi.filename;
// call s3put to handle the upload:
const response = await s3put(file, options);
return response;
}
});
// also expose a plugin method on hapi server:
server.decorate('server', 'uploadToS3', (file, localOptions) => {
if (!localOptions) {
localOptions = {};
}
localOptions = aug({}, options, localOptions);
return s3put(file, localOptions);
});
};
exports.plugin = {
register,
once: true,
pkg: require('./package.json')
};