Galery nearly finished. only access rights aren't loaded for editin
This commit is contained in:
@@ -10,6 +10,14 @@ import Folder from '../models/community/folder.js';
|
||||
import Image from '../models/community/image.js';
|
||||
import ImageVisibilityType from '../models/type/image_visibility.js';
|
||||
import FolderImageVisibility from '../models/community/folder_image_visibility.js';
|
||||
import ImageImageVisibility from '../models/community/image_image_visibility.js';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
class SocialNetworkService extends BaseService {
|
||||
async searchUsers({ username, ageFrom, ageTo, genders }) {
|
||||
@@ -95,15 +103,81 @@ class SocialNetworkService extends BaseService {
|
||||
return await Image.findAll({
|
||||
where: {
|
||||
folderId: folder.id
|
||||
}
|
||||
},
|
||||
order: [
|
||||
['title', 'asc']
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async uploadImage(hashedId, imageData) {
|
||||
this.validateImageData(imageData);
|
||||
const userId = await this.checkUserAccess(hashedId);
|
||||
imageData.id = userId;
|
||||
return await Image.create(imageData);
|
||||
async uploadImage(hashedId, file, formData) {
|
||||
const userId = await this.getUserId(hashedId);
|
||||
const newFileName = this.generateUniqueFileName(file.originalname);
|
||||
const filePath = this.buildFilePath(newFileName);
|
||||
await this.saveFile(file.buffer, filePath);
|
||||
const newImage = await this.createImageRecord(formData, userId, file, newFileName);
|
||||
await this.saveImageVisibilities(newImage.id, formData.visibility);
|
||||
return newImage;
|
||||
}
|
||||
|
||||
async getUserId(hashedId) {
|
||||
return await this.checkUserAccess(hashedId);
|
||||
}
|
||||
|
||||
generateUniqueFileName(originalFileName) {
|
||||
const uniqueHash = uuidv4();
|
||||
return `${uniqueHash}`;
|
||||
}
|
||||
|
||||
buildFilePath(fileName) {
|
||||
const userImagesPath = path.join(__dirname, '../images/user');
|
||||
return path.join(userImagesPath, fileName);
|
||||
}
|
||||
|
||||
async saveFile(buffer, filePath) {
|
||||
try {
|
||||
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
||||
await fs.writeFile(filePath, buffer);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to save file: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async createImageRecord(formData, userId, file, fileName) {
|
||||
try {
|
||||
return await Image.create({
|
||||
title: formData.title,
|
||||
description: formData.description || null,
|
||||
originalFileName: file.originalname,
|
||||
hash: fileName,
|
||||
folderId: formData.folderId,
|
||||
userId: userId,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create image record: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async saveImageVisibilities(imageId, visibilities) {
|
||||
if (typeof visibilities === 'string') {
|
||||
visibilities = JSON.parse(visibilities);
|
||||
}
|
||||
if (!visibilities || !Array.isArray(visibilities)) {
|
||||
throw new Error('Invalid visibilities provided');
|
||||
}
|
||||
|
||||
try {
|
||||
const visibilityPromises = visibilities.map(visibilityId => {
|
||||
return ImageImageVisibility.create({
|
||||
imageId: imageId,
|
||||
visibilityTypeId: visibilityId
|
||||
});
|
||||
});
|
||||
|
||||
await Promise.all(visibilityPromises);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to save image visibilities: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async getImage(imageId) {
|
||||
@@ -255,6 +329,51 @@ class SocialNetworkService extends BaseService {
|
||||
async getPossibleImageVisibilities() {
|
||||
return await ImageVisibilityType.findAll();
|
||||
}
|
||||
|
||||
async getImageFilePath(hashedUserId, hash) {
|
||||
const image = await Image.findOne({ where: { hash } });
|
||||
if (!image) {
|
||||
throw new Error('Image not found');
|
||||
}
|
||||
const userId = await this.checkUserAccess(hashedUserId);
|
||||
const hasAccess = await this.checkUserImageAccess(userId, image.id);
|
||||
if (!hasAccess) {
|
||||
throw new Error('Access denied');
|
||||
}
|
||||
const imagePath = this.buildFilePath(image.hash);
|
||||
if (!fs.existsSync(imagePath)) {
|
||||
throw new Error('File not found');
|
||||
}
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
async checkUserImageAccess(userId, imageId) {
|
||||
const image = await Image.findByPk(imageId);
|
||||
if (image.userId === userId) {
|
||||
return true;
|
||||
}
|
||||
const accessRules = await ImageImageVisibility.findAll({
|
||||
where: { imageId }
|
||||
});
|
||||
return accessRules.some(rule => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
async changeImage(hashedUserId, imageId, title, visibilities) {
|
||||
const userId = await this.checkUserAccess(hashedUserId);
|
||||
await this.checkUserImageAccess(userId, imageId);
|
||||
const image = await Image.findOne({ where: { id: imageId } });
|
||||
if (!image) {
|
||||
throw new Error('image not found')
|
||||
}
|
||||
await image.update({ title: title });
|
||||
await ImageImageVisibility.destroy({ where: { imageId } });
|
||||
for (const visibility of visibilities) {
|
||||
await ImageImageVisibility.create({ imageId, visibilityTypeId: visibility.id });
|
||||
}
|
||||
return image.folderId;
|
||||
}
|
||||
}
|
||||
|
||||
export default SocialNetworkService;
|
||||
|
||||
Reference in New Issue
Block a user