File tree Expand file tree Collapse file tree
03-Client/src/app/services/api Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,26 @@ interface UploadResult {
1515 data : { url : string } ;
1616}
1717
18+ const MIME_TO_EXT : Record < string , string > = {
19+ "image/jpeg" : ".jpg" ,
20+ "image/jpg" : ".jpg" ,
21+ "image/png" : ".png" ,
22+ "image/webp" : ".webp" ,
23+ } ;
24+
25+ // browser-image-compression and mobile capture sometimes produce a File with
26+ // no name or no extension. The server's extension check rejects those even
27+ // when the bytes are a valid JPEG, so we derive a name from the MIME type.
28+ function ensureFileName ( file : File ) : string {
29+ const name = file . name ?? "" ;
30+ const hasAllowedExt = / \. ( j p e ? g | p n g | w e b p ) $ / i. test ( name ) ;
31+ if ( hasAllowedExt ) return name ;
32+
33+ const ext = MIME_TO_EXT [ file . type ?. toLowerCase ( ) ?? "" ] ?? ".jpg" ;
34+ const base = name . replace ( / \. [ ^ . ] + $ / , "" ) || `upload-${ Date . now ( ) } ` ;
35+ return `${ base } ${ ext } ` ;
36+ }
37+
1838/**
1939 * Upload an image file to the backend.
2040 * Returns the URL of the stored file.
@@ -23,7 +43,7 @@ export async function uploadImage(file: File): Promise<string> {
2343 const token = localStorage . getItem ( "accessToken" ) ;
2444
2545 const formData = new FormData ( ) ;
26- formData . append ( "file" , file ) ;
46+ formData . append ( "file" , file , ensureFileName ( file ) ) ;
2747
2848 const res = await fetch ( `${ API_BASE } /files/upload` , {
2949 method : "POST" ,
You can’t perform that action at this time.
0 commit comments