3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { VSBuffer } from '../../../../base/common/buffer.js' ;
6
+ import { decodeBase64 , VSBuffer } from '../../../../base/common/buffer.js' ;
7
7
import { joinPath } from '../../../../base/common/resources.js' ;
8
8
import { URI } from '../../../../base/common/uri.js' ;
9
9
import { IFileService } from '../../../../platform/files/common/files.js' ;
@@ -55,7 +55,11 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
55
55
const ctx = canvas . getContext ( '2d' ) ;
56
56
if ( ctx ) {
57
57
ctx . drawImage ( img , 0 , 0 , width , height ) ;
58
- canvas . toBlob ( ( blob ) => {
58
+
59
+ const jpegTypes = [ 'image/jpeg' , 'image/jpg' ] ;
60
+ const outputMimeType = mimeType && jpegTypes . includes ( mimeType ) ? 'image/jpeg' : 'image/png' ;
61
+
62
+ canvas . toBlob ( blob => {
59
63
if ( blob ) {
60
64
const reader = new FileReader ( ) ;
61
65
reader . onload = ( ) => {
@@ -66,7 +70,7 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
66
70
} else {
67
71
reject ( new Error ( 'Failed to create blob from canvas' ) ) ;
68
72
}
69
- } , 'image/png' ) ;
73
+ } , outputMimeType ) ;
70
74
} else {
71
75
reject ( new Error ( 'Failed to get canvas context' ) ) ;
72
76
}
@@ -81,7 +85,7 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
81
85
export function convertStringToUInt8Array ( data : string ) : Uint8Array {
82
86
const base64Data = data . includes ( ',' ) ? data . split ( ',' ) [ 1 ] : data ;
83
87
if ( isValidBase64 ( base64Data ) ) {
84
- return Uint8Array . from ( atob ( base64Data ) , char => char . charCodeAt ( 0 ) ) ;
88
+ return decodeBase64 ( base64Data ) . buffer ;
85
89
}
86
90
return new TextEncoder ( ) . encode ( data ) ;
87
91
}
0 commit comments