[feat] added account sync option
This commit is contained in:
@@ -319,6 +319,10 @@ export const languageEnglish = {
|
||||
notLoggedIn: "Not Logged in to Risu Account",
|
||||
googleDriveInfo: "Connect to google drive to sync your data.",
|
||||
googleDriveConnection: "Google Drive Connection",
|
||||
googleDriveConnected: "Google Drive Connected"
|
||||
|
||||
googleDriveConnected: "Google Drive Connected",
|
||||
SaveDataInAccount: "Save Data in Account",
|
||||
dataSavingInAccount: "Saving Data in Account",
|
||||
logout: "Logout",
|
||||
loadDataFromAccount: "Load Data from Account",
|
||||
saveCurrentDataToAccount: "Save Current Data to Account"
|
||||
}
|
||||
@@ -5,6 +5,8 @@
|
||||
import { loadRisuAccountData, saveRisuAccountData } from "src/ts/drive/accounter";
|
||||
import { DataBase } from "src/ts/storage/database";
|
||||
import Check from "src/lib/Others/Check.svelte";
|
||||
import { alertSelect } from "src/ts/alert";
|
||||
import { isTauri } from "src/ts/storage/globalApi";
|
||||
let openIframe = false
|
||||
let openIframeURL = ''
|
||||
let popup:Window = null
|
||||
@@ -53,14 +55,23 @@
|
||||
<h1 class="text-3xl font-black min-w-0">Risu Account{#if $DataBase.account}
|
||||
<button class="bg-selected p-1 text-sm font-light rounded-md hover:bg-green-500 transition-colors float-right" on:click={async () => {
|
||||
$DataBase.account = undefined
|
||||
}}>Logout</button>
|
||||
}}>{language.logout}</button>
|
||||
{/if}</h1>
|
||||
</div>
|
||||
{#if $DataBase.account}
|
||||
<span class="mb-4 text-gray-400">ID: {$DataBase.account.id}</span>
|
||||
{#if $DataBase.useExperimental && localStorage.getItem('ac_flag!') === "able"}
|
||||
{#if $DataBase.useExperimental && (!isTauri)}
|
||||
<div class="flex items-center mt-2">
|
||||
<Check bind:check={$DataBase.account.useSync} name='Sync Data between Devices'/>
|
||||
{#if $DataBase.account.useSync}
|
||||
<span>{language.dataSavingInAccount}</span>
|
||||
{:else}
|
||||
<Check check={false} name={language.SaveDataInAccount} onChange={(v) => {
|
||||
if(v){
|
||||
localStorage.setItem('dosync', 'sync')
|
||||
location.reload()
|
||||
}
|
||||
}}/>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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){
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user