diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4f1562aafd671c56bc13822f2af9b0f98814cccf..87ecba956d4209df3b2e36af318f39d1b11a9ea7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -48,7 +48,8 @@ model Entity { text_position String? image_path String? @db.Text image_width Int? - image_height Int? + file_width Int? + file_height Int? entity_position String? entity_title_position String? image_scale String? @@ -82,7 +83,11 @@ model Image { entity_order Int image_path String? @db.Text image_width Int? - image_height Int? + file_width_initial Int + file_height_initial Int + image_url_initial String? + file_width Int? + file_height Int? image_scale String? title String? text String? @db.Text diff --git a/src/controllers/entitiesController.ts b/src/controllers/entitiesController.ts index b9ce478b5c84eaf0b67fda824ff7bdeab69da320..8811192567d287d0170d5c909e6b4bc4ee0de1b0 100644 --- a/src/controllers/entitiesController.ts +++ b/src/controllers/entitiesController.ts @@ -39,6 +39,13 @@ class EntitiesController { console.log(error); } } + async returnOriginalSizeImage(req: IWSRequest<'returnOriginalSizeImage', IEntity>) { + try { + return await EntitiesService.returnOriginalSizeImage(req.body); + } catch (error) { + console.log(error); + } + } async changeEntitiesOrder(req: IChangeEntitiesOrder) { try { return await EntitiesService.changeEntitiesOrder(req.body); diff --git a/src/helpers/index.ts b/src/helpers/index.ts index e57be7f87c954b7509c24e2b15a7416cd0dc3e39..f84ac4063682f3a9cd687c07047c08be162f3e5e 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -1,5 +1,6 @@ import { bot } from '../telegramBot'; import { PrismaClient } from '@prisma/client'; +import path from 'node:path'; export const validateMessage = async ( response: any, @@ -57,6 +58,27 @@ export const getPrismaEntity = async (body: any) => { // }); } }; +export const getImagePathByUuid = (entity_uuid: string, isOriginal?: boolean) => { + const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); + let newImagePath; + if (process.platform.includes('win')) { + newImagePath = imagePath.split('\\'); + } else { + newImagePath = imagePath.split('/'); + } + newImagePath.splice(-1); + if (isOriginal) { + newImagePath.push(`original${entity_uuid}.jpg`); + } else { + newImagePath.push(`${entity_uuid}.jpg`); + } + if (process.platform.includes('win')) { + newImagePath = newImagePath.join('\\'); + } else { + newImagePath = newImagePath.join('/'); + } + return newImagePath; +}; export const updatePrismaEntity = async (body: any) => { switch (body.entity_type) { case 'divider': @@ -74,7 +96,7 @@ export const updatePrismaEntity = async (body: any) => { data: { ...body } }); case 'image': - delete body.imageUrl; + delete body.image_url; return prisma.image.update({ where: { entity_uuid: body.entity_uuid diff --git a/src/interface/database.ts b/src/interface/database.ts index f3998981519ae45ca421f9776ef86ea93459f2cf..8ed34e7a60b7a8a52bb03ecd367b090745d5cdfc 100644 --- a/src/interface/database.ts +++ b/src/interface/database.ts @@ -16,7 +16,7 @@ export interface IEntity { text_position?: string | null; image_buffer?: string; image_path?: string; - imageUrl?: string; + image_url?: string; image_width?: number; image_height?: number; entity_position?: string; diff --git a/src/routes/websocket.ts b/src/routes/websocket.ts index 4842700ae234b5ccb7428eaef6224d576186b531..069564bb1407dbeb543c4803446eb0d586930aac 100644 --- a/src/routes/websocket.ts +++ b/src/routes/websocket.ts @@ -63,6 +63,12 @@ export const websocketRoute = async (req: any) => { submitToUsers('editEntity', editedEntity); break; } + case 'returnOriginalSizeImage': { + const result = await EntitiesController!.returnOriginalSizeImage(req); + submitFilesToUsers(result.buffer); + submitToUsers('returnOriginalSizeImage', result.entity); + break; + } case 'changeEntitiesOrder': const changedEntitiesOrders = await EntitiesController.changeEntitiesOrder(req); submitToUsers('changeEntitiesOrder', changedEntitiesOrders); diff --git a/src/services/entitiesService.ts b/src/services/entitiesService.ts index 57ff139c8441bfc50a0870ec818eecf49dc1c793..d41c2ca925f112966f74203ef5c197fa42e13e3e 100644 --- a/src/services/entitiesService.ts +++ b/src/services/entitiesService.ts @@ -1,15 +1,17 @@ import { PrismaClient } from '@prisma/client'; import * as fs from 'node:fs'; import path from 'node:path'; -import { IEntity, IPageEntity } from '../interface/database'; +import { IEntity } from '../interface/database'; import { randomUUID } from 'node:crypto'; import { createPrismaEntity, - updatePrismaEntity, + deletePrismaEntity, + getImagePathByUuid, getPrismaEntity, - deletePrismaEntity + updatePrismaEntity } from '../helpers'; import PagesService from './pagesService'; + const prisma = new PrismaClient(); class EntitiesService { @@ -17,23 +19,14 @@ class EntitiesService { if (!body.entity_uuid) body.entity_uuid = randomUUID(); if (body?.image_buffer) { const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); - let newImagePath; - if (process.platform.includes('win')) { - newImagePath = imagePath.split('\\'); - } else { - newImagePath = imagePath.split('/'); - } - newImagePath.splice(-1); - console.log('body in createEntity: ', body, 'body.entity_uuid: ', body.entity_uuid); - newImagePath.push(`${body.entity_uuid}.jpg`); - if (process.platform.includes('win')) { - newImagePath = newImagePath.join('\\'); - } else { - newImagePath = newImagePath.join('/'); - } + const originalImagePath = path.join(path.resolve(), `/public/images/originalImage.jpg`); + let newImagePath = getImagePathByUuid(body.entity_uuid); fs.rename(imagePath, newImagePath, function (err) { if (err) console.log('ERROR in fs.rename: ' + err); }); + fs.rename(originalImagePath, `original${imagePath}`, function (err) { + if (err) console.log('ERROR in fs.rename: ' + err); + }); delete body.image_buffer; body.image_path = newImagePath; } @@ -45,6 +38,8 @@ class EntitiesService { } // единÑÑ‚Ð²ÐµÐ½Ð½Ð°Ñ Ñ„ÑƒÐ½ÐºÑ†Ð¸Ñ, ÑÑ€Ð°Ð±Ð°Ñ‚Ñ‹Ð²Ð°ÑŽÑ‰Ð°Ñ Ð¿Ð¾ Ñокету Ð´Ð»Ñ Ñ„Ð°Ð¹Ð»Ð¾Ð² async createImage(body: Buffer) { + const originalImagePath = path.join(path.resolve(), `/public/images/originalImage.jpg`); + fs.writeFileSync(originalImagePath, body); const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); fs.writeFileSync(imagePath, body); } @@ -87,7 +82,7 @@ class EntitiesService { fs.rename(imagePath, body.image_path!, function (err) { if (err) throw err; }); - delete body.imageUrl; + delete body.image_url; return prisma.image.update({ where: { entity_uuid: body.entity_uuid @@ -95,6 +90,18 @@ class EntitiesService { data: { ...body } }); } + async returnOriginalSizeImage(body: IEntity) { + body.image_path = getImagePathByUuid(body.entity_uuid, true); + const newState = await prisma.image.update({ + where: { + entity_uuid: body.entity_uuid + }, + data: { ...body } + }); + const file = fs.readFileSync(body.image_path); + const buffer = Buffer.from(file); + return { buffer: [buffer], entity: newState }; + } async editEntity(body: IEntity) { return updatePrismaEntity(body); }