diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4f1562aafd671c56bc13822f2af9b0f98814cccf..36db60f2a5cb2f55d69aa17418f547f6a7d6284f 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? @@ -81,8 +82,13 @@ model Image { entity_uuid String @id @default(uuid()) entity_order Int image_path String? @db.Text + image_url_initial String? + image_width_initial Int + file_width_initial Int + file_height_initial Int image_width Int? - image_height Int? + 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..7efa4691fa176edfa3c58242d163e41272ef7d0a 100644 --- a/src/controllers/entitiesController.ts +++ b/src/controllers/entitiesController.ts @@ -10,9 +10,9 @@ class EntitiesController { console.log(error); } } - async createImage(req: Buffer) { + async createImage(req: Buffer, isCropImageNow: boolean) { try { - return await EntitiesService.createImage(req); + return await EntitiesService.createImage(req, isCropImageNow); } catch (error) { console.log(error); } @@ -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/index.ts b/src/index.ts index 430964ca62879f4f4ff855ba6526228249ad1d7a..4e4b2fdc8ad926b7d503625d9c28f31cc1599d29 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import { connectBot } from './telegramBot'; await connectBot(); const users = new Set(); +let isCropImageNow: boolean = false; const PORT = Number(process.env.PORT) || 5000; const FILES_PORT = process.env.FILES_PORT || 5001; @@ -22,6 +23,7 @@ wss.on('connection', (ws) => { ws.id = Date.now(); ws.on('message', async (req) => { req = JSON.parse(req); + if (req.event === 'setCropNow') isCropImageNow = true; await websocketRoute(req); }); }); @@ -36,8 +38,12 @@ filesWss.on('connection', (ws) => { users.add(ws); console.log('users wss: ', users.size); ws.on('message', async (req: Buffer) => { - await EntitiesController.createImage(req); - submitToUsers('createImageEntity', ''); + await EntitiesController.createImage(req, isCropImageNow); + if (!isCropImageNow) { + submitToUsers('createImageEntity', ''); + } else { + isCropImageNow = false; + } }); }); 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/telegramBot.ts b/src/routes/telegramBot.ts index 3d012de9472cff93a18194b6d8b006cc4e6cb0fa..3c94254697a30b5c9861d1062e5dfc900408a47d 100644 --- a/src/routes/telegramBot.ts +++ b/src/routes/telegramBot.ts @@ -223,25 +223,3 @@ export const onSuggestionResponse = async (chatId, suggestion) => { fs.writeFileSync(imagePath, suggestion); await sendMessageOnStart(chatId); }; -// export const sendMessageOn = async (chatId) => { -// await bot.sendMessage(chatId, 'Что Вас интересует?', { -// reply_markup: { -// inline_keyboard: [ -// [{ text: 'Параграфы', callback_data: '/textInfo' }], -// [{ text: 'Изображения', callback_data: '/imageInfo' }], -// [{ text: 'Назад', callback_data: '/start' }] -// ] -// } -// }); -// }; -// export const sendMessageOn = async (chatId) => { -// await bot.sendMessage(chatId, 'Что Вас интересует?', { -// reply_markup: { -// inline_keyboard: [ -// [{ text: 'Параграфы', callback_data: '/textInfo' }], -// [{ text: 'Изображения', callback_data: '/imageInfo' }], -// [{ text: 'Назад', callback_data: '/start' }] -// ] -// } -// }); -// }; diff --git a/src/routes/websocket.ts b/src/routes/websocket.ts index 4842700ae234b5ccb7428eaef6224d576186b531..c2fba9b01206786ea2da5c4200bf7446c8dd1367 100644 --- a/src/routes/websocket.ts +++ b/src/routes/websocket.ts @@ -59,8 +59,13 @@ export const websocketRoute = async (req: any) => { break; } case 'cropImage': { - const editedEntity = await EntitiesController.cropImage(req); - submitToUsers('editEntity', editedEntity); + await EntitiesController.cropImage(req); + break; + } + case 'returnOriginalSizeImage': { + const result = await EntitiesController!.returnOriginalSizeImage(req); + submitFilesToUsers(result.buffer); + submitToUsers('returnOriginalSizeImage', result.entity); break; } case 'changeEntitiesOrder': diff --git a/src/services/entitiesService.ts b/src/services/entitiesService.ts index 57ff139c8441bfc50a0870ec818eecf49dc1c793..2f53357310b2c325a4e24262680cc6db5506994c 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,26 +19,20 @@ 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`); + const newImagePath = getImagePathByUuid(body.entity_uuid); + const newOriginalImagePath = getImagePathByUuid(body.entity_uuid, true); + fs.rename(imagePath, newImagePath, function (err) { if (err) console.log('ERROR in fs.rename: ' + err); }); + fs.rename(originalImagePath, newOriginalImagePath, function (err) { + if (err) console.log('ERROR in fs.rename: ' + err); + }); delete body.image_buffer; body.image_path = newImagePath; } + const page_uuid = body.page_uuid!; delete body.page_uuid; const createdEntity = createPrismaEntity(body); @@ -44,7 +40,11 @@ class EntitiesService { return createdEntity; } // единственная функция, срабатывающая по сокету для файлов - async createImage(body: Buffer) { + async createImage(body: Buffer, isCropImageNow: boolean) { + if (!isCropImageNow) { + 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); } @@ -68,6 +68,13 @@ class EntitiesService { const file = fs.readFileSync(imagePath); const buffer = Buffer.from(file); entitiesImages.push(buffer); + const originalImagePath = path.join( + path.resolve(), + `/public/images/original${entity.entity_uuid}.jpg` + ); + const originalFile = fs.readFileSync(originalImagePath); + const originalBuffer = Buffer.from(originalFile); + entitiesImages.push(originalBuffer); }); return { entities: entitiesToReturn, @@ -81,19 +88,32 @@ class EntitiesService { } async cropImage(body: IEntity) { const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); - fs.unlink(body.image_path!, (err) => { - if (err) throw err; - }); - fs.rename(imagePath, body.image_path!, function (err) { - if (err) throw err; - }); - delete body.imageUrl; - return prisma.image.update({ + const timer = setInterval(() => { + if (fs.existsSync(imagePath)) { + clearInterval(timer); + fs.unlinkSync(body.image_path!); + fs.renameSync(imagePath, body.image_path!); + delete body.image_url; + return prisma.image.update({ + where: { + entity_uuid: body.entity_uuid + }, + data: { ...body } + }); + } + }, 50); + } + 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); @@ -113,10 +133,17 @@ class EntitiesService { } async deleteEntity(body: IEntity) { await deletePrismaEntity(body); - if (body.image_path) + if (body.image_path) { fs.unlink(body.image_path, (err) => { if (err) throw err; }); + const originalImagePath = getImagePathByUuid(body.entity_uuid, true); + if (fs.existsSync(originalImagePath)) { + fs.unlink(originalImagePath, (err) => { + if (err) throw err; + }); + } + } await PagesService.deletePageEntity(body.page_uuid, body.entity_uuid); return { entity_uuid: body.entity_uuid