Skip to content

Commit

Permalink
feat: ability to resize source images with query string
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Sep 16, 2021
1 parent 51629d3 commit c2ea600
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions api/src/controllers/storage.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const sharp = require('sharp');
const { promisify } = require('util');
const sizeOf = promisify(require('image-size'));
const { createCanvas, loadImage, registerFont } = require('canvas');
Expand Down Expand Up @@ -82,7 +83,10 @@ module.exports.matches = async (req, res) => {
return res.end(buffer);
}

const buffer = fs.readFileSync(source);
const buffer =
req.query.thumb === ''
? await sharp(source).jpeg({ quality: 75 }).resize(400).withMetadata().toBuffer()
: fs.readFileSync(source);
res.set('Content-Type', 'image/jpeg');
return res.end(buffer);
};
Expand All @@ -91,11 +95,14 @@ module.exports.train = async (req, res) => {
const { name, filename } = req.params;
const source = `${PATH}/train/${name}/${filename}`;

if (!fs.existsSync(source)) {
return res.status(BAD_REQUEST).error(`${source} does not exist`);
}
if (!fs.existsSync(source)) return res.status(BAD_REQUEST).error(`${source} does not exist`);

const buffer =
req.query.thumb === ''
? await sharp(source).jpeg({ quality: 75 }).resize(400).withMetadata().toBuffer()
: fs.readFileSync(source);
res.set('Content-Type', 'image/jpeg');

const buffer = fs.readFileSync(source);
res.set('Content-Type', 'image/jpeg');
return res.end(buffer);
};
Expand Down

0 comments on commit c2ea600

Please sign in to comment.