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

change image settings in process

parent e1717c7c
No related branches found
No related tags found
1 merge request!6Refactor image entity
...@@ -48,7 +48,8 @@ model Entity { ...@@ -48,7 +48,8 @@ model Entity {
text_position String? text_position String?
image_path String? @db.Text image_path String? @db.Text
image_width Int? image_width Int?
image_height Int? file_width Int?
file_height Int?
entity_position String? entity_position String?
entity_title_position String? entity_title_position String?
image_scale String? image_scale String?
...@@ -82,7 +83,11 @@ model Image { ...@@ -82,7 +83,11 @@ model Image {
entity_order Int entity_order Int
image_path String? @db.Text image_path String? @db.Text
image_width Int? 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? image_scale String?
title String? title String?
text String? @db.Text text String? @db.Text
......
...@@ -39,6 +39,13 @@ class EntitiesController { ...@@ -39,6 +39,13 @@ class EntitiesController {
console.log(error); 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) { async changeEntitiesOrder(req: IChangeEntitiesOrder) {
try { try {
return await EntitiesService.changeEntitiesOrder(req.body); return await EntitiesService.changeEntitiesOrder(req.body);
......
import { bot } from '../telegramBot'; import { bot } from '../telegramBot';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import path from 'node:path';
export const validateMessage = async ( export const validateMessage = async (
response: any, response: any,
...@@ -57,6 +58,27 @@ export const getPrismaEntity = async (body: 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) => { export const updatePrismaEntity = async (body: any) => {
switch (body.entity_type) { switch (body.entity_type) {
case 'divider': case 'divider':
...@@ -74,7 +96,7 @@ export const updatePrismaEntity = async (body: any) => { ...@@ -74,7 +96,7 @@ export const updatePrismaEntity = async (body: any) => {
data: { ...body } data: { ...body }
}); });
case 'image': case 'image':
delete body.imageUrl; delete body.image_url;
return prisma.image.update({ return prisma.image.update({
where: { where: {
entity_uuid: body.entity_uuid entity_uuid: body.entity_uuid
......
...@@ -16,7 +16,7 @@ export interface IEntity { ...@@ -16,7 +16,7 @@ export interface IEntity {
text_position?: string | null; text_position?: string | null;
image_buffer?: string; image_buffer?: string;
image_path?: string; image_path?: string;
imageUrl?: string; image_url?: string;
image_width?: number; image_width?: number;
image_height?: number; image_height?: number;
entity_position?: string; entity_position?: string;
......
...@@ -63,6 +63,12 @@ export const websocketRoute = async (req: any) => { ...@@ -63,6 +63,12 @@ export const websocketRoute = async (req: any) => {
submitToUsers('editEntity', editedEntity); submitToUsers('editEntity', editedEntity);
break; break;
} }
case 'returnOriginalSizeImage': {
const result = await EntitiesController!.returnOriginalSizeImage(req);
submitFilesToUsers(result.buffer);
submitToUsers('returnOriginalSizeImage', result.entity);
break;
}
case 'changeEntitiesOrder': case 'changeEntitiesOrder':
const changedEntitiesOrders = await EntitiesController.changeEntitiesOrder(req); const changedEntitiesOrders = await EntitiesController.changeEntitiesOrder(req);
submitToUsers('changeEntitiesOrder', changedEntitiesOrders); submitToUsers('changeEntitiesOrder', changedEntitiesOrders);
......
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import * as fs from 'node:fs'; import * as fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { IEntity, IPageEntity } from '../interface/database'; import { IEntity } from '../interface/database';
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import { import {
createPrismaEntity, createPrismaEntity,
updatePrismaEntity, deletePrismaEntity,
getImagePathByUuid,
getPrismaEntity, getPrismaEntity,
deletePrismaEntity updatePrismaEntity
} from '../helpers'; } from '../helpers';
import PagesService from './pagesService'; import PagesService from './pagesService';
const prisma = new PrismaClient(); const prisma = new PrismaClient();
class EntitiesService { class EntitiesService {
...@@ -17,23 +19,14 @@ class EntitiesService { ...@@ -17,23 +19,14 @@ class EntitiesService {
if (!body.entity_uuid) body.entity_uuid = randomUUID(); if (!body.entity_uuid) body.entity_uuid = randomUUID();
if (body?.image_buffer) { if (body?.image_buffer) {
const imagePath = path.join(path.resolve(), `/public/images/image.jpg`); const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
let newImagePath; const originalImagePath = path.join(path.resolve(), `/public/images/originalImage.jpg`);
if (process.platform.includes('win')) { let newImagePath = getImagePathByUuid(body.entity_uuid);
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('/');
}
fs.rename(imagePath, newImagePath, function (err) { fs.rename(imagePath, newImagePath, function (err) {
if (err) console.log('ERROR in fs.rename: ' + 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; delete body.image_buffer;
body.image_path = newImagePath; body.image_path = newImagePath;
} }
...@@ -45,6 +38,8 @@ class EntitiesService { ...@@ -45,6 +38,8 @@ class EntitiesService {
} }
// единственная функция, срабатывающая по сокету для файлов // единственная функция, срабатывающая по сокету для файлов
async createImage(body: Buffer) { 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`); const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
fs.writeFileSync(imagePath, body); fs.writeFileSync(imagePath, body);
} }
...@@ -87,7 +82,7 @@ class EntitiesService { ...@@ -87,7 +82,7 @@ class EntitiesService {
fs.rename(imagePath, body.image_path!, function (err) { fs.rename(imagePath, body.image_path!, function (err) {
if (err) throw err; if (err) throw err;
}); });
delete body.imageUrl; delete body.image_url;
return prisma.image.update({ return prisma.image.update({
where: { where: {
entity_uuid: body.entity_uuid entity_uuid: body.entity_uuid
...@@ -95,6 +90,18 @@ class EntitiesService { ...@@ -95,6 +90,18 @@ class EntitiesService {
data: { ...body } 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) { async editEntity(body: IEntity) {
return updatePrismaEntity(body); return updatePrismaEntity(body);
} }
......
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