2019-09-25 03:16:07 +00:00
|
|
|
export class FileHandler {
|
2019-10-08 23:04:22 +00:00
|
|
|
public static ProcessImages = (fileList: FileList): Promise<[string[], string[]]> => {
|
2019-09-26 01:59:16 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2019-10-08 23:04:22 +00:00
|
|
|
const processedFilenames: string[] = [];
|
|
|
|
const originalFilenames: string[] = [];
|
2019-09-25 03:16:07 +00:00
|
|
|
|
2019-09-26 01:59:16 +00:00
|
|
|
for (let i = 0; i < fileList.length; i++) {
|
|
|
|
const file = fileList[i];
|
|
|
|
const filename = await FileHandler.ProcessImage(file);
|
|
|
|
|
2019-10-08 23:04:22 +00:00
|
|
|
processedFilenames.push(filename);
|
|
|
|
originalFilenames.push(file.name);
|
2019-09-26 01:59:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 23:04:22 +00:00
|
|
|
resolve([ processedFilenames, originalFilenames ]);
|
2019-09-26 01:59:16 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
private static ProcessImage = (file: File): Promise<string> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-09-25 03:16:07 +00:00
|
|
|
const reader = new FileReader();
|
|
|
|
|
2019-09-28 02:29:42 +00:00
|
|
|
reader.onload = (event: ProgressEvent<FileReader>) => {
|
|
|
|
resolve(event.target!.result as string);
|
2019-09-26 01:59:16 +00:00
|
|
|
};
|
2019-09-25 03:16:07 +00:00
|
|
|
|
2019-09-26 01:59:16 +00:00
|
|
|
reader.onerror = reject;
|
2019-09-25 03:16:07 +00:00
|
|
|
|
2019-09-26 01:59:16 +00:00
|
|
|
// Read in the image file as a data URL.
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
});
|
|
|
|
};
|
2019-09-25 03:16:07 +00:00
|
|
|
}
|