diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index e98386c0..8e484552 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -32,6 +32,7 @@ import { language } from "src/lang"; import { startObserveDom } from "../observer"; import { removeDefaultHandler } from "src/main"; import { updateGuisize } from "../gui/guisize"; +import { encodeCapKeySafe } from "./mobileStorage"; //@ts-ignore export const isTauri = !!window.__TAURI__ @@ -87,16 +88,16 @@ let checkedPaths:string[] = [] async function checkCapFileExists(getUriOptions: CapFS.GetUriOptions): Promise { try { - await CapFS.Filesystem.stat(getUriOptions); - return true; + await CapFS.Filesystem.stat(getUriOptions); + return true; } catch (checkDirException) { - if (checkDirException.message === 'File does not exist') { - return false; - } else { - throw checkDirException; - } + if (checkDirException.message === 'File does not exist') { + return false; + } else { + throw checkDirException; + } } - } +} export async function getFileSrc(loc:string) { if(isTauri){ if(loc.startsWith('assets')){ @@ -120,13 +121,13 @@ export async function getFileSrc(loc:string) { } if(Capacitor.isNativePlatform()){ if(!await checkCapFileExists({ - path: loc, + path: encodeCapKeySafe(loc), directory: CapFS.Directory.External })){ return '' } const uri = await CapFS.Filesystem.getUri({ - path: loc, + path: encodeCapKeySafe(loc), directory: CapFS.Directory.External }) return Capacitor.convertFileSrc(uri.uri) diff --git a/src/ts/storage/mobileStorage.ts b/src/ts/storage/mobileStorage.ts index da0c93f5..5fca9a82 100644 --- a/src/ts/storage/mobileStorage.ts +++ b/src/ts/storage/mobileStorage.ts @@ -1,10 +1,10 @@ import * as CapFS from '@capacitor/filesystem' -function encodeKeySafe(oldKey:string){ +export function encodeCapKeySafe(oldKey:string){ return oldKey.replace(/_/g, '__').replace(/\//g, '_s').replace(/\./g, '_d').replace(/\$/g, '_t').replace(/-/g, '_h').replace(/:/g, '_c') + '.bin' } -function decodeKeySafe(newKey:string){ +export function decodeCapKeySafe(newKey:string){ newKey = newKey.substring(0, newKey.length-4) return newKey.replace(/_c/g, ':').replace(/_h/g, '-').replace(/_t/g, '$').replace(/_d/g, '.').replace(/_s/g, '/').replace(/__/g, '_') } @@ -13,7 +13,7 @@ function decodeKeySafe(newKey:string){ export class MobileStorage{ async setItem(key:string, value:Uint8Array) { await CapFS.Filesystem.writeFile({ - path: encodeKeySafe(key), + path: encodeCapKeySafe(key), data: Buffer.from(value).toString('base64'), directory: CapFS.Directory.External, recursive: true, @@ -22,7 +22,7 @@ export class MobileStorage{ async getItem(key:string):Promise { try { const b64 = await CapFS.Filesystem.readFile({ - path: encodeKeySafe(key), + path: encodeCapKeySafe(key), directory: CapFS.Directory.External, }) return Buffer.from(b64.data as string, 'base64') @@ -41,11 +41,11 @@ export class MobileStorage{ directory: CapFS.Directory.External, }) - return files.files.map(f=>decodeKeySafe(f.name)) + return files.files.map(f=>decodeCapKeySafe(f.name)) } async removeItem(key:string){ await CapFS.Filesystem.deleteFile({ - path: encodeKeySafe(key), + path: encodeCapKeySafe(key), directory: CapFS.Directory.External, }) } @@ -78,7 +78,7 @@ export async function capStorageInvestigation(){ }) for(const file of files.files){ - const key = decodeKeySafe(file.name) + const key = decodeCapKeySafe(file.name) const size = file.size investResults.push({key, size: byteLengthToString(size)}) }