Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved Storage Calls from Frontend to Backend #126

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 28 additions & 55 deletions app/(default)/api/leads/upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,33 @@
import { getStorage, ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { NextResponse } from "next/server";
import { db, storage} from "@/Firebase";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { NextResponse } from "next/server";
import { storage } from "@/Firebase";


export async function POST(request: Request) {
try {
const storage = getStorage();
const selectedLead = await request.json();
const imageRef = ref(storage, `images/${selectedLead.name}`);
let imageUrl;
try {
const formData = await request.formData();

try {
const imageBlob = await fetch(selectedLead.imageUrl).then((r) => r.blob());
console.log("Image blob fetched:", imageBlob);
try {
await uploadBytes(imageRef, imageBlob);
console.log("Image uploaded to storage");
try {
imageUrl = await getDownloadURL(imageRef);
console.log("Image URL obtained:", imageUrl);
} catch (error: any) {
console.error("Error getting image URL:", error);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
}
} catch (error: any) {
console.error("Error uploading image:", error);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
}
} catch (error: any) {
console.error("Error fetching image blob:", error);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
}

return NextResponse.json({ imageUrl: imageUrl }, { status: 201 });
} catch (error) {
if (error instanceof Error) {
console.error("Error details:", error.message);
return NextResponse.json(
{ error: "An error occurred", details: error.message },
{ status: 500 }
);
} else {
console.error("Unknown error:", error);
return NextResponse.json(
{ error: "An unknown error occurred" },
{ status: 500 }
);
}
const Image: File | null = formData.get('file') as File;
if (!Image) {
return NextResponse.json(
{ message: "Bad Request", details: "No file uploaded" },
{ status: 400 }
);
}
}

const name: string = formData.get('name') as string;
const imageRef = ref(storage, `images/${name}`);
await uploadBytes(imageRef, Image);
const imageUrl = await getDownloadURL(imageRef);

return NextResponse.json({
imageUrl: imageUrl
});
} catch (error:any) {
console.error("Error uploading file:", error);
return NextResponse.json(
{ message: "Internal Server Error", details: error.message },
{ status: 500 }
);
}
}
Loading