| import { fsApi } from "@/Api/FS"; |
| import { FileInfo } from "./FileInfo"; |
| import { getFileType } from "./FileUtils2"; |
|
|
| export interface IFileParts { |
| basename: string; |
| ext: string; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function checkBadFileType( |
| allAcceptableFileTypes: string[], |
| type: string | undefined, |
| filename: string |
| ): string | undefined { |
| if (type === undefined || allAcceptableFileTypes.indexOf(type) === -1) { |
| return `Error loading "${filename}". Cannot load files of this type.`; |
| } |
| return undefined; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function filesToFileInfos( |
| fileList: File[], |
| isZip: boolean, |
| allAcceptableFileTypes: string[] |
| ): Promise<(FileInfo | string)[] | undefined> { |
| |
|
|
| const fileInfoBatchesPromises: Promise<FileInfo[] | string>[] = []; |
| for (const file of fileList) { |
| const type = getFileType(file.name); |
| if (type === undefined) { |
| fileInfoBatchesPromises.push( |
| Promise.resolve(`Could not determine format of file "${file.name}".`) |
| ); |
| continue; |
| } |
|
|
| const treatAsZip = |
| isZip || type == "MOLMODA" || type == "BIOTITE" || type == "ZIP"; |
|
|
| const fileInfoBatchPromise: Promise<FileInfo[] | string> = new Promise( |
| (resolve, reject) => { |
| const reader = new FileReader(); |
|
|
| reader.onload = (e: ProgressEvent) => { |
| |
| const err = checkBadFileType( |
| allAcceptableFileTypes, |
| type, |
| file.name |
| ); |
| if (err) { |
| resolve(err); |
| return; |
| } |
|
|
| const fileReader = e.target as FileReader; |
| let fileContents = fileReader.result as string; |
| |
| if (!treatAsZip) { |
| fileContents = fileContents.replace(/\r\n/g, "\n"); |
| resolve([ |
| new FileInfo({ |
| name: file.name, |
| |
| contents: fileContents, |
| |
| }), |
| ]); |
| } else { |
| |
| resolve(fsApi.uncompress(fileContents)); |
| } |
| }; |
|
|
| reader.onerror = (e: any) => { |
| reject(e.currentTarget.error.message); |
| }; |
|
|
| if (!treatAsZip) { |
| reader.readAsText(file); |
| } else { |
| |
| reader.readAsBinaryString(file); |
| } |
| } |
| ); |
| fileInfoBatchesPromises.push(fileInfoBatchPromise); |
| } |
|
|
| |
| return Promise.all(fileInfoBatchesPromises).then( |
| (fileInfoBatches: (FileInfo[] | string)[]) => { |
| const flattenedFileInfos: (FileInfo | string)[] = []; |
| for (const fileInfoBatch of fileInfoBatches) { |
| if (typeof fileInfoBatch === "string") { |
| |
| flattenedFileInfos.push(fileInfoBatch); |
| } else { |
| for (const fileInfo of fileInfoBatch) { |
| |
| if ( |
| ["biotite_file.json", "molmoda_file.json"].indexOf( |
| fileInfo.name |
| ) !== -1 |
| ) { |
| fileInfo.name = correctFilenameExt( |
| fileInfo.name, |
| "MOLMODA" |
| ); |
| flattenedFileInfos.push(fileInfo); |
| continue; |
| } |
|
|
| |
| const type = getFileType(fileInfo.name); |
| const err = checkBadFileType( |
| allAcceptableFileTypes, |
| type, |
| fileInfo.name |
| ); |
| if (err) { |
| flattenedFileInfos.push(err); |
| } else { |
| flattenedFileInfos.push(fileInfo); |
| } |
| } |
| } |
| } |
| return flattenedFileInfos; |
| } |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function correctFilenameExt(filename: string, type: string): string { |
| const typeByFilename = getFileType(filename); |
|
|
| |
| if (typeByFilename === type.toUpperCase()) { |
| return filename; |
| } |
|
|
| |
| |
| return filename + "." + type.toLowerCase(); |
| } |
|
|