UNPKG

@opengis/fastify-table

Version:

core-plugins

95 lines (68 loc) 3.37 kB
import path from 'node:path'; import { imageSize } from 'image-size'; import { config, downloadFile, uploadFile, grpc, isFileExists, } from '../../../../utils.js'; import getMimeType from '../../../plugins/file/providers/mime/index.js'; const defaultWidth = 400; const defaultHeight = 240; const getHeight = (width, size, ratio = 1) => { if (size && size.toLowerCase().split('x')[1]) return size.toLowerCase().split('x')[1]; if (width) return width / ratio; return defaultHeight; }; const { resizeImage } = grpc(); /** * Апі використовується для зміни розміру фото за шляхом */ export default async function resize({ query = {}, unittest }, reply) { const { filepath, quality, size, w, h, nocache, maxWidth = 400, } = query; if (!filepath) { return reply.status(400).send('not enough query params: filepath'); } const basename = path.basename(filepath); const mimeType = getMimeType(filepath); const resizePath1 = size ? filepath.replace(basename, `${size}_resized_${basename}`) : filepath.replace(basename, `${w || defaultWidth}_${h || (w ? '' : defaultHeight)}_resized_${basename}`); // get Resize Data const fileExists = await isFileExists(resizePath1); const originalFileExists = await isFileExists(resizePath1.replace('files/', 'files/original/')); // resize-all API compatibility const resizePath = originalFileExists ? resizePath1.replace('files/', 'files/original/') : resizePath1; const resizeData = fileExists ? await downloadFile(resizePath, { buffer: true }) : null; if (resizeData && !config.disableCache && !nocache && !unittest) { return reply.headers({ 'Content-Type': mimeType, 'Cache-control': 'max-age=604800' }).send(resizeData); } // get File Data const fileData = await downloadFile(filepath, { buffer: true }); if (!fileData?.length) { return reply.status(404).send(`Файл не знайдено - ${filepath}`); } const resizeQuality = Math.min(quality || 75, 100); const { width = defaultWidth, height = defaultHeight } = imageSize(fileData) || {}; const ratio = width / height; const check = (size?.toLowerCase?.()?.split?.('x') || []).concat(w, h).filter(el => el && +el > maxWidth); if (check.length) { return reply.status(400).send({ message: 'resize image size too big' }); } const resizeWidth = (h && !w ? h * ratio : null) || (size?.toLowerCase?.()?.split?.('x')?.[1] && !size?.toLowerCase?.()?.split?.('x')?.[0] ? size.toLowerCase().split('x')[1] * ratio : null) || w || size?.toLowerCase?.()?.split?.('x')?.[0] || Math.min(width, maxWidth); const resizeHeight = h || getHeight(resizeWidth, size, ratio); if (config.trace) { console.log('original width/height/ratio: ', width, height, ratio); console.log('resize width/height/ratio/quality: ', resizeWidth, resizeHeight, resizeWidth / resizeHeight, resizeQuality); } const { result } = await resizeImage({ base64: Buffer.from(fileData).toString('base64'), width: resizeWidth, height: resizeHeight, quality: resizeQuality, }); await uploadFile(resizePath, Buffer.from(result, 'base64')); return reply.headers({ 'Content-Type': mimeType }).send(Buffer.from(result, 'base64')); }