-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-manager.ts
114 lines (105 loc) · 3.73 KB
/
file-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { FileTypes } from "../enums";
export class FileManager {
/**
* private input used for opening system window for upload files
*/
private readonly input: HTMLInputElement;
/**
* private input used for opening system window for download files
*/
private readonly link: HTMLAnchorElement;
public constructor() {
this.input = document.createElement("input");
this.input.setAttribute("type", "file");
this.input.setAttribute("value", "files");
this.input.setAttribute("class", "hide");
this.link = document.createElement("a");
this.link.setAttribute("class", "hide");
this.link.setAttribute("href", "");
}
/**
* Save text content into file with specific extensions
*
* @param name file name
* @param text file content
* @param type file {@link FileTypes}. Default value is {@link FileTypes.TXT}
*/
public saveFile(name: string, text: string, type: FileTypes = FileTypes.TXT): void {
this.link.href = URL.createObjectURL(new Blob([text], {type}));
this.link.download = name;
this.link.click();
}
/**
* Save image into file
*
* @param name image name
* @param image image element or path to image
*/
public saveImage(name: string, image: string | HTMLImageElement): void {
this.link.href = typeof image === "string" ? image : image.src;
this.link.download = name;
this.link.click();
}
/**
* Load image using system file picker
*
* @param func loading callback
*/
public loadImage(func: (result: HTMLImageElement, fileName: File) => void): void {
this.input.multiple = false;
this.input.accept = "image/*";
this.input.onchange = (event: any) => {
const files = event.target.files;
if (files.length <= 0) {
return;
}
const reader: FileReader = new FileReader();
reader.onload = () => {
const image = new Image();
image.src = reader.result as string;
func(image, files[0]);
};
reader.readAsDataURL(files[0]);
};
this.input.click();
}
/**
* Load file using system file picker
*
* @param func loading callback
* @param encoding file encoding
*/
public loadFile(func: (result: string, file: File) => void, encoding?: string): void {
this.input.multiple = false;
this.input.onchange = (e: Event) => {
const reader = new FileReader();
const files = (e.target as HTMLInputElement).files;
if (files && files.length > 0) {
reader.onload = () => func(String(reader.result), files[0]);
reader.readAsText(files[0], encoding);
}
};
this.input.click();
}
public loadFiles(func: (files: FileList | null) => void): void {
this.input.multiple = true;
this.input.onchange = (e: Event) => func((e.target as HTMLInputElement).files);
this.input.click();
}
/**
* Load binary file using system file picker
*
* @param func loading callback
*/
public loadBinaryFile(func: (result: ArrayBuffer | string | null, fileName: string) => void): void {
(this.input as any).onchange = (event: InputEvent) => {
const reader = new FileReader();
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
reader.onload = () => func(reader.result, files[0].name);
reader.readAsBinaryString(files[0]);
}
};
this.input.click();
}
}