upload routes tested
This commit is contained in:
parent
73631d1972
commit
15d1f6967e
6 changed files with 28 additions and 23 deletions
|
|
@ -55,7 +55,7 @@ export const createProject = async (userId: string) => {
|
||||||
try {
|
try {
|
||||||
const { id } = await createEmptyProject(userId);
|
const { id } = await createEmptyProject(userId);
|
||||||
const bucket = await createBucket(id);
|
const bucket = await createBucket(id);
|
||||||
return { status: 200, message: "New project created and file uploaded successfully", data: { id, bucketName: bucket } };
|
return { status: 200, message: "New project created successfully", data: { id, bucketName: bucket } };
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(error.message);
|
console.log(error.message);
|
||||||
|
|
@ -91,7 +91,6 @@ export const updateProject = async (id: string, body: any) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const deleteProject = async (id: string) => {
|
export const deleteProject = async (id: string) => {
|
||||||
try {
|
try {
|
||||||
const deleteProject = await db.delete(projects).where(eq(projects.id, id)).returning({ id: projects.id });
|
const deleteProject = await db.delete(projects).where(eq(projects.id, id)).returning({ id: projects.id });
|
||||||
|
|
|
||||||
|
|
@ -4,19 +4,15 @@ import { 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";
|
||||||
|
|
||||||
export const uploadPhoto = async (formData: FormData, userId: string) => {
|
export const uploadPhoto = async (file: File, project_id: string, userId: string) => {
|
||||||
try {
|
try {
|
||||||
// Validate userId
|
// Validate userId
|
||||||
if (!userId || typeof userId !== "string") {
|
if (!userId || typeof userId !== "string") {
|
||||||
return { status: 400, message: "Invalid user ID" };
|
return { status: 400, message: "Invalid user ID" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract form data
|
|
||||||
const projectId = formData.get("id");
|
|
||||||
const file = formData.get("file");
|
|
||||||
|
|
||||||
// Validate projectId
|
// Validate projectId
|
||||||
if (!projectId || typeof projectId !== "string") {
|
if (!project_id || typeof project_id !== "string") {
|
||||||
return { status: 400, message: "Invalid project ID" };
|
return { status: 400, message: "Invalid project ID" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,17 +21,24 @@ export const uploadPhoto = async (formData: FormData, userId: string) => {
|
||||||
return { status: 400, message: "Invalid or missing file" };
|
return { status: 400, message: "Invalid or missing file" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload file
|
// Extract file extension (e.g., ".jpg", ".png")
|
||||||
const urlLink = await uploadToMinio(file, projectId, file.name);
|
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) {
|
if (!urlLink || !urlLink.url) {
|
||||||
return { status: 500, message: "File upload failed" };
|
return { status: 500, message: "File upload failed" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save file info in DB
|
// Save file info in DB with modified filename
|
||||||
const saveFile = await db.insert(uploads).values({
|
const saveFile = await db.insert(uploads).values({
|
||||||
filename: file.name,
|
filename: uniqueFileName,
|
||||||
url: urlLink.url,
|
url: urlLink.url,
|
||||||
projectId
|
projectId: project_id,
|
||||||
}).returning();
|
}).returning();
|
||||||
|
|
||||||
return { status: 200, message: "File uploaded successfully", data: saveFile };
|
return { status: 200, message: "File uploaded successfully", data: saveFile };
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,10 @@ export const uploadRoutes = new Elysia({
|
||||||
return { userId: authData.userId }; // Inject into context
|
return { userId: authData.userId }; // Inject into context
|
||||||
});
|
});
|
||||||
|
|
||||||
uploadRoutes.post("/add", async ({ request, userId }) => {
|
uploadRoutes.post("/add", async ({ body, userId }) => {
|
||||||
const user_id: String | any = userId;
|
const user_id: String | any = userId;
|
||||||
const formData = await request.formData();
|
const { id: project_id, file } = body;
|
||||||
return uploadPhoto(formData, user_id);
|
return uploadPhoto(file, project_id, user_id);
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
file: t.File(),
|
file: t.File(),
|
||||||
|
|
@ -27,9 +27,12 @@ uploadRoutes.post("/add", async ({ request, userId }) => {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
uploadRoutes.delete("/delete/:uploads_url", async ({ params: { uploads_url } }) => deletePhoto(uploads_url), {
|
uploadRoutes.delete("/delete", async ({ query }) => {
|
||||||
params: t.Object({
|
const { url } = query;
|
||||||
uploads_url: t.String(),
|
return deletePhoto(url);
|
||||||
|
}, {
|
||||||
|
query: t.Object({
|
||||||
|
url: t.String(),
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ const allowedOrigins = [
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(cors({
|
.use(cors({
|
||||||
origin: allowedOrigins,
|
origin: allowedOrigins,
|
||||||
|
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"],
|
||||||
|
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With", "Accept", "Origin", "Access-Control-Allow-Origin"],
|
||||||
credentials: true,
|
credentials: true,
|
||||||
}))
|
}))
|
||||||
.use(swagger({
|
.use(swagger({
|
||||||
|
|
|
||||||
|
|
@ -26,9 +26,8 @@ export const createBucket = async (bucketName: string) => {
|
||||||
} else {
|
} else {
|
||||||
return bucketName; // Return the bucket name if it already exists
|
return bucketName; // Return the bucket name if it already exists
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error("Error creating or configuring bucket:", error);
|
console.error("Error creating or configuring bucket:", error);
|
||||||
|
|
||||||
// Optionally rethrow the error with additional context
|
// Optionally rethrow the error with additional context
|
||||||
throw new Error(`Error creating bucket "${bucketName}": ${error.message}`);
|
throw new Error(`Error creating bucket "${bucketName}": ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,8 @@ export const uploadToMinio = async (file: File, bucketName: string, objectName:
|
||||||
|
|
||||||
return { url: publicUrl };
|
return { url: publicUrl };
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error("Error uploading file to MinIO:", error);
|
console.error("Error uploading file to MinIO:", error);
|
||||||
|
|
||||||
throw new Error(`Error uploading file: ${error.message}`);
|
throw new Error(`Error uploading file: ${error.message}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Loading…
Add table
Reference in a new issue