feat: add cap storage utilitys
This commit is contained in:
@@ -32,4 +32,6 @@
|
|||||||
<!-- Permissions -->
|
<!-- Permissions -->
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -10,6 +10,13 @@
|
|||||||
import OptionInput from "src/lib/UI/GUI/OptionInput.svelte";
|
import OptionInput from "src/lib/UI/GUI/OptionInput.svelte";
|
||||||
import Help from "src/lib/Others/Help.svelte";
|
import Help from "src/lib/Others/Help.svelte";
|
||||||
import { installPython } from "src/ts/process/models/local";
|
import { installPython } from "src/ts/process/models/local";
|
||||||
|
import { Capacitor } from "@capacitor/core";
|
||||||
|
import { capStorageInvestigation } from "src/ts/storage/mobileStorage";
|
||||||
|
|
||||||
|
let estaStorage:{
|
||||||
|
key:string,
|
||||||
|
size:string,
|
||||||
|
}[] = []
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<h2 class="text-2xl font-bold mt-2">{language.advancedSettings}</h2>
|
<h2 class="text-2xl font-bold mt-2">{language.advancedSettings}</h2>
|
||||||
@@ -151,6 +158,26 @@
|
|||||||
class="drop-shadow-lg p-3 border-borderc border-solid mt-6 flex justify-center items-center ml-2 mr-2 border-1 hover:bg-selected text-sm">
|
class="drop-shadow-lg p-3 border-borderc border-solid mt-6 flex justify-center items-center ml-2 mr-2 border-1 hover:bg-selected text-sm">
|
||||||
{language.ShowLog}
|
{language.ShowLog}
|
||||||
</button>
|
</button>
|
||||||
|
{#if Capacitor.isNativePlatform()}
|
||||||
|
<button
|
||||||
|
on:click={async () => {
|
||||||
|
estaStorage = await capStorageInvestigation()
|
||||||
|
}}
|
||||||
|
class="drop-shadow-lg p-3 border-borderc border-solid mt-6 flex justify-center items-center ml-2 mr-2 border-1 hover:bg-selected text-sm">
|
||||||
|
Investigate Storage
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{#if estaStorage.length > 0}
|
||||||
|
<div class="mt-4 flex flex-col w-full p-2">
|
||||||
|
{#each estaStorage as item}
|
||||||
|
<div class="flex p-2 rounded-md items-center justify-between">
|
||||||
|
<span class="text-textcolor">{item.key}</span>
|
||||||
|
<span class="text-textcolor ml-2">{item.size}</span>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
{#if $DataBase.tpo}
|
{#if $DataBase.tpo}
|
||||||
<button
|
<button
|
||||||
on:click={async () => {
|
on:click={async () => {
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
import * as CapFS from '@capacitor/filesystem'
|
import * as CapFS from '@capacitor/filesystem'
|
||||||
import { get } from 'svelte/store';
|
|
||||||
import { DataBase } from './database';
|
function encodeKeySafe(oldKey:string){
|
||||||
import { getUnpargeables } from './globalApi';
|
return oldKey.replace(/_/g, '__').replace(/\//g, '_s').replace(/\./g, '_d').replace(/\$/g, '_t').replace(/-/g, '_h').replace(/:/g, '_c') + '.bin'
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeKeySafe(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, '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export class MobileStorage{
|
export class MobileStorage{
|
||||||
async setItem(key:string, value:Uint8Array) {
|
async setItem(key:string, value:Uint8Array) {
|
||||||
await CapFS.Filesystem.writeFile({
|
await CapFS.Filesystem.writeFile({
|
||||||
path: key,
|
path: encodeKeySafe(key),
|
||||||
data: Buffer.from(value).toString('base64'),
|
data: Buffer.from(value).toString('base64'),
|
||||||
directory: CapFS.Directory.External,
|
directory: CapFS.Directory.External,
|
||||||
recursive: true,
|
recursive: true,
|
||||||
@@ -15,7 +22,7 @@ export class MobileStorage{
|
|||||||
async getItem(key:string):Promise<Buffer> {
|
async getItem(key:string):Promise<Buffer> {
|
||||||
try {
|
try {
|
||||||
const b64 = await CapFS.Filesystem.readFile({
|
const b64 = await CapFS.Filesystem.readFile({
|
||||||
path: key,
|
path: encodeKeySafe(key),
|
||||||
directory: CapFS.Directory.External,
|
directory: CapFS.Directory.External,
|
||||||
})
|
})
|
||||||
return Buffer.from(b64.data as string, 'base64')
|
return Buffer.from(b64.data as string, 'base64')
|
||||||
@@ -29,15 +36,58 @@ export class MobileStorage{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async keys():Promise<string[]>{
|
async keys():Promise<string[]>{
|
||||||
let db = get(DataBase)
|
const files = await CapFS.Filesystem.readdir({
|
||||||
return getUnpargeables(db, 'pure')
|
path: '',
|
||||||
|
directory: CapFS.Directory.External,
|
||||||
|
})
|
||||||
|
|
||||||
|
return files.files.map(f=>decodeKeySafe(f.name))
|
||||||
}
|
}
|
||||||
async removeItem(key:string){
|
async removeItem(key:string){
|
||||||
await CapFS.Filesystem.deleteFile({
|
await CapFS.Filesystem.deleteFile({
|
||||||
path: key,
|
path: encodeKeySafe(key),
|
||||||
directory: CapFS.Directory.External,
|
directory: CapFS.Directory.External,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
listItem = this.keys
|
listItem = this.keys
|
||||||
|
}
|
||||||
|
|
||||||
|
function byteLengthToString(byteLength:number):string{
|
||||||
|
if(byteLength < 1024){
|
||||||
|
return byteLength + ' B'
|
||||||
|
}
|
||||||
|
if(byteLength < 1024*1024){
|
||||||
|
return (byteLength/1024).toFixed(2) + ' KB'
|
||||||
|
}
|
||||||
|
if(byteLength < 1024*1024*1024){
|
||||||
|
return (byteLength/1024/1024).toFixed(2) + ' MB'
|
||||||
|
}
|
||||||
|
return (byteLength/1024/1024/1024).toFixed(2) + ' GB'
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function capStorageInvestigation(){
|
||||||
|
const investResults:{
|
||||||
|
key:string,
|
||||||
|
size:string,
|
||||||
|
}[] = []
|
||||||
|
|
||||||
|
const files = await CapFS.Filesystem.readdir({
|
||||||
|
path: '',
|
||||||
|
directory: CapFS.Directory.External,
|
||||||
|
})
|
||||||
|
|
||||||
|
for(const file of files.files){
|
||||||
|
const key = decodeKeySafe(file.name)
|
||||||
|
const size = file.size
|
||||||
|
investResults.push({key, size: byteLengthToString(size)})
|
||||||
|
}
|
||||||
|
|
||||||
|
const estimated = await navigator.storage.estimate()
|
||||||
|
|
||||||
|
if(estimated){
|
||||||
|
investResults.push({key:'webstorage', size:byteLengthToString(estimated.usage)})
|
||||||
|
}
|
||||||
|
|
||||||
|
return investResults
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user