Added the ablility to save the response in utf8 format. (#2792)

This commit is contained in:
Sanjai Kumar 2024-08-12 17:20:56 +05:30 committed by GitHub
parent 0d3e7acf9b
commit 5706c4b138
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1165,12 +1165,22 @@ const registerNetworkIpc = (mainWindow) => {
return `response.${extension}`;
};
const fileName =
getFileNameFromContentDispositionHeader() || getFileNameFromUrlPath() || getFileNameBasedOnContentTypeHeader();
const getEncodingFormat = () => {
const contentType = getHeaderValue('content-type');
const extension = mime.extension(contentType) || 'txt';
return ['json', 'xml', 'html', 'yml', 'yaml', 'txt'].includes(extension) ? 'utf-8' : 'base64';
};
const determineFileName = () => {
return (
getFileNameFromContentDispositionHeader() || getFileNameFromUrlPath() || getFileNameBasedOnContentTypeHeader()
);
};
const fileName = determineFileName();
const filePath = await chooseFileToSave(mainWindow, fileName);
if (filePath) {
await writeBinaryFile(filePath, Buffer.from(response.dataBuffer, 'base64'));
await writeBinaryFile(filePath, Buffer.from(response.dataBuffer, getEncodingFormat()));
}
} catch (error) {
return Promise.reject(error);