forked from benminer/cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (46 loc) · 1.49 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
import { api, storage } from '@serverless/cloud';
import Jimp from 'jimp';
import cors from 'cors';
const random = (length = 6) => Math.random().toString(20).substr(2, length);
api.use(
cors({
origin: 'http://localhost:3000',
}),
);
api.post('/', async (req, res) => {
const fileBuff = req.files[0].buffer;
if (!fileBuff) {
return res.status(400).send('No file provided');
}
try {
const imageId = random();
await storage.write(imageId, fileBuff);
return res.send({ url: `https://${req.hostname}/${imageId}` });
} catch (error) {
console.error(error);
return res.status(500).send(error.message);
}
});
api.get('/:imageId', async (req, res) => {
const imageId = req.params.imageId;
const width = parseInt(req.query.w);
const height = parseInt(req.query.h);
try {
if (width && height) {
const resizedImageId = `${imageId}-${width}-${height}`;
if (!(await storage.exists(resizedImageId))) {
const imageBuffer = await storage.readBuffer(imageId);
const image = await Jimp.read(imageBuffer);
image.resize(width, height);
const resizedImageBuffer = await image.getBufferAsync(Jimp.AUTO);
await storage.write(resizedImageId, resizedImageBuffer);
}
return await res.sendFile(resizedImageId);
}
return await res.sendFile(imageId);
} catch (error) {
console.error(error);
let status = error.message === 'NotFound' ? 400 : 500;
return res.status(status).send(error.message);
}
});