Skip to content
Snippets Groups Projects
Commit 9125b327 authored by Дмитрий Малюгин's avatar Дмитрий Малюгин :clock4:
Browse files

finished to change image architecture

parent 82edf1fb
Branches refactor/imageProperties
No related tags found
1 merge request!6Refactor image entity
......@@ -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?
......
......@@ -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);
}
......
......@@ -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;
}
});
});
......
......@@ -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' }]
// ]
// }
// });
// };
......@@ -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': {
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment