Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved #1709 - Format json file content when download response #3185

Closed
wants to merge 9 commits into from
Closed
31 changes: 18 additions & 13 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const { makeAxiosInstance } = require('./axios-instance');
const { addAwsV4Interceptor, resolveAwsV4Credentials } = require('./awsv4auth-helper');
const { addDigestInterceptor } = require('./digestauth-helper');
const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../../utils/proxy-util');
const { chooseFileToSave, writeBinaryFile, writeFile } = require('../../utils/filesystem');
const { chooseFileToSave, writeFile } = require('../../utils/filesystem');
const { getCookieStringForUrl, addCookieToJar, getDomainsWithCookies } = require('../../utils/cookies');
const {
resolveOAuth2AuthorizationCodeAccessToken,
Expand Down Expand Up @@ -1232,16 +1232,25 @@ const registerNetworkIpc = (mainWindow) => {
}
};

const contentType = getHeaderValue('content-type');

const getFileExtension = () => {
return (contentType && mime.extension(contentType)) || 'txt';
};

const getFileNameBasedOnContentTypeHeader = () => {
const contentType = getHeaderValue('content-type');
const extension = (contentType && mime.extension(contentType)) || 'txt';
return `response.${extension}`;
return `response.${getFileExtension(contentType)}`;
};

const getFormattedResponseIfJson = () => {
if (getFileExtension(contentType) === 'json') {
return Buffer.from(JSON.stringify(response.data, null, 2), 'utf8');
}
return Buffer.from(response.dataBuffer, 'base64');
};

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';
return ['json', 'xml', 'html', 'yml', 'yaml', 'txt'].includes(getFileExtension(contentType)) ? 'utf-8' : 'base64';
};

const determineFileName = () => {
Expand All @@ -1254,12 +1263,8 @@ const registerNetworkIpc = (mainWindow) => {
const filePath = await chooseFileToSave(mainWindow, fileName);
if (filePath) {
const encoding = getEncodingFormat();
const data = Buffer.from(response.dataBuffer, 'base64')
if (encoding === 'utf-8') {
await writeFile(filePath, data);
} else {
await writeBinaryFile(filePath, data);
}
const formattedResponse = getFormattedResponseIfJson();
await writeFile(filePath, formattedResponse, { encoding });
}
} catch (error) {
return Promise.reject(error);
Expand Down
15 changes: 2 additions & 13 deletions packages/bruno-electron/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,9 @@ function normalizeWslPath(pathname) {
return pathname.replace(/^\/wsl.localhost/, '\\\\wsl.localhost').replace(/\//g, '\\');
}

const writeFile = async (pathname, content) => {
const writeFile = async (pathname, content, encoding = 'utf8') => {
try {
fs.writeFileSync(pathname, content, {
encoding: 'utf8'
});
} catch (err) {
return Promise.reject(err);
}
};

const writeBinaryFile = async (pathname, content) => {
try {
fs.writeFileSync(pathname, content);
fsPromises.writeFile(pathname, content, { encoding });
} catch (err) {
return Promise.reject(err);
}
Expand Down Expand Up @@ -171,7 +161,6 @@ module.exports = {
isWSLPath,
normalizeWslPath,
writeFile,
writeBinaryFile,
hasJsonExtension,
hasBruExtension,
createDirectory,
Expand Down