@@ -14,14 +14,14 @@ if (input) input.onchange = async (e: Event) => {
14
14
const file = element . files [ 0 ] ;
15
15
const compressedFile = await compressFile ( file ) ;
16
16
const dataURI = await getBase64Image ( compressedFile ) ;
17
- const fullName = file . name ;
18
- const fullNameArray = fullName . split ( '.' ) ;
19
- const extension = fullNameArray . pop ( ) ;
20
- const fileName = fullNameArray . join ( '.' ) ;
17
+ const fileName = file . name . split ( '.' ) . slice ( 0 , - 1 ) . join ( '.' ) ;
18
+ const isCompressed = compressedFile . size !== file . size ;
19
+ const newFileName = isCompressed ? fileName + '-min' : fileName ;
20
+ const fileExtension = isCompressed ? 'jpg' : file . name . split ( '.' ) . at ( - 1 ) ;
21
21
downloadElement . href = dataURI ;
22
22
statusElement . style . display = 'none' ;
23
23
downloadElement . style . display = '' ;
24
- downloadElement . download = ` ${ fileName } -min. ${ extension } ` ;
24
+ downloadElement . download = newFileName + '.' + fileExtension ;
25
25
}
26
26
27
27
let quality = 1 ;
@@ -45,7 +45,7 @@ async function compressFile(file: File): Promise<Blob> {
45
45
function blobToBase64 ( blob : Blob ) : Promise < string > {
46
46
return new Promise ( resolve => {
47
47
const reader = new FileReader ( ) ;
48
- reader . onload = function ( ) {
48
+ reader . onload = ( ) => {
49
49
const dataUrl = reader . result ;
50
50
if ( typeof dataUrl === 'string' ) resolve ( dataUrl ) ;
51
51
} ;
0 commit comments