[fix] file sync login

This commit is contained in:
kwaroran
2023-06-30 16:44:20 +09:00
parent 7eb6907eef
commit c2648477d2
5 changed files with 76 additions and 27 deletions

View File

@@ -6,6 +6,7 @@
import { ParseMarkdown } from '../../ts/parser'; import { ParseMarkdown } from '../../ts/parser';
import BarIcon from '../SideBars/BarIcon.svelte'; import BarIcon from '../SideBars/BarIcon.svelte';
import { User } from 'lucide-svelte'; import { User } from 'lucide-svelte';
import { hubURL } from 'src/ts/characterCards';
let btn let btn
let input = '' let input = ''
@@ -25,6 +26,17 @@
}) })
</script> </script>
<svelte:window on:message={async (e) => {
if(e.origin.startsWith("https://sv.risuai.xyz") || e.origin.startsWith("http://127.0.0.1")){
if(e.data.msg.data.vaild && $alertStore.type === 'login'){
$alertStore = {
type: 'none',
msg: JSON.stringify(e.data.msg)
}
}
}
}}></svelte:window>
{#if $alertStore.type !== 'none' && $alertStore.type !== 'toast'} {#if $alertStore.type !== 'none' && $alertStore.type !== 'toast'}
<div class="absolute w-full h-full z-50 bg-black bg-opacity-50 flex justify-center items-center" class:vis={ $alertStore.type === 'wait2'}> <div class="absolute w-full h-full z-50 bg-black bg-opacity-50 flex justify-center items-center" class:vis={ $alertStore.type === 'wait2'}>
<div class="bg-darkbg p-4 break-any rounded-md flex flex-col max-w-3xl max-h-full overflow-y-auto"> <div class="bg-darkbg p-4 break-any rounded-md flex flex-col max-w-3xl max-h-full overflow-y-auto">
@@ -86,6 +98,11 @@
msg: document.querySelector('#alert-input')?.value msg: document.querySelector('#alert-input')?.value
}) })
}}>OK</button> }}>OK</button>
{:else if $alertStore.type === 'login'}
<div class="fixed top-0 left-0 bg-black bg-opacity-50 w-full h-full flex justify-center items-center">
<iframe src={hubURL + '/hub/login'} title="login" class="w-full h-full">
</iframe>
</div>
{:else if $alertStore.type === 'selectChar'} {:else if $alertStore.type === 'selectChar'}
<div class="flex w-full items-start flex-wrap gap-2 justify-start"> <div class="flex w-full items-start flex-wrap gap-2 justify-start">
{#each $DataBase.characters as char, i} {#each $DataBase.characters as char, i}

View File

@@ -3,7 +3,7 @@ import { sleep } from "./util"
import { language } from "../lang" import { language } from "../lang"
interface alertData{ interface alertData{
type: 'error'| 'normal'|'none'|'ask'|'wait'|'selectChar'|'input'|'toast'|'wait2'|'markdown'|'select' type: 'error'| 'normal'|'none'|'ask'|'wait'|'selectChar'|'input'|'toast'|'wait2'|'markdown'|'select'|'login'
msg: string msg: string
} }
@@ -29,6 +29,21 @@ export function alertNormal(msg:string){
}) })
} }
export async function alertLogin(){
alertStore.set({
'type': 'login',
'msg': 'login'
})
while(true){
if (get(alertStore).type === 'none'){
break
}
await sleep(10)
}
return get(alertStore).msg
}
export async function alertSelect(msg:string[]){ export async function alertSelect(msg:string[]){
alertStore.set({ alertStore.set({
'type': 'select', 'type': 'select',

View File

@@ -2,42 +2,53 @@ 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" import localforage from "localforage"
import { alertLogin } from "../alert"
export class AccountStorage{ export class AccountStorage{
auth:string auth:string
usingSync:boolean usingSync:boolean
async setItem(key:string, value:Uint8Array) { async setItem(key:string, value:Uint8Array) {
await this.checkAuth() this.checkAuth()
const da = await fetch(hubURL + '/api/account/write', { let da:Response
method: "POST", while((!da) || da.status === 403){
body: value, da = await fetch(hubURL + '/api/account/write', {
headers: { method: "POST",
'content-type': 'application/json', body: value,
'x-risu-key': key, headers: {
'x-risu-auth': this.auth ?? sessionStorage.getItem("fallbackRisuToken") 'content-type': 'application/json',
} 'x-risu-key': key,
}) 'x-risu-auth': this.auth
}
})
localStorage.setItem("fallbackRisuToken",await alertLogin())
this.checkAuth()
}
if(da.status < 200 || da.status >= 300){ if(da.status < 200 || da.status >= 300){
throw await da.text() throw await da.text()
} }
return await da.text() return await da.text()
} }
async getItem(key:string):Promise<Buffer> { async getItem(key:string):Promise<Buffer> {
await this.checkAuth() this.checkAuth()
if(key.startsWith('assets/')){ if(key.startsWith('assets/')){
const k:ArrayBuffer = await localforage.getItem(key) const k:ArrayBuffer = await localforage.getItem(key)
if(k){ if(k){
return Buffer.from(k) return Buffer.from(k)
} }
} }
const da = await fetch(hubURL + '/api/account/read', { let da:Response
method: "GET", while((!da) || da.status === 403){
headers: { da = await fetch(hubURL + '/api/account/read', {
'x-risu-key': key, method: "GET",
'x-risu-auth': this.auth ?? sessionStorage.getItem("fallbackRisuToken") headers: {
} 'x-risu-key': key,
}) 'x-risu-auth': this.auth
}
})
localStorage.setItem("fallbackRisuToken",await alertLogin())
this.checkAuth()
}
if(da.status < 200 || da.status >= 300){ if(da.status < 200 || da.status >= 300){
throw await da.text() throw await da.text()
} }
@@ -57,16 +68,12 @@ export class AccountStorage{
throw "Error: You remove data in account. report this to dev if you found this." throw "Error: You remove data in account. report this to dev if you found this."
} }
private async checkAuth(){ private checkAuth(){
const db = get(DataBase) const db = get(DataBase)
this.auth = db?.account?.token this.auth = db?.account?.token
if(!this.auth){ if(!this.auth){
db.account = { db.account = JSON.parse(localStorage.getItem("fallbackRisuToken"))
id: "", this.auth = db?.account?.token
token: sessionStorage.getItem("fallbackRisuToken"),
useSync: true,
data: {}
}
} }
} }

View File

@@ -38,6 +38,9 @@ export class AutoStorage{
async checkAccountSync(){ async checkAccountSync(){
let db = get(DataBase) let db = get(DataBase)
if(this.isAccount){
return true
}
if(db.account?.useSync && (localStorage.getItem('accountst') !== 'able')){ if(db.account?.useSync && (localStorage.getItem('accountst') !== 'able')){
getUnpargeables(db) getUnpargeables(db)
console.log("migrating") console.log("migrating")
@@ -68,11 +71,13 @@ export class AutoStorage{
}) })
localStorage.setItem('accountst', 'able') localStorage.setItem('accountst', 'able')
sessionStorage.setItem('fallbackRisuToken',db.account?.token) localStorage.setItem('fallbackRisuToken',JSON.stringify(db.account))
this.isAccount = true
return true return true
} }
else if(localStorage.getItem('accountst') === 'able'){ else if(localStorage.getItem('accountst') === 'able'){
this.realStorage = new AccountStorage() this.realStorage = new AccountStorage()
this.isAccount = true
} }
return false return false
} }
@@ -81,6 +86,7 @@ export class AutoStorage{
if(!this.realStorage){ if(!this.realStorage){
if(localStorage.getItem('accountst') === 'able'){ if(localStorage.getItem('accountst') === 'able'){
this.realStorage = new AccountStorage() this.realStorage = new AccountStorage()
this.isAccount = true
return return
} }
if(isNodeServer){ if(isNodeServer){

View File

@@ -86,6 +86,10 @@ export async function getFileSrc(loc:string) {
return convertFileSrc(loc) return convertFileSrc(loc)
} }
try { try {
if(forageStorage.isAccount){
return hubURL + `/rs/` + loc
}
if(usingSw){ if(usingSw){
const encoded = Buffer.from(loc,'utf-8').toString('hex') const encoded = Buffer.from(loc,'utf-8').toString('hex')
let ind = fileCache.origin.indexOf(loc) let ind = fileCache.origin.indexOf(loc)