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