[feat] android support
This commit is contained in:
@@ -8,11 +8,13 @@ import { DataBase, type Database } from "./database"
|
||||
import { AccountStorage } from "./accountStorage"
|
||||
import { decodeRisuSave, encodeRisuSave } from "./risuSave";
|
||||
import { language } from "src/lang"
|
||||
import { MobileStorage } from "./mobileStorage"
|
||||
import { Capacitor } from "@capacitor/core"
|
||||
|
||||
export class AutoStorage{
|
||||
isAccount:boolean = false
|
||||
|
||||
realStorage:LocalForage|NodeStorage|OpfsStorage|AccountStorage
|
||||
realStorage:LocalForage|NodeStorage|OpfsStorage|AccountStorage|MobileStorage
|
||||
|
||||
async setItem(key:string, value:Uint8Array):Promise<string|null> {
|
||||
await this.Init()
|
||||
@@ -116,6 +118,10 @@ export class AutoStorage{
|
||||
this.isAccount = true
|
||||
return
|
||||
}
|
||||
if(Capacitor.isNativePlatform()){
|
||||
this.realStorage = new MobileStorage()
|
||||
return
|
||||
}
|
||||
if(isNodeServer){
|
||||
console.log("using node storage")
|
||||
this.realStorage = new NodeStorage()
|
||||
|
||||
@@ -23,6 +23,8 @@ import { AutoStorage } from "./autoStorage";
|
||||
import { updateAnimationSpeed } from "../gui/animation";
|
||||
import { updateColorScheme, updateTextTheme } from "../gui/colorscheme";
|
||||
import { saveDbKei } from "../kei/backup";
|
||||
import { Capacitor, CapacitorHttp } from '@capacitor/core';
|
||||
import * as CapFS from '@capacitor/filesystem'
|
||||
|
||||
//@ts-ignore
|
||||
export const isTauri = !!window.__TAURI__
|
||||
@@ -70,6 +72,18 @@ let fileCache:{
|
||||
let pathCache:{[key:string]:string} = {}
|
||||
let checkedPaths:string[] = []
|
||||
|
||||
async function checkCapFileExists(getUriOptions: CapFS.GetUriOptions): Promise<boolean> {
|
||||
try {
|
||||
await CapFS.Filesystem.stat(getUriOptions);
|
||||
return true;
|
||||
} catch (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')){
|
||||
@@ -88,8 +102,20 @@ export async function getFileSrc(loc:string) {
|
||||
}
|
||||
return convertFileSrc(loc)
|
||||
}
|
||||
if(Capacitor.isNativePlatform()){
|
||||
if(!await checkCapFileExists({
|
||||
path: loc,
|
||||
directory: CapFS.Directory.External
|
||||
})){
|
||||
return ''
|
||||
}
|
||||
const uri = await CapFS.Filesystem.getUri({
|
||||
path: loc,
|
||||
directory: CapFS.Directory.External
|
||||
})
|
||||
return Capacitor.convertFileSrc(uri.uri)
|
||||
}
|
||||
try {
|
||||
|
||||
if(forageStorage.isAccount && loc.startsWith('assets')){
|
||||
return hubURL + `/rs/` + loc
|
||||
}
|
||||
@@ -194,6 +220,16 @@ export async function saveAsset(data:Uint8Array, customId:string = '', fileName:
|
||||
await writeBinaryFile(`assets/${id}.${fileExtension}`, data ,{dir: BaseDirectory.AppData})
|
||||
return `assets/${id}.${fileExtension}`
|
||||
}
|
||||
else if(Capacitor.isNativePlatform()){
|
||||
const path = `assets/${id}.${fileExtension}`
|
||||
await CapFS.Filesystem.writeFile({
|
||||
path: path,
|
||||
data: Buffer.from(data).toString('base64'),
|
||||
directory: CapFS.Directory.External,
|
||||
recursive: true,
|
||||
})
|
||||
return path
|
||||
}
|
||||
else{
|
||||
let form = `assets/${id}.${fileExtension}`
|
||||
const replacer = await forageStorage.setItem(form, data)
|
||||
@@ -403,7 +439,7 @@ export async function loadData() {
|
||||
if(isDriverMode){
|
||||
return
|
||||
}
|
||||
if(navigator.serviceWorker){
|
||||
if(navigator.serviceWorker && (!Capacitor.isNativePlatform())){
|
||||
usingSw = true
|
||||
await navigator.serviceWorker.register("/sw.js", {
|
||||
scope: "/"
|
||||
@@ -632,6 +668,42 @@ export async function globalFetch(url:string, arg:{
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (Capacitor.isNativePlatform()){
|
||||
const body = arg.body
|
||||
const headers = arg.headers ?? {}
|
||||
if(arg.body instanceof URLSearchParams){
|
||||
if(!headers["Content-Type"]){
|
||||
headers["Content-Type"] = `application/x-www-form-urlencoded`
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(!headers["Content-Type"]){
|
||||
headers["Content-Type"] = `application/json`
|
||||
}
|
||||
}
|
||||
const res = await CapacitorHttp.request({
|
||||
url: url,
|
||||
method: method,
|
||||
headers: headers,
|
||||
data: body,
|
||||
responseType: arg.rawResponse ? 'arraybuffer' : 'json'
|
||||
})
|
||||
|
||||
if(arg.rawResponse){
|
||||
addFetchLog("Uint8Array Response", true)
|
||||
return {
|
||||
ok: true,
|
||||
data: new Uint8Array(res.data as ArrayBuffer),
|
||||
headers: res.headers
|
||||
}
|
||||
}
|
||||
addFetchLog(res.data, true)
|
||||
return {
|
||||
ok: true,
|
||||
data: res.data,
|
||||
headers: res.headers
|
||||
}
|
||||
}
|
||||
else{
|
||||
try {
|
||||
let body:any
|
||||
|
||||
43
src/ts/storage/mobileStorage.ts
Normal file
43
src/ts/storage/mobileStorage.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import * as CapFS from '@capacitor/filesystem'
|
||||
import { get } from 'svelte/store';
|
||||
import { DataBase } from './database';
|
||||
import { getUnpargeables } from './globalApi';
|
||||
|
||||
export class MobileStorage{
|
||||
async setItem(key:string, value:Uint8Array) {
|
||||
await CapFS.Filesystem.writeFile({
|
||||
path: key,
|
||||
data: Buffer.from(value).toString('base64'),
|
||||
directory: CapFS.Directory.External,
|
||||
recursive: true,
|
||||
})
|
||||
}
|
||||
async getItem(key:string):Promise<Buffer> {
|
||||
try {
|
||||
const b64 = await CapFS.Filesystem.readFile({
|
||||
path: key,
|
||||
directory: CapFS.Directory.External,
|
||||
})
|
||||
return Buffer.from(b64.data as string, 'base64')
|
||||
} catch (error) {
|
||||
if(error){
|
||||
if(error.message.includes(`does not exist`)){
|
||||
return null
|
||||
}
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
async keys():Promise<string[]>{
|
||||
let db = get(DataBase)
|
||||
return getUnpargeables(db, 'pure')
|
||||
}
|
||||
async removeItem(key:string){
|
||||
await CapFS.Filesystem.deleteFile({
|
||||
path: key,
|
||||
directory: CapFS.Directory.External,
|
||||
})
|
||||
}
|
||||
|
||||
listItem = this.keys
|
||||
}
|
||||
Reference in New Issue
Block a user