import { eq } from "drizzle-orm"; import { db } from "../../db"; import { projects, uploads } from "../../db/schema"; import { uploadToMinio } from "../../helper/upload/uploadToMinio"; import { removeFromMinio } from "../../helper/upload/removeFromMinio"; export const uploadPhoto = async (file: File, project_id: string, userId: string, token: string) => { try { // Validate userId if (!userId || typeof userId !== "string") { return { status: 400, message: "Invalid user ID", token }; } // Validate projectId if (!project_id || typeof project_id !== "string") { return { status: 400, message: "Invalid project ID", token }; } // Validate file input if (!file || !(file instanceof File) || !file.name) { return { status: 400, message: "Invalid or missing file", token }; } const findProject = await db.select().from(projects).where(eq(projects.id, project_id)); if (findProject.length > 0) { // Extract file extension (e.g., ".jpg", ".png") const fileExtension = file.name.substring(file.name.lastIndexOf(".")); // Generate a unique filename using the timestamp const timestamp = Date.now(); // Current timestamp in milliseconds const uniqueFileName = `${file.name.split(".")[0]}-${timestamp}${fileExtension}`; // Upload file to MinIO with the unique filename const urlLink = await uploadToMinio(file, project_id, uniqueFileName); if (!urlLink || !urlLink.url) { return { status: 500, message: "File upload failed", token }; } // Save file info in DB with modified filename const saveFile = await db.insert(uploads).values({ filename: uniqueFileName, url: urlLink.url, projectId: project_id, }).returning(); return { status: 200, message: "File uploaded successfully", data: saveFile, token }; } else { return { status: 404, message: "No projects found with this project id", token } } } catch (error: any) { console.error("Error processing file:", error); return { status: 500, message: "An error occurred while uploading the photo", token }; } }; export const deletePhoto = async (url: string, token: string) => { try { if (!url) { return { status: 404, message: "File url is missing", token } } const deleteFile = await db .delete(uploads) .where(eq(uploads.url, url)) .returning(); // Ensure there's a file to delete if (!deleteFile || deleteFile.length === 0) { return { status: 404, message: "File not found", token }; } const { projectId, filename } = deleteFile[0]; // Ensure projectId and filename are valid if (!projectId || !filename) { return { status: 400, message: "Invalid project ID or filename", token }; } const minioRemove = await removeFromMinio(projectId, filename); return { status: 200, message: minioRemove.msg, token }; } catch (error: any) { console.error("Error processing file:", error); return { status: 500, message: `An error occurred while deleting the photo: ${error.message}`, token }; } }; export const getAllPhoto = async (id: string, token: string) => { try { // project id if (!id) { return { status: 404, message: "Project ID is missing", token } } const getAllPhoto = await db.select().from(uploads).where(eq(uploads.projectId, id)); if (getAllPhoto.length === 0) { return { status: 200, message: "No photos found for the given project ID", data: [], token } } return { status: 200, message: "All photos retrieved successfully", data: getAllPhoto, token }; } catch (error: any) { console.log(`Error getting photos: ${error.message}`); return { status: 500, message: "An error occurred while getting the photos", token } } }