198 lines
5.9 KiB
JavaScript
198 lines
5.9 KiB
JavaScript
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
const PROJECTS_DIR = path.join('.');
|
|
const database_manager = require('../database/database_manager.js');
|
|
|
|
async function createFolder(name) {
|
|
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
|
try {
|
|
await fs.access(projectDir);
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
await fs.mkdir(projectDir, { recursive: true });
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
return projectDir;
|
|
}
|
|
|
|
async function deleteFolder(name) {
|
|
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
|
try {
|
|
await fs.access(projectDir);
|
|
await fs.rm(projectDir, { recursive: true, force: true });
|
|
} catch (error) {
|
|
if (error.code !== 'ENOENT') {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function scanAllImages(dir = 'storage') {
|
|
const projectDir = path.join(PROJECTS_DIR, dir);
|
|
let results = [];
|
|
|
|
// check if the directory exists and create it if not
|
|
try {
|
|
await fs.access(projectDir);
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
await fs.mkdir(projectDir, { recursive: true });
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function scanDirectory(directory) {
|
|
const files = await fs.readdir(directory);
|
|
for (const file of files) {
|
|
const filePath = path.join(directory, file);
|
|
const stat = await fs.stat(filePath);
|
|
if (stat.isDirectory()) {
|
|
await scanDirectory(filePath);
|
|
} else if (file.endsWith('.jpg')) {
|
|
results.push(filePath);
|
|
}
|
|
}
|
|
}
|
|
|
|
await scanDirectory(projectDir);
|
|
return results;
|
|
}
|
|
|
|
async function saveFile(filePath, content) {
|
|
let Buffer=Buffer.from(content, 'base64');
|
|
if (Buffer.isBuffer(content)) {
|
|
await fs.writeFile(filePath, content);
|
|
} else {
|
|
throw new Error('Content must be a buffer');
|
|
}
|
|
}
|
|
|
|
async function getFile(name) {
|
|
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
|
return await fs.readFile(filePath);
|
|
}
|
|
|
|
async function deleteFile(name) {
|
|
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
|
try {
|
|
await fs.access(filePath); // Vérifie si le fichier existe
|
|
await fs.rm(filePath); // Supprime le fichier
|
|
return `File ${filePath} deleted successfully.`;
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
return `File ${filePath} does not exist.`;
|
|
} else {
|
|
throw error; // Relance l'erreur si ce n'est pas une erreur de fichier introuvable
|
|
}
|
|
}
|
|
}
|
|
|
|
async function handleFileOperation(operation, ...args) {
|
|
try {
|
|
return await operation(...args);
|
|
} catch (error) {
|
|
console.error(`[FILE OPERATION ERROR] ${error.message}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
const project = {
|
|
createProjectDirectory: async function (projectId) {
|
|
const projectPath = `${projectId}`;
|
|
await handleFileOperation(createFolder, projectPath);
|
|
await handleFileOperation(createFolder, `${projectPath}/images`);
|
|
await handleFileOperation(createFolder, `${projectPath}/videos`);
|
|
console.log("[FILE] createProjectDirectory : " + projectPath);
|
|
},
|
|
|
|
deleteProjectDirectory: async function (projectId) {
|
|
const projectPath = `${projectId}`;
|
|
await handleFileOperation(deleteFolder, projectPath);
|
|
console.log("[FILE] deleteProjectDirectory : " + projectPath);
|
|
}
|
|
};
|
|
|
|
const measurement = {
|
|
get_measurement_image: async function (projectId, orderId) {
|
|
const projectPath = `${projectId}`;
|
|
const imagePath = `${projectPath}/images/${orderId}.jpg`;
|
|
console.log("[FILE] get_measurement_image : " + imagePath);
|
|
return await handleFileOperation(getFile, imagePath);
|
|
},
|
|
|
|
upload_measurement_image: async function (image, projectId, orderId) {
|
|
const projectPath = `${projectId}`;
|
|
const imagePath = `${projectPath}/images/${orderId}.jpg`;
|
|
console.log("[FILE] upload_measurement_image : " + imagePath);
|
|
await handleFileOperation(saveFile, imagePath, image.buffer);
|
|
return imagePath;
|
|
},
|
|
|
|
get_path_from_id: async function (projectId, orderId) {
|
|
const query = database_manager.measurement.get_measurement_by_project_and_order_id(projectId, orderId);
|
|
console.log("[FILE] get_path_from_id : " + query.path);
|
|
console.log("[FILE] get_path_from_id : " + query);
|
|
return query.path;
|
|
},
|
|
|
|
get_path_list: async function (IdList, projectId) {
|
|
let parsedIdList;
|
|
try {
|
|
parsedIdList = JSON.parse(IdList);
|
|
} catch (e) {
|
|
console.error("Error parsing IdList:", e);
|
|
return [];
|
|
}
|
|
|
|
const pathList = [];
|
|
for (const orderId of parsedIdList) {
|
|
const path = await this.get_path_from_id(projectId, orderId);
|
|
pathList.push(path);
|
|
}
|
|
return pathList;
|
|
},
|
|
}
|
|
|
|
const video = {
|
|
get_video: async function (projectId, orderId) {
|
|
const projectPath = `${projectId}`;
|
|
const videoPath = `${projectPath}/videos/${orderId}.mp4`;
|
|
console.log("[FILE] get_video : " + videoPath);
|
|
return await handleFileOperation(getFile, videoPath);
|
|
},
|
|
|
|
delete_video: async function (videoId) {
|
|
const query = database_manager.video.get_video_by_id(videoId);
|
|
const videoPath = query.video_file;
|
|
console.log("[FILE] delete_video : " + videoPath);
|
|
return await handleFileOperation(deleteFile, videoPath);
|
|
},
|
|
|
|
delete_unfinished_videos: async function () {
|
|
const unfinishedVideos = await database_manager.video.get_unfinished_videos();
|
|
|
|
for (const video of unfinishedVideos) {
|
|
try {
|
|
await this.delete_video(video.id);
|
|
console.log(`Deleted unfinished video with id: ${video.id}`);
|
|
} catch (error) {
|
|
console.error(`Error deleting unfinished video with id: ${video.id}`, error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
createFolder,
|
|
deleteFolder,
|
|
scanAllImages,
|
|
saveFile,
|
|
getFile,
|
|
deleteFile,
|
|
project,
|
|
measurement,
|
|
video
|
|
}; |