Newer
Older
import { PrismaClient } from '@prisma/client';
import * as fs from 'node:fs';
import path from 'node:path';
class HomeService {
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
async getHomeBackground() {
const backgroundInfo = await prisma.setting.findFirst({
where: {
setting_name: 'homeBackground'
}
});
if (backgroundInfo?.setting_value) {
const file = fs.readFileSync(backgroundInfo.setting_value);
return Buffer.from(file, 'base64');
}
}
async changeHomeBackground(body) {
const response = await fetch(body.setting_value);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const extension = body.extension.split('/')[1];
const imagePath = path.join(path.resolve(), `/public/images/backgrounds/homeBackground.jpg`);
fs.writeFileSync(imagePath, buffer);
delete body.extension;
body.setting_value = imagePath;
const homeBackground = await prisma.setting.findFirst({
where: {
setting_name: 'homeBackground'
}
});
if (homeBackground) {
return prisma.setting.update({
where: {
setting_name: 'homeBackground'
},
data: { ...body }
});
}
return prisma.setting.create({ data: { ...body } });
}
async removeHomeBackground() {
const imagePath = path.join(path.resolve(), `/public/images/backgrounds/homeBackground.jpg`);
fs.unlink(imagePath, (err) => {
if (err) throw err;
});
await prisma.setting.delete({
where: {
setting_name: 'homeBackground'
}
});
}
const entitiesDB = await prisma.home_entity.findMany({
orderBy: [{ entity_order: 'asc' }]
});
const entitiesImages = [];
const entitiesToReturn = entitiesDB.map((entity) => {
if (!entity.image_width) return entity;
const imagePath = path.join(path.resolve(), `/public/images/${entity.entity_uuid}.jpg`);
const file = fs.readFileSync(imagePath);
const buffer = Buffer.from(file, 'base64');
entitiesImages.push(buffer);
return entity;
});
return {
entities: entitiesToReturn,
entitiesImages: entitiesImages
};
const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
newImagePath.splice(-1);
newImagePath.push(`${body.entity_uuid}.jpg`);
fs.rename(imagePath, newImagePath, function (err) {
if (err) console.log('ERROR in fs.rename: ' + err);
});
return prisma.home_entity.create({ data: { ...body } });
}
// единственная функция, срабатывающая по сокету для файлов
async createImage(body) {
const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
fs.writeFileSync(imagePath, body);
async cropImage(body) {
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.home_entity.update({
where: {
entity_uuid: body.entity_uuid
},
data: { ...body }
});
}
async editEntity(body) {
return prisma.home_entity.update({
where: {
entity_uuid: body.entity_uuid
},
data: { ...body }
});
}
async deleteEntity(body) {
const deletedEntity = await prisma.home_entity.findFirst({
where: {
entity_uuid: body.entity_uuid
}
});
await prisma.home_entity.delete({
where: {
entity_uuid: body.entity_uuid
}
});
if (body.image_width)
fs.unlink(body.image_path, (err) => {
if (err) throw err;
});
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
return deletedEntity;
}
async changeOrderEntity(body) {
const allEntities = await prisma.home_entity.findMany({
orderBy: [{ entity_order: 'asc' }]
});
const currentEntity = allEntities.find((entity) => entity.entity_uuid === body.entity_uuid);
const nextEntity =
body.direction === 'up'
? allEntities.reverse().find((entity) => entity.entity_order < currentEntity.entity_order)
: allEntities.find((entity) => entity.entity_order > currentEntity.entity_order);
await prisma.home_entity.update({
where: {
entity_uuid: currentEntity.entity_uuid
},
data: {
entity_order: nextEntity.entity_order
}
});
await prisma.home_entity.update({
where: {
entity_uuid: nextEntity.entity_uuid
},
data: {
entity_order: currentEntity.entity_order
}
});
return body;
}