[feat] add kei backup
This commit is contained in:
100
src/ts/kei/backup.ts
Normal file
100
src/ts/kei/backup.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { get } from "svelte/store"
|
||||
import { alertNormal, alertSelect } from "../alert"
|
||||
import { keiServerURL } from "./kei"
|
||||
import { DataBase, setDatabase } from "../storage/database"
|
||||
|
||||
export async function autoServerBackup(){
|
||||
const db = get(DataBase)
|
||||
const res = await fetch(keiServerURL() + '/autobackup/list', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: db.account.token
|
||||
})
|
||||
})
|
||||
if(res.status === 200){
|
||||
const json = await res.json()
|
||||
if(json.activated){
|
||||
alertNormal("Auto backup is now activated in your account. Now it would backup automaticly, and you can now restore your data from the backup server.")
|
||||
db.account.kei = true
|
||||
return
|
||||
}
|
||||
const backups:[string,string][] = json.backups
|
||||
let pointer = 0
|
||||
while(true){
|
||||
const slices = backups.slice(pointer, pointer + 5)
|
||||
const menu = slices.map((backup) => backup[0])
|
||||
menu.push("Next")
|
||||
menu.push("Previous")
|
||||
menu.push("Cancel")
|
||||
const selectIndex = parseInt(await alertSelect(menu))
|
||||
const selected = menu[selectIndex]
|
||||
if(selected === "Cancel"){
|
||||
break
|
||||
}
|
||||
else if(selected === "Next"){
|
||||
pointer += 5
|
||||
if(pointer >= backups.length){
|
||||
pointer = 0
|
||||
}
|
||||
}
|
||||
else if(selected === "Previous"){
|
||||
pointer -= 5
|
||||
if(pointer < 0){
|
||||
pointer = Math.floor(backups.length / 5) * 5
|
||||
}
|
||||
}
|
||||
else{
|
||||
const backup = backups.find((backup) => backup[0] === selected)
|
||||
if(backup){
|
||||
const res = await fetch(keiServerURL() + '/autobackup/restore', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: db.account.token,
|
||||
backup: backup[1]
|
||||
})
|
||||
})
|
||||
if(res.status === 200){
|
||||
setDatabase(await res.json())
|
||||
alertNormal("Successfully restored!")
|
||||
}
|
||||
else{
|
||||
alertNormal("Error: " + res.text())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(res.status === 401){
|
||||
alertNormal("You need to activate Risu-Kei in your account, or host your Risu-Kei server from github to use this feature.")
|
||||
}
|
||||
else{
|
||||
alertNormal("Error: " + res.text())
|
||||
}
|
||||
}
|
||||
|
||||
let lastKeiSave = 0
|
||||
export async function saveDbKei() {
|
||||
let db = get(DataBase)
|
||||
if(db.account.kei){
|
||||
if(Date.now() - lastKeiSave < 60000 * 5){
|
||||
return
|
||||
}
|
||||
lastKeiSave = Date.now()
|
||||
fetch(keiServerURL() + '/autobackup/save', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token: db.account.token,
|
||||
database: db
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
9
src/ts/kei/kei.ts
Normal file
9
src/ts/kei/kei.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { get } from "svelte/store";
|
||||
import { hubURL } from "../characterCards";
|
||||
import { DataBase } from "../storage/database";
|
||||
|
||||
export function keiServerURL(){
|
||||
const db = get(DataBase)
|
||||
if(db.keiServerURL) return db.keiServerURL;
|
||||
return hubURL + "/kei";
|
||||
}
|
||||
Reference in New Issue
Block a user