Genesis
This commit is contained in:
21
src/endpoints/image-handler.js
Normal file
21
src/endpoints/image-handler.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { sendBinary, sendJson } from '../http.js';
|
||||
import { resizeImage } from '../image-resize.js';
|
||||
import { getImageByHash } from '../queries/image.js';
|
||||
|
||||
export function createImageHandler() {
|
||||
return async function handle(_req, res, { url }) {
|
||||
const path = url.searchParams.get('path');
|
||||
|
||||
if (!path) {
|
||||
return sendJson(res, 400, { Message: "Missing required query parameter 'path'." });
|
||||
}
|
||||
|
||||
const image = await getImageByHash(path, '200');
|
||||
if (!image) {
|
||||
return sendJson(res, 404, { Message: `No image was found for path '${path}'.` });
|
||||
}
|
||||
|
||||
const buffer = await resizeImage(image.buffer);
|
||||
return sendBinary(res, 200, buffer, image.contentType);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user