@@ -15,9 +15,17 @@ async function uploadToAppDev(buffer: Buffer, filename: string): Promise<string>
1515 const form = new FormData ( ) ;
1616 form . append ( "bucket" , APPDEV_BUCKET ) ;
1717 form . append ( "image" , new File ( [ new Uint8Array ( buffer ) ] , filename , { type : "image/webp" } ) ) ;
18+ console . log ( "[avatar/appdev] uploading to" , UPLOAD_URL , { bucket : APPDEV_BUCKET , filename, bytes : buffer . byteLength } ) ;
1819 const res = await fetch ( UPLOAD_URL , { method : "POST" , body : form } ) ;
19- if ( ! res . ok ) throw new Error ( "Upload failed" ) ;
20- return ( await res . text ( ) ) . trim ( ) ;
20+ console . log ( "[avatar/appdev] response status" , res . status ) ;
21+ if ( ! res . ok ) {
22+ const body = await res . text ( ) ;
23+ console . error ( "[avatar/appdev] upload failed" , { status : res . status , body } ) ;
24+ throw new Error ( `AppDev upload failed: ${ res . status } ${ body } ` ) ;
25+ }
26+ const url = ( await res . text ( ) ) . trim ( ) ;
27+ console . log ( "[avatar/appdev] uploaded url" , url ) ;
28+ return url ;
2129}
2230
2331async function removeFromAppDev ( imageUrl : string ) : Promise < void > {
@@ -52,38 +60,47 @@ async function removeFromEmulator(uid: string): Promise<void> {
5260}
5361
5462export async function POST ( request : NextRequest ) {
55- const tokens = await getTokens ( request . cookies , authConfig ) ;
56- if ( ! tokens ) return NextResponse . json ( { error : "Unauthorized" } , { status : 401 } ) ;
63+ try {
64+ const tokens = await getTokens ( request . cookies , authConfig ) ;
65+ if ( ! tokens ) return NextResponse . json ( { error : "Unauthorized" } , { status : 401 } ) ;
66+
67+ const uid = tokens . decodedToken . uid ;
5768
58- const uid = tokens . decodedToken . uid ;
69+ const formData = await request . formData ( ) ;
70+ const file = formData . get ( "file" ) as File | null ;
71+ if ( ! file ) return NextResponse . json ( { error : "No file provided" } , { status : 400 } ) ;
5972
60- const formData = await request . formData ( ) ;
61- const file = formData . get ( "file" ) as File | null ;
62- if ( ! file ) return NextResponse . json ( { error : "No file provided" } , { status : 400 } ) ;
73+ console . log ( "[avatar] received file" , { name : file . name , size : file . size , type : file . type } ) ;
6374
64- const arrayBuffer = await file . arrayBuffer ( ) ;
65- const processed = await sharp ( Buffer . from ( arrayBuffer ) )
66- . resize ( SIZE , SIZE , { fit : "cover" } )
67- . webp ( { quality : 85 } )
68- . toBuffer ( ) ;
75+ const arrayBuffer = await file . arrayBuffer ( ) ;
76+ const processed = await sharp ( Buffer . from ( arrayBuffer ) )
77+ . resize ( SIZE , SIZE , { fit : "cover" } )
78+ . webp ( { quality : 85 } )
79+ . toBuffer ( ) ;
6980
70- // Remove old image
71- const existing = await getUserProfile ( uid ) ;
72- if ( existing ?. profilePictureUrl ) {
73- if ( process . env . NODE_ENV === "development" ) {
74- await removeFromEmulator ( uid ) ;
75- } else {
76- await removeFromAppDev ( existing . profilePictureUrl ) ;
81+ console . log ( "[avatar] processed buffer size" , processed . byteLength ) ;
82+
83+ // Remove old image
84+ const existing = await getUserProfile ( uid ) ;
85+ if ( existing ?. profilePictureUrl ) {
86+ if ( process . env . NODE_ENV === "development" ) {
87+ await removeFromEmulator ( uid ) ;
88+ } else {
89+ await removeFromAppDev ( existing . profilePictureUrl ) ;
90+ }
7791 }
78- }
7992
80- // Upload new image
81- const url =
82- process . env . NODE_ENV === "development"
83- ? await uploadToEmulator ( processed , uid )
84- : await uploadToAppDev ( processed , `${ uid } .webp` ) ;
93+ // Upload new image
94+ const url =
95+ process . env . NODE_ENV === "development"
96+ ? await uploadToEmulator ( processed , uid )
97+ : await uploadToAppDev ( processed , `${ uid } .webp` ) ;
8598
86- await updateUserProfile ( uid , { profilePictureUrl : url } ) ;
99+ await updateUserProfile ( uid , { profilePictureUrl : url } ) ;
87100
88- return NextResponse . json ( { url } ) ;
101+ return NextResponse . json ( { url } ) ;
102+ } catch ( err ) {
103+ console . error ( "[avatar] unhandled error" , err ) ;
104+ return NextResponse . json ( { error : String ( err ) } , { status : 500 } ) ;
105+ }
89106}
0 commit comments