Skip to content

Commit

Permalink
Merge pull request #126 from govindup63/main
Browse files Browse the repository at this point in the history
feat:  Moved Storage Calls from Frontend to Backend
  • Loading branch information
SkySingh04 authored Dec 20, 2024
2 parents 18725ad + a2d998d commit e4b9588
Show file tree
Hide file tree
Showing 10 changed files with 655 additions and 531 deletions.
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

0 comments on commit e4b9588

Please sign in to comment.