| import { FileInfo } from "@/FileSystem/FileInfo"; |
| import { dynamicImports } from "../DynamicImports"; |
| import { DataFormat, IData } from "./FSInterfaces"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| export function saveTxt(params: FileInfo): Promise<any> { |
| |
| |
|
|
| |
| const validCompressedExts = [".zip", ".molmoda", ".biotite"]; |
| if ( |
| params.compressedName && |
| !validCompressedExts.some((ext) => |
| params.compressedName?.toLowerCase().endsWith(ext) |
| ) |
| ) { |
| params.compressedName += ".zip"; |
| } |
|
|
| if (params.compressedName) { |
| return saveZipWithTxtFiles(params.compressedName, [params]); |
| } |
|
|
| |
| return dynamicImports.fileSaver.module.then((fileSaver: any) => { |
| const blob = new Blob([params.contents as string], { |
| type: "text/plain;charset=utf-8", |
| }); |
| fileSaver.saveAs(blob, params.name); |
| return; |
| }); |
|
|
| |
| |
| |
| |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function saveSvg(params: FileInfo): Promise<any> { |
| |
| if (!params.name.toLowerCase().endsWith(".svg")) { |
| params.name += ".svg"; |
| } |
|
|
| |
| |
| const validCompressedExts = [".zip", ".molmoda", ".biotite"]; |
| if ( |
| params.compressedName && |
| !validCompressedExts.some((ext) => |
| params.compressedName?.toLowerCase().endsWith(ext) |
| ) |
| ) { |
| params.compressedName += ".zip"; |
| } |
|
|
| if (params.compressedName) { |
| |
| |
| |
| |
| const svgFileInfoForZip = new FileInfo({ |
| name: params.name, |
| contents: params.contents, |
| |
| }); |
| return saveZipWithTxtFiles(params.compressedName, [svgFileInfoForZip]); |
| } |
|
|
| |
| return dynamicImports.fileSaver.module.then((fileSaver: any) => { |
| const blob = new Blob([params.contents as string], { |
| type: "image/svg+xml", |
| }); |
| fileSaver.saveAs(blob, params.name); |
| return; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function createZipBlob(files: FileInfo[]): Promise<Blob> { |
| return dynamicImports.jsZip.module.then((JSZip: any) => { |
| const zip = new JSZip(); |
| files.forEach((file) => { |
| zip.file(file.name, file.contents, { |
| compression: "DEFLATE", |
| |
| |
| |
| |
| |
| }); |
| }); |
| return zip.generateAsync({ type: "blob" }); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function saveZipWithTxtFiles( |
| compressedName: string, |
| files: FileInfo[] |
| ): Promise<any> { |
| |
| |
| const makeZipPromise = createZipBlob(files); |
|
|
| const promises: Promise<any>[] = [ |
| dynamicImports.fileSaver.module, |
| makeZipPromise, |
| ]; |
|
|
| return Promise.all(promises).then((payload: any[]) => { |
| const fileSaver = payload[0]; |
| const zipBlob = payload[1]; |
| fileSaver.saveAs(zipBlob, compressedName); |
| return; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function _dataURIToBlob(dataURI: string): Blob { |
| |
|
|
| |
| |
| const byteString = atob(dataURI.split(",")[1]); |
|
|
| |
| const mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0]; |
|
|
| |
| const ab = new ArrayBuffer(byteString.length); |
|
|
| |
| const ia = new Uint8Array(ab); |
|
|
| |
| for (let i = 0; i < byteString.length; i++) { |
| ia[i] = byteString.charCodeAt(i); |
| } |
|
|
| |
| return new Blob([ab], { type: mimeString }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function savePngUri(fileName: string, pngUri: string) { |
| dynamicImports.fileSaver.module |
| .then((fileSaver) => { |
| const blob = _dataURIToBlob(pngUri); |
| fileSaver.saveAs(blob, fileName); |
| return; |
| }) |
| .catch((err: any) => { |
| throw err; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function uncompress(s: string): Promise<FileInfo[]> { |
| const getZipObjPromise = dynamicImports.jsZip.module.then((JSZip) => { |
| return JSZip.loadAsync(s); |
| }); |
|
|
| |
| |
| |
| |
| |
| let fileNames: string[]; |
| |
| return getZipObjPromise |
| .then((zip: any) => { |
| fileNames = Object.keys(zip.files); |
| |
| const promises = fileNames.map((f) => zip.files[f].async("string")); |
| return Promise.all(promises); |
| }) |
| .then((fileContents: string[]) => { |
| const fileInfos: FileInfo[] = []; |
| for (let i = 0; i < fileNames.length; i++) { |
| const fileName = fileNames[i]; |
| if (fileName.startsWith("__MACOSX")) { |
| continue; |
| } |
| if (fileName.startsWith(".")) { |
| continue; |
| } |
|
|
| const contents = fileContents[i]; |
|
|
| fileInfos.push( |
| new FileInfo({ |
| name: fileName.split("/").pop() as string, |
| |
| |
| |
| contents: contents, |
| |
| }) |
| ); |
| } |
| return fileInfos; |
| }); |
| |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function saveData( |
| data: IData, |
| filename: string, |
| format: DataFormat | string |
| ) { |
| if (format === DataFormat.JSON) { |
| saveTxt( |
| new FileInfo({ |
| name: filename, |
| contents: JSON.stringify(data.rows, null, 2), |
| }) |
| ); |
| } else { |
| Promise.all([ |
| dynamicImports.exceljs.module, |
| dynamicImports.fileSaver.module, |
| ]) |
| .then(async ([ExcelJS, fileSaver]) => { |
| const workbook = new ExcelJS.Workbook(); |
| const worksheet = workbook.addWorksheet("Sheet1"); |
|
|
| worksheet.columns = data.headers.map((header) => ({ |
| header: header, |
| key: header, |
| })); |
|
|
| worksheet.addRows(data.rows); |
|
|
| const fileExtension = filename.split(".").pop()?.toLowerCase(); |
| let buffer: ArrayBuffer; |
| let blobType: string; |
|
|
| if (fileExtension === "xlsx") { |
| buffer = await workbook.xlsx.writeBuffer(); |
| blobType = |
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; |
| } else if (fileExtension === "csv") { |
| buffer = await workbook.csv.writeBuffer(); |
| blobType = "text/csv;charset=utf-8;"; |
| } else { |
| |
| if (format === DataFormat.XLSX) { |
| buffer = await workbook.xlsx.writeBuffer(); |
| blobType = |
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; |
| } else if (format === DataFormat.CSV) { |
| buffer = await workbook.csv.writeBuffer(); |
| blobType = "text/csv;charset=utf-8;"; |
| } else { |
| console.error("Unsupported format for saving:", format); |
| return; |
| } |
| } |
| const blob = new Blob([buffer], { type: blobType }); |
| fileSaver.saveAs(blob, filename); |
| return; |
| }) |
| .catch((err: any) => { |
| throw err; |
| }); |
| } |
| } |
|
|