@@ -14,17 +14,17 @@ class ReactS3Client {
1414 }
1515 public async uploadFile ( file : File , newFileName ?: string ) : Promise < UploadResponse > {
1616 throwUploadError ( this . config , file ) ;
17- let fileExtension : string ;
17+ let fileExtension : string = '' ;
1818 const fd = new FormData ( ) ;
1919
20- if ( file . type == null ) {
21- fileExtension = '' ;
22- } else {
23- fileExtension = file . type . split ( '/' ) [ 1 ] ;
20+ if ( file . type != null ) {
21+ fileExtension = file . type . split ( '/' ) . pop ( ) || '' ;
2422 }
2523
26- const fileName = `${ newFileName || shortId . generate ( ) } .${ fileExtension } ` ;
27- const key = `${ this . config . dirName ? this . config . dirName + '/' : '' } ${ fileName } ` ;
24+ const fileName = `${ newFileName || shortId . generate ( ) } ${ fileExtension && ( '.' + fileExtension ) } ` ;
25+ // remove duplicate forward slashes
26+ const dirName = ( this . config . dirName ? this . config . dirName + '/' : '' ) . replace ( / ( [ ^ : ] \/ ) \/ + / g, '$1' )
27+ const key = `${ dirName } ${ fileName } ` ;
2828 const url : string = GetUrl ( this . config ) ;
2929 fd . append ( 'key' , key ) ;
3030 fd . append ( 'acl' , 'public-read' ) ;
@@ -43,8 +43,8 @@ class ReactS3Client {
4343 if ( ! data . ok ) return Promise . reject ( data ) ;
4444 return Promise . resolve ( {
4545 bucket : this . config . bucketName ,
46- key : ` ${ this . config . dirName ? this . config . dirName + '/' : '' } ${ fileName } ` ,
47- location : `${ url } /${ this . config . dirName ? this . config . dirName + '/' : '' } ${ fileName } ` ,
46+ key,
47+ location : `${ url } /${ key } ` ,
4848 status : data . status ,
4949 } ) ;
5050 }
0 commit comments