-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from govindup63/main
feat: Moved Storage Calls from Frontend to Backend
- Loading branch information
Showing
10 changed files
with
655 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
); | ||
} | ||
} |
Oops, something went wrong.