diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 87ecba956d4209df3b2e36af318f39d1b11a9ea7..36db60f2a5cb2f55d69aa17418f547f6a7d6284f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -82,10 +82,11 @@ model Image { entity_uuid String @id @default(uuid()) entity_order Int image_path String? @db.Text - image_width Int? + image_url_initial String? + image_width_initial Int file_width_initial Int file_height_initial Int - image_url_initial String? + image_width Int? file_width Int? file_height Int? image_scale String? diff --git a/src/controllers/entitiesController.ts b/src/controllers/entitiesController.ts index 8811192567d287d0170d5c909e6b4bc4ee0de1b0..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); } 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/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 069564bb1407dbeb543c4803446eb0d586930aac..c2fba9b01206786ea2da5c4200bf7446c8dd1367 100644 --- a/src/routes/websocket.ts +++ b/src/routes/websocket.ts @@ -59,8 +59,7 @@ 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': { diff --git a/src/services/entitiesService.ts b/src/services/entitiesService.ts index d41c2ca925f112966f74203ef5c197fa42e13e3e..2f53357310b2c325a4e24262680cc6db5506994c 100644 --- a/src/services/entitiesService.ts +++ b/src/services/entitiesService.ts @@ -20,16 +20,19 @@ class EntitiesService { if (body?.image_buffer) { const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); const originalImagePath = path.join(path.resolve(), `/public/images/originalImage.jpg`); - let newImagePath = getImagePathByUuid(body.entity_uuid); + 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, `original${imagePath}`, function (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); @@ -37,9 +40,11 @@ class EntitiesService { return createdEntity; } // единÑÑ‚Ð²ÐµÐ½Ð½Ð°Ñ Ñ„ÑƒÐ½ÐºÑ†Ð¸Ñ, ÑÑ€Ð°Ð±Ð°Ñ‚Ñ‹Ð²Ð°ÑŽÑ‰Ð°Ñ Ð¿Ð¾ Ñокету Ð´Ð»Ñ Ñ„Ð°Ð¹Ð»Ð¾Ð² - async createImage(body: Buffer) { - const originalImagePath = path.join(path.resolve(), `/public/images/originalImage.jpg`); - fs.writeFileSync(originalImagePath, body); + 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); } @@ -63,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, @@ -76,19 +88,20 @@ 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.image_url; - return prisma.image.update({ - where: { - entity_uuid: body.entity_uuid - }, - data: { ...body } - }); + 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); @@ -120,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