upload photo controller fixed

This commit is contained in:
Saimon8420 2025-02-05 15:38:32 +06:00
parent 473a741f10
commit 0e49fc53ab

View file

@ -1,6 +1,6 @@
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { db } from "../../db"; import { db } from "../../db";
import { uploads } from "../../db/schema"; import { projects, uploads } from "../../db/schema";
import { uploadToMinio } from "../../helper/upload/uploadToMinio"; import { uploadToMinio } from "../../helper/upload/uploadToMinio";
import { removeFromMinio } from "../../helper/upload/removeFromMinio"; import { removeFromMinio } from "../../helper/upload/removeFromMinio";
@ -20,7 +20,9 @@ export const uploadPhoto = async (file: File, project_id: string, userId: string
if (!file || !(file instanceof File) || !file.name) { if (!file || !(file instanceof File) || !file.name) {
return { status: 400, message: "Invalid or missing file", token }; 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") // Extract file extension (e.g., ".jpg", ".png")
const fileExtension = file.name.substring(file.name.lastIndexOf(".")); const fileExtension = file.name.substring(file.name.lastIndexOf("."));
@ -42,7 +44,10 @@ export const uploadPhoto = async (file: File, project_id: string, userId: string
}).returning(); }).returning();
return { status: 200, message: "File uploaded successfully", data: saveFile, token }; 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) { } catch (error: any) {
console.error("Error processing file:", error); console.error("Error processing file:", error);
return { status: 500, message: "An error occurred while uploading the photo", token }; return { status: 500, message: "An error occurred while uploading the photo", token };
@ -52,7 +57,7 @@ export const uploadPhoto = async (file: File, project_id: string, userId: string
export const deletePhoto = async (url: string, token: string) => { export const deletePhoto = async (url: string, token: string) => {
try { try {
if (!url) { if (!url) {
return { status: 404, message: "File ID is missing", token } return { status: 404, message: "File url is missing", token }
} }
const deleteFile = await db const deleteFile = await db