Skip to content

Commit 3d1604e

Browse files
authored
client(common): openFile function (#31982)
* client(common): openFile function * client(common): rename openFileUtil to openFile
1 parent 1af54ca commit 3d1604e

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

generators/angular/templates/src/main/webapp/app/core/util/data-util.service.ts.ejs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Injectable } from '@angular/core';
2020
import { FormGroup } from '@angular/forms';
2121
import { Observable, Observer } from 'rxjs';
2222

23-
import { formatAsBytes, paddingSize, size, toBase64 } from 'app/shared/jhipster/data-utils';
23+
import { formatAsBytes, openFile, paddingSize, size, toBase64 } from 'app/shared/jhipster/data-utils';
2424

2525
export type FileLoadErrorType = 'not.image' | 'could.not.extract';
2626

@@ -48,22 +48,7 @@ export class DataUtils {
4848
* Method to open file
4949
*/
5050
openFile(data: string, contentType: string | null | undefined): void {
51-
contentType ??= '';
52-
53-
const byteCharacters = atob(data);
54-
const byteNumbers = new Array(byteCharacters.length);
55-
for (let i = 0; i < byteCharacters.length; i++) {
56-
byteNumbers[i] = byteCharacters.codePointAt(i);
57-
}
58-
const byteArray = new Uint8Array(byteNumbers);
59-
const blob = new Blob([byteArray], {
60-
type: contentType,
61-
});
62-
const fileURL = globalThis.URL.createObjectURL(blob);
63-
const win = globalThis.open(fileURL);
64-
win!.onload = function () {
65-
URL.revokeObjectURL(fileURL);
66-
};
51+
openFile(data, contentType);
6752
}
6853

6954
/**

generators/client/generators/common/templates/src/main/webapp/app/shared/jhipster/data-utils.ts.ejs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ export const toBase64 = (file: File, callback: (base64Data: string) => void): vo
4545
};
4646
fileReader.readAsDataURL(file);
4747
}
48+
49+
export const openFile = (data: string, contentType: string | null | undefined): void => {
50+
contentType ??= '';
51+
52+
const byteCharacters = atob(data);
53+
const byteNumbers = new Array(byteCharacters.length);
54+
for (let i = 0; i < byteCharacters.length; i++) {
55+
byteNumbers[i] = byteCharacters.codePointAt(i);
56+
}
57+
const byteArray = new Uint8Array(byteNumbers);
58+
const blob = new Blob([byteArray], {
59+
type: contentType,
60+
});
61+
const fileURL = globalThis.URL.createObjectURL(blob);
62+
const win = globalThis.open(fileURL);
63+
if (win) {
64+
win.onload = () => URL.revokeObjectURL(fileURL);
65+
}
66+
}

generators/vue/templates/src/main/webapp/app/shared/data/data-utils.service.ts.ejs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
See the License for the specific language governing permissions and
1717
limitations under the License.
1818
-%>
19-
import { formatAsBytes, paddingSize, size, toBase64 } from '@/shared/jhipster/data-utils';
19+
import { formatAsBytes, openFile, paddingSize, size, toBase64 } from '@/shared/jhipster/data-utils';
2020

2121
/**
2222
* An composable utility for data.
@@ -43,20 +43,7 @@ const useDataUtils = () => ({
4343
* Method to open file
4444
*/
4545
openFile(contentType, data) {
46-
const byteCharacters = atob(data);
47-
const byteNumbers = new Array(byteCharacters.length);
48-
for (let i = 0; i < byteCharacters.length; i++) {
49-
byteNumbers[i] = byteCharacters.charCodeAt(i);
50-
}
51-
const byteArray = new Uint8Array(byteNumbers);
52-
const blob = new Blob([byteArray], {
53-
type: contentType,
54-
});
55-
const objectURL = URL.createObjectURL(blob);
56-
const win = window.open(objectURL);
57-
if (win) {
58-
win.onload = () => URL.revokeObjectURL(objectURL);
59-
}
46+
openFile(data, contentType);
6047
},
6148

6249
/**

0 commit comments

Comments
 (0)