File size: 6,505 Bytes
71174bc | 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | import { fsApi } from "@/Api/FS";
import { FileInfo } from "./FileInfo";
import { getFileType } from "./FileUtils2";
export interface IFileParts {
basename: string;
ext: string;
}
/**
* Checks if a given file type is acceptable.
*
* @param {string[]} allAcceptableFileTypes The file types to check against.
* @param {string | undefined} type The file type to check.
* @param {string} filename The filename to check.
* @returns {string | undefined} An error message if the file type is not
* acceptable. If acceptable, undefined.
*/
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;
}
/**
* Convert a list of File objects to type FileInfo.
*
* @param {File[]} fileList The list of files to convert, as
* loaded through <input>.
* @param {boolean} isZip Whether the files are zip files.
* @param {string[]} allAcceptableFileTypes A list of acceptable file types.
* @returns {Promise<FileInfo | string | undefined>} A promise that resolves to the converted file.
*/
export function filesToFileInfos(
fileList: File[],
isZip: boolean,
allAcceptableFileTypes: string[]
): Promise<(FileInfo | string)[] | undefined> {
// Type is file extension, uppercase.
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) => {
// If it's not an acceptable file type, abort effort.
const err = checkBadFileType(
allAcceptableFileTypes,
type,
file.name
);
if (err) {
resolve(err);
return;
}
const fileReader = e.target as FileReader;
let fileContents = fileReader.result as string;
// let fileContentsPromise: Promise<FileInfo[]>;
if (!treatAsZip) {
fileContents = fileContents.replace(/\r\n/g, "\n");
resolve([
new FileInfo({
name: file.name,
// size: file.size,
contents: fileContents,
// type: type,
}),
]);
} else {
// It's a zip file (or a molmoda file).
resolve(fsApi.uncompress(fileContents)); // , "molmoda_file.json");
}
};
reader.onerror = (e: any) => {
reject(e.currentTarget.error.message);
};
if (!treatAsZip) {
reader.readAsText(file);
} else {
// It's a zip file.
reader.readAsBinaryString(file);
}
}
);
fileInfoBatchesPromises.push(fileInfoBatchPromise);
}
// Flatten the array of arrays of files into a single array of files.
return Promise.all(fileInfoBatchesPromises).then(
(fileInfoBatches: (FileInfo[] | string)[]) => {
const flattenedFileInfos: (FileInfo | string)[] = [];
for (const fileInfoBatch of fileInfoBatches) {
if (typeof fileInfoBatch === "string") {
// An error message. Add without modification.
flattenedFileInfos.push(fileInfoBatch);
} else {
for (const fileInfo of fileInfoBatch) {
// Special exception for molmoda files...
if (
["biotite_file.json", "molmoda_file.json"].indexOf(
fileInfo.name
) !== -1
) {
fileInfo.name = correctFilenameExt(
fileInfo.name,
"MOLMODA"
);
flattenedFileInfos.push(fileInfo);
continue;
}
// If it's not an acceptable file type, abort effort.
const type = getFileType(fileInfo.name);
const err = checkBadFileType(
allAcceptableFileTypes,
type,
fileInfo.name
);
if (err) {
flattenedFileInfos.push(err);
} else {
flattenedFileInfos.push(fileInfo);
}
}
}
}
return flattenedFileInfos;
}
);
}
/**
* Given a filename and format type, update the filename so the extension
* reflects the correct type.
*
* @param {string} filename The filename to update.
* @param {string} type The type to update to.
* @returns {string} . The updated filename.
*/
export function correctFilenameExt(filename: string, type: string): string {
const typeByFilename = getFileType(filename);
// If the extension is already correct, return the filename
if (typeByFilename === type.toUpperCase()) {
return filename;
}
// If the extension is not correct, return the filename with the correct
// extension
return filename + "." + type.toLowerCase();
}
|