This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (47 loc) · 1.56 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
const server = require('./server');
const fs = require('fs');
server.run().then( server => {
server.route({
method: 'GET',
path: '/',
handler: (req, h) => {
return h.file( 'index.html' );
}
});
server.route({
method: 'GET',
path: '/images/{any*}',
handler: (req, h) => {
const { params } = req;
return h.file( `uploads/${params.any}` );
}
});
server.route({
method: 'POST',
path: '/upload',
handler: (req, h) => {
const { payload } = req;
return new Promise( (res, rej) => {
if( payload.dataURL ) {
payload.file = payload.file.replace('data:image/png;base64,', '');
payload.file = Buffer.from( payload.file, 'base64');
fs.writeFile(`./public/uploads/${payload.filename}`, payload.file, 'base64', err => {
if( err ) {
console.log(err);
rej(err);
}
res({file: payload.filename});
});
} else {
fs.writeFile(`./public/uploads/${payload.filename}`, payload.file, err => {
if( err ) {
console.log(err);
rej(err);
}
res({file: payload.filename});
});
}
});
}
});
});