[feat] added account sync option

This commit is contained in:
kwaroran
2023-07-02 20:09:43 +09:00
parent c2e20d1de1
commit ddfaafb7cf
5 changed files with 50 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ import { DataBase } from "./database"
import { hubURL } from "../characterCards"
import localforage from "localforage"
import { alertLogin } from "../alert"
import { getUnpargeables } from "./globalApi"
export class AccountStorage{
auth:string
@@ -21,6 +22,9 @@ export class AccountStorage{
'x-risu-auth': this.auth
}
})
if(da.status === 304){
return key
}
if(da.status === 403){
localStorage.setItem("fallbackRisuToken",await alertLogin())
this.checkAuth()
@@ -66,10 +70,11 @@ export class AccountStorage{
return Buffer.from(ab)
}
async keys():Promise<string[]>{
throw "Error: You cannot list in account. report this to dev if you found this."
let db = get(DataBase)
return getUnpargeables(db, 'pure')
}
async removeItem(key:string){
throw "Error: You remove data in account. report this to dev if you found this."
throw "Error: You cannot remove data in account. report this to dev if you found this."
}
private checkAuth(){
@@ -78,6 +83,7 @@ export class AccountStorage{
if(!this.auth){
db.account = JSON.parse(localStorage.getItem("fallbackRisuToken"))
this.auth = db?.account?.token
db.account.useSync = true
}
}

View File

@@ -2,11 +2,12 @@ import localforage from "localforage"
import { getUnpargeables, isNodeServer, replaceDbResources } from "./globalApi"
import { NodeStorage } from "./nodeStorage"
import { OpfsStorage } from "./opfsStorage"
import { alertConfirm, alertStore } from "../alert"
import { alertConfirm, alertSelect, alertStore } from "../alert"
import { get } from "svelte/store"
import { DataBase } from "./database"
import { AccountStorage } from "./accountStorage"
import { encodeRisuSave } from "./risuSave";
import { language } from "src/lang"
export class AutoStorage{
isAccount:boolean = false
@@ -41,12 +42,28 @@ export class AutoStorage{
if(this.isAccount){
return true
}
if(db.account?.useSync && (localStorage.getItem('accountst') !== 'able')){
if((localStorage.getItem('dosync') === 'sync' || db.account?.useSync) && (localStorage.getItem('accountst') !== 'able')){
getUnpargeables(db)
console.log("migrating")
const keys = await this.realStorage.keys()
let i = 0;
const accountStorage = new AccountStorage()
const a = accountStorage.getItem('database/database.bin')
if(a){
const sel = await alertSelect([language.loadDataFromAccount, language.saveCurrentDataToAccount])
if(sel === "0"){
this.realStorage = accountStorage
alertStore.set({
type: "none",
msg: ""
})
localStorage.setItem('accountst', 'able')
localStorage.setItem('fallbackRisuToken',JSON.stringify(db.account))
this.isAccount = true
return true
}
}
let replaced:{[key:string]:string} = {}
for(const key of keys){

View File

@@ -677,7 +677,7 @@ function getBasename(data:string){
return lasts
}
export function getUnpargeables(db:Database) {
export function getUnpargeables(db:Database, uptype:'basename'|'pure' = 'basename') {
let unpargeable:string[] = []
function addUnparge(data:string){
@@ -687,9 +687,9 @@ export function getUnpargeables(db:Database) {
if(data === ''){
return
}
const bn = getBasename(data)
const bn = uptype === 'basename' ? getBasename(data) : data
if(!unpargeable.includes(bn)){
unpargeable.push(getBasename(data))
unpargeable.push(bn)
}
}