fix: cap file not appearing

This commit is contained in:
kwaroran
2024-05-24 23:28:19 +09:00
parent 2798e23447
commit 125018bc7e
2 changed files with 18 additions and 17 deletions

View File

@@ -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<boolean> {
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)

View File

@@ -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<Buffer> {
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)})
}