[feat] functional account storage
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { language } from "src/lang";
|
||||
import Help from "src/lib/Others/Help.svelte";
|
||||
import { hubURL } from "src/ts/characterCards";
|
||||
import { getCharImage, selectUserImg } from "src/ts/characters";
|
||||
import { loadRisuAccountData, saveRisuAccountData } from "src/ts/drive/accounter";
|
||||
import { checkDriver } from "src/ts/drive/drive";
|
||||
import { DataBase } from "src/ts/storage/database";
|
||||
import Check from "src/lib/Others/Check.svelte";
|
||||
let openIframe = false
|
||||
let openIframeURL = ''
|
||||
let popup:Window = null
|
||||
@@ -59,22 +58,11 @@
|
||||
</div>
|
||||
{#if $DataBase.account}
|
||||
<span class="mb-4 text-gray-400">ID: {$DataBase.account.id}</span>
|
||||
<!-- {#if $DataBase.useExperimental}
|
||||
|
||||
<h1 class="text-xl font-bold mt-2">{language.googleDriveConnection} <Help key="experimental"/></h1>
|
||||
{#if !$DataBase.account.data.refresh_token}
|
||||
<span class="text-sm font-light mb-2 text-gray-400">{language.googleDriveInfo}</span>
|
||||
<button class="bg-selected p-2 rounded-md hover:bg-green-500 transition-colors" on:click={async () => {
|
||||
if((!popup) || popup.closed){
|
||||
popup = window.open(await checkDriver('reftoken'))
|
||||
}
|
||||
}}>
|
||||
Connect to Google Drive
|
||||
</button>
|
||||
{:else}
|
||||
<span class="text-sm font-light mb-2 text-gray-400">{language.googleDriveConnected}</span>
|
||||
{/if}
|
||||
{/if} -->
|
||||
{#if $DataBase.useExperimental && localStorage.getItem('ac_flag!') === "able"}
|
||||
<div class="flex items-center mt-2">
|
||||
<Check bind:check={$DataBase.account.useSync} name='Sync Data between Devices'/>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{:else}
|
||||
<span>{language.notLoggedIn}</span>
|
||||
|
||||
@@ -275,8 +275,12 @@ async function loadDrive(ACCESS_TOKEN:string, mode: 'backup'|'sync'):Promise<voi
|
||||
const files:DriveFile[] = await getFilesInFolder(ACCESS_TOKEN)
|
||||
let foragekeys:string[] = []
|
||||
let loadedForageKeys = false
|
||||
let db = get(DataBase)
|
||||
|
||||
async function checkImageExists(images:string) {
|
||||
if(db?.account?.useSync){
|
||||
return false
|
||||
}
|
||||
if(isTauri){
|
||||
return await exists(`assets/` + images, {dir: BaseDirectory.AppData})
|
||||
}
|
||||
|
||||
@@ -1,93 +1,73 @@
|
||||
import { get } from "svelte/store"
|
||||
import { DataBase } from "./database"
|
||||
import { hubURL } from "../characterCards"
|
||||
import localforage from "localforage"
|
||||
|
||||
export class AccountStorage{
|
||||
auth:string
|
||||
usingSync:boolean
|
||||
|
||||
async setItem(key:string, value:Uint8Array) {
|
||||
await this.checkAuth()
|
||||
const da = await fetch(hubURL + '/api/account/write', {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
content: Buffer.from(value).toString('base64')
|
||||
}),
|
||||
body: value,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'file-path': Buffer.from(key, 'utf-8').toString('hex'),
|
||||
'risu-auth': this.auth
|
||||
'x-risu-key': key,
|
||||
'x-risu-auth': this.auth ?? sessionStorage.getItem("fallbackRisuToken")
|
||||
}
|
||||
})
|
||||
if(da.status < 200 || da.status >= 300){
|
||||
throw "setItem Error"
|
||||
}
|
||||
const data = await da.json()
|
||||
if(data.error){
|
||||
throw data.error
|
||||
throw await da.text()
|
||||
}
|
||||
return await da.text()
|
||||
}
|
||||
async getItem(key:string):Promise<Buffer> {
|
||||
if(key.startsWith('assets/')){
|
||||
return Buffer.from(await (await fetch(`${hubURL}/resource/` + key)).arrayBuffer())
|
||||
}
|
||||
await this.checkAuth()
|
||||
if(key.startsWith('assets/')){
|
||||
const k:ArrayBuffer = await localforage.getItem(key)
|
||||
if(k){
|
||||
return Buffer.from(k)
|
||||
}
|
||||
}
|
||||
const da = await fetch(hubURL + '/api/account/read', {
|
||||
method: "GET",
|
||||
headers: {
|
||||
'file-path': Buffer.from(key, 'utf-8').toString('hex'),
|
||||
'risu-auth': this.auth
|
||||
'x-risu-key': key,
|
||||
'x-risu-auth': this.auth ?? sessionStorage.getItem("fallbackRisuToken")
|
||||
}
|
||||
})
|
||||
const data = await da.json()
|
||||
if(da.status < 200 || da.status >= 300){
|
||||
throw "getItem Error"
|
||||
throw await da.text()
|
||||
}
|
||||
if(data.error){
|
||||
throw data.error
|
||||
}
|
||||
if(data.content === null){
|
||||
if(da.status === 204){
|
||||
return null
|
||||
}
|
||||
return Buffer.from(data.content, 'base64')
|
||||
const ab = await da.arrayBuffer()
|
||||
if(key.startsWith('assets/')){
|
||||
await localforage.setItem(key, ab)
|
||||
}
|
||||
return Buffer.from(ab)
|
||||
}
|
||||
async keys():Promise<string[]>{
|
||||
await this.checkAuth()
|
||||
const da = await fetch(hubURL + '/api/account/list', {
|
||||
method: "GET",
|
||||
headers:{
|
||||
'risu-auth': this.auth
|
||||
}
|
||||
})
|
||||
const data = await da.json()
|
||||
if(da.status < 200 || da.status >= 300){
|
||||
throw "listItem Error"
|
||||
}
|
||||
if(data.error){
|
||||
throw data.error
|
||||
}
|
||||
return data.content
|
||||
throw "Error: You cannot list in account. report this to dev if you found this."
|
||||
}
|
||||
async removeItem(key:string){
|
||||
await this.checkAuth()
|
||||
const da = await fetch(hubURL + '/api/account/remove', {
|
||||
method: "GET",
|
||||
headers: {
|
||||
'file-path': Buffer.from(key, 'utf-8').toString('hex'),
|
||||
'risu-auth': this.auth
|
||||
}
|
||||
})
|
||||
if(da.status < 200 || da.status >= 300){
|
||||
throw "removeItem Error"
|
||||
}
|
||||
const data = await da.json()
|
||||
if(data.error){
|
||||
throw data.error
|
||||
}
|
||||
throw "Error: You remove data in account. report this to dev if you found this."
|
||||
}
|
||||
|
||||
private async checkAuth(){
|
||||
this.auth = get(DataBase)?.account?.token
|
||||
|
||||
const db = get(DataBase)
|
||||
this.auth = db?.account?.token
|
||||
if(!this.auth){
|
||||
db.account = {
|
||||
id: "",
|
||||
token: sessionStorage.getItem("fallbackRisuToken"),
|
||||
useSync: true,
|
||||
data: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,14 +3,22 @@ import { isNodeServer } from "./globalApi"
|
||||
import { NodeStorage } from "./nodeStorage"
|
||||
import { OpfsStorage } from "./opfsStorage"
|
||||
import { alertConfirm, alertStore } from "../alert"
|
||||
import { get } from "svelte/store"
|
||||
import { DataBase } from "./database"
|
||||
import { AccountStorage } from "./accountStorage"
|
||||
|
||||
export class AutoStorage{
|
||||
isAccount:boolean = false
|
||||
|
||||
realStorage:LocalForage|NodeStorage|OpfsStorage
|
||||
realStorage:LocalForage|NodeStorage|OpfsStorage|AccountStorage
|
||||
|
||||
async setItem(key:string, value:Uint8Array) {
|
||||
async setItem(key:string, value:Uint8Array):Promise<string|null> {
|
||||
await this.Init()
|
||||
return await this.realStorage.setItem(key, value)
|
||||
if(this.isAccount){
|
||||
return await (this.realStorage as AccountStorage).setItem(key, value)
|
||||
}
|
||||
await this.realStorage.setItem(key, value)
|
||||
return null
|
||||
}
|
||||
async getItem(key:string):Promise<Buffer> {
|
||||
await this.Init()
|
||||
@@ -27,6 +35,18 @@ export class AutoStorage{
|
||||
return await this.realStorage.removeItem(key)
|
||||
}
|
||||
|
||||
checkAccountSync(){
|
||||
let db = get(DataBase)
|
||||
if(db.account?.useSync){
|
||||
console.log("using account storage")
|
||||
sessionStorage.setItem('fallbackRisuToken',db.account?.token)
|
||||
this.realStorage = new AccountStorage()
|
||||
this.isAccount = true
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private async Init(){
|
||||
if(!this.realStorage){
|
||||
if(isNodeServer){
|
||||
|
||||
@@ -523,6 +523,7 @@ export interface Database{
|
||||
access_token?:string
|
||||
expires_in?: number
|
||||
}
|
||||
useSync?:boolean
|
||||
},
|
||||
classicMaxWidth: boolean,
|
||||
useChatSticker:boolean,
|
||||
|
||||
@@ -14,7 +14,7 @@ import { loadPlugins } from "../plugins/plugins";
|
||||
import { alertError } from "../alert";
|
||||
import { checkDriverInit, syncDrive } from "../drive/drive";
|
||||
import { hasher } from "../parser";
|
||||
import { characterHubImport } from "../characterCards";
|
||||
import { characterHubImport, hubURL } from "../characterCards";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { defaultJailbreak, defaultMainPrompt, oldJailbreak, oldMainPrompt } from "./defaultPrompts";
|
||||
import { loadRisuAccountData } from "../drive/accounter";
|
||||
@@ -188,8 +188,13 @@ export async function saveAsset(data:Uint8Array, customId:string = '', fileName:
|
||||
return `assets/${id}.${fileExtension}`
|
||||
}
|
||||
else{
|
||||
await forageStorage.setItem(`assets/${id}.${fileExtension}`, data)
|
||||
return `assets/${id}.${fileExtension}`
|
||||
let form = `assets/${id}.${fileExtension}`
|
||||
const replacer = await forageStorage.setItem(form, data)
|
||||
if(replacer){
|
||||
console.log(replacer)
|
||||
return replacer
|
||||
}
|
||||
return form
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +217,9 @@ export async function saveDb(){
|
||||
}
|
||||
else{
|
||||
await forageStorage.setItem('database/database.bin', dbData)
|
||||
await forageStorage.setItem(`database/dbbackup-${(Date.now()/100).toFixed()}.bin`, dbData)
|
||||
if(!forageStorage.isAccount){
|
||||
await forageStorage.setItem(`database/dbbackup-${(Date.now()/100).toFixed()}.bin`, dbData)
|
||||
}
|
||||
}
|
||||
await getDbBackups()
|
||||
}
|
||||
@@ -222,6 +229,10 @@ export async function saveDb(){
|
||||
|
||||
|
||||
async function getDbBackups() {
|
||||
let db = get(DataBase)
|
||||
if(db?.account?.useSync){
|
||||
return
|
||||
}
|
||||
if(isTauri){
|
||||
const keys = await readDir('database', {dir: BaseDirectory.AppData})
|
||||
let backups:number[] = []
|
||||
@@ -330,6 +341,33 @@ export async function loadData() {
|
||||
throw "Your save file is corrupted"
|
||||
}
|
||||
}
|
||||
if(forageStorage.checkAccountSync()){
|
||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin')
|
||||
if(checkNullish(gotStorage)){
|
||||
gotStorage = encodeRisuSave({})
|
||||
await forageStorage.setItem('database/database.bin', gotStorage)
|
||||
}
|
||||
try {
|
||||
setDatabase(
|
||||
decodeRisuSave(gotStorage)
|
||||
)
|
||||
} catch (error) {
|
||||
const backups = await getDbBackups()
|
||||
let backupLoaded = false
|
||||
for(const backup of backups){
|
||||
try {
|
||||
const backupData:Uint8Array = await forageStorage.getItem(`database/dbbackup-${backup}.bin`)
|
||||
setDatabase(
|
||||
decodeRisuSave(backupData)
|
||||
)
|
||||
backupLoaded = true
|
||||
} catch (error) {}
|
||||
}
|
||||
if(!backupLoaded){
|
||||
throw "Your save file is corrupted"
|
||||
}
|
||||
}
|
||||
}
|
||||
const isDriverMode = await checkDriverInit()
|
||||
if(isDriverMode){
|
||||
return
|
||||
@@ -797,6 +835,9 @@ export function checkCharOrder() {
|
||||
|
||||
async function pargeChunks(){
|
||||
const db = get(DataBase)
|
||||
if(db.account?.useSync){
|
||||
return
|
||||
}
|
||||
|
||||
const unpargeable = getUnpargeables(db)
|
||||
if(isTauri){
|
||||
|
||||
Reference in New Issue
Block a user