This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/routes/images.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-01-06 06:21:20 -08:00
import { Router } from 'express';
import {ulid} from "ulid";
import multer from 'multer';
import {loadImage, createCanvas} from 'canvas';
import {handleErrorAsync} from "../../src/helpers";
2021-01-06 06:21:20 -08:00
import awsConfig from '../aws';
import S3 from 'aws-sdk/clients/s3';
const sizes = {
big: [1600, false],
2021-04-05 08:03:13 -07:00
flag: [256, false],
2021-01-06 06:21:20 -08:00
thumb: [240, true],
}
const resizeImage = (image, width, height, sx = null, sy = null) => {
const canvas = createCanvas(width, height);
if (sx === null) {
canvas.getContext('2d').drawImage(image, 0, 0, width, height);
} else {
canvas.getContext('2d').drawImage(image, sx, sy, width, height, 0, 0, width, height);
}
return canvas;
}
const cropToSquare = (image) => {
return image.width > image.height
? resizeImage(image, image.height, image.height, (image.width - image.height) / 2, 0)
: resizeImage(image, image.width, image.width, 0, (image.height - image.width) / 2);
}
const scaleDownTo = (image, size) => {
if (image.width > image.height) {
return image.width > size
? resizeImage(image, size, image.height * size / image.width)
: image;
}
return image.height > size
? resizeImage(image, image.width * size / image.height, size)
: image;
}
const router = Router();
router.post('/images/upload', multer({limits: {fileSize: 10 * 1024 * 1024}}).any('images[]', 12), handleErrorAsync(async (req, res) => {
2021-01-06 06:21:20 -08:00
const s3 = new S3(awsConfig);
const ids = [];
for (let file of req.files) {
const id = ulid();
const image = await loadImage(file.buffer);
for (let s in sizes) {
if (!sizes.hasOwnProperty(s)) { continue; }
const [size, square] = sizes[s];
let canvas = createCanvas(image.width, image.height);
canvas.getContext('2d').drawImage(image, 0, 0);
if (square) {
canvas = cropToSquare(canvas);
}
canvas = scaleDownTo(canvas, size);
await s3.putObject({
Key: `images/${id}-${s}.png`,
Body: canvas.toBuffer('image/png'),
ContentType: 'image/png',
ACL: 'public-read',
}).promise();
}
ids.push(id);
}
return res.json(ids);
}));
2021-01-06 06:21:20 -08:00
export default router;