feat(server): add POST /api/admin/gallery/:id/resize endpoint
This commit is contained in:
@@ -52,6 +52,43 @@ export async function registerAdminGalleryRoutes(fastify) {
|
||||
},
|
||||
)
|
||||
|
||||
fastify.post(
|
||||
'/api/admin/gallery/:id/resize',
|
||||
{ preHandler: [fastify.verifyAdmin] },
|
||||
async (request, reply) => {
|
||||
const { id } = request.params
|
||||
const row = await prisma.galleryImage.findUnique({ where: { id } })
|
||||
if (!row) {
|
||||
return reply.code(404).send({ error: 'Изображение не найдено' })
|
||||
}
|
||||
if (row.isResized) {
|
||||
return reply.code(409).send({ error: 'Изображение уже обработано' })
|
||||
}
|
||||
|
||||
const urlParts = row.url.replace(/^\//, '').split('/')
|
||||
const fileName = urlParts[urlParts.length - 1]
|
||||
const uuid = path.parse(fileName).name
|
||||
|
||||
try {
|
||||
const { generateAllSizes, convertOriginalToWebp } = await import('../../lib/image-resize.js')
|
||||
|
||||
const fullPath = path.join(process.cwd(), urlParts.slice(0, -1).join('/'), fileName)
|
||||
await generateAllSizes(uuid, '', fullPath)
|
||||
const newUrl = await convertOriginalToWebp(uuid, '')
|
||||
|
||||
await prisma.galleryImage.update({
|
||||
where: { id },
|
||||
data: { url: newUrl, isResized: true },
|
||||
})
|
||||
|
||||
return { url: newUrl }
|
||||
} catch (error) {
|
||||
request.log.error(error, 'Resize failed')
|
||||
return reply.code(500).send({ error: 'Ошибка обработки изображения' })
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
fastify.delete(
|
||||
'/api/admin/gallery/:id',
|
||||
{ preHandler: [fastify.verifyAdmin] },
|
||||
|
||||
Reference in New Issue
Block a user