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

Fix visualization of large images on angular #15644

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ describe('Data Utils Service Test', () => {
const newWindow = { ...window };
newWindow.document.write = jest.fn();
window.open = jest.fn(() => newWindow);
window.URL.createObjectURL = jest.fn();
// 'JHipster' in base64 is 'SkhpcHN0ZXI='
const data = 'SkhpcHN0ZXI=';
const contentType = 'text/plain';
service.openFile(data, contentType);
expect(newWindow.document.write).toHaveBeenCalledWith(expect.stringContaining('src="data:text/plain;base64,SkhpcHN0ZXI="'));
expect(window.open).toHaveBeenCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,27 @@ export class DataUtils {
*/
openFile(data: string, contentType: string | null | undefined): void {
contentType = contentType ?? '';

const byteCharacters = atob(data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {
type: contentType,
});
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (window.navigator.msSaveOrOpenBlob) {
// To support IE
const byteCharacters = atob(data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], {
type: contentType,
});
window.navigator.msSaveOrOpenBlob(blob);
} else {
// Other browsers
const fileURL = `data:${contentType};base64,${data}`;
const win = window.open();
win?.document.write(
'<iframe src="' +
fileURL +
'" frameborder="0" style="border:0; top:0; left:0; bottom:0; right:0; width:100%; height:100%;" allowfullscreen></iframe>'
);
const fileURL = window.URL.createObjectURL(blob);
const win = window.open(fileURL);
win!.onload = function() {
URL.revokeObjectURL(fileURL);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ describe('Component Tests', () => {

describe('openFile', () => {
it('Should call openFile from DataUtils', () => {
const newWindow = { ...window };
newWindow.document.write = jest.fn();
window.open = jest.fn(() => newWindow);
window.onload = jest.fn(() => newWindow);
window.URL.createObjectURL = jest.fn();
// GIVEN
jest.spyOn(dataUtils, 'openFile');
const fakeContentType = 'fake content type';
Expand Down