[fix] new backupformating

This commit is contained in:
kwaroran
2023-11-17 22:41:20 +09:00
parent 79875661c5
commit 929ed557d2
2 changed files with 60 additions and 34 deletions

View File

@@ -60,6 +60,19 @@ export async function alertSelect(msg:string[]){
return get(alertStore).msg
}
export async function alertErrorWait(msg:string){
alertStore.set({
'type': 'wait2',
'msg': msg
})
while(true){
if (get(alertStore).type === 'none'){
break
}
await sleep(10)
}
}
export function alertMd(msg:string){
alertStore.set({
'type': 'markdown',

View File

@@ -1,5 +1,5 @@
import { get } from "svelte/store";
import { alertError, alertInput, alertNormal, alertSelect, alertStore } from "../alert";
import { alertError, alertErrorWait, alertInput, alertNormal, alertSelect, alertStore } from "../alert";
import { DataBase, setDatabase, type Database } from "../storage/database";
import { forageStorage, getUnpargeables, isNodeServer, isTauri, openURL } from "../storage/globalApi";
import { BaseDirectory, exists, readBinaryFile, readDir, writeBinaryFile } from "@tauri-apps/api/fs";
@@ -221,7 +221,7 @@ async function backupDrive(ACCESS_TOKEN:string) {
if(!key || !key.endsWith('.png')){
continue
}
const formatedKey = formatKeys(key)
const formatedKey = newFormatKeys(key)
if(!fileNames.includes(formatedKey)){
await createFileInFolder(ACCESS_TOKEN, formatedKey, await readBinaryFile(asset.path))
}
@@ -239,7 +239,7 @@ async function backupDrive(ACCESS_TOKEN:string) {
if(!key.endsWith('.png')){
continue
}
const formatedKey = formatKeys(key)
const formatedKey = newFormatKeys(key)
if(!fileNames.includes(formatedKey)){
await createFileInFolder(ACCESS_TOKEN, formatedKey, await forageStorage.getItem(key))
}
@@ -366,42 +366,45 @@ async function loadDrive(ACCESS_TOKEN:string, mode: 'backup'|'sync'):Promise<voi
localStorage.setItem('risu_lastsaved', `${lastSaved}`)
const requiredImages = (getUnpargeables(db))
let ind = 0;
let errorLogs:string[] = []
for(const images of requiredImages){
ind += 1
const formatedImage = formatKeys(images)
if(mode === 'sync'){
alertStore.set({
type: "wait",
msg: `Sync Files... (${ind} / ${requiredImages.length})`
})
}
else{
alertStore.set({
type: "wait",
msg: `Loading Backup... (${ind} / ${requiredImages.length})`
})
}
if(await checkImageExists(images)){
//skip process
}
else{
if(formatedImage.length >= 7){
if(fileNames.includes(formatedImage)){
for(const file of files){
if(file.name === formatedImage){
const fData = await getFileData(ACCESS_TOKEN, file.id)
if(isTauri){
await writeBinaryFile(`assets/` + images, fData ,{dir: BaseDirectory.AppData})
}
else{
await forageStorage.setItem('assets/' + images, fData)
for(let tries=0;tries<3;tries++){
const formatedImage = tries === 0 ? newFormatKeys(images) : formatKeys(images)
if(mode === 'sync'){
alertStore.set({
type: "wait",
msg: `Sync Files... (${ind} / ${requiredImages.length})`
})
}
else{
alertStore.set({
type: "wait",
msg: `Loading Backup... (${ind} / ${requiredImages.length})`
})
}
if(await checkImageExists(images)){
//skip process
}
else{
if(formatedImage.length >= 7){
if(fileNames.includes(formatedImage)){
for(const file of files){
if(file.name === formatedImage){
const fData = await getFileData(ACCESS_TOKEN, file.id)
if(isTauri){
await writeBinaryFile(`assets/` + images, fData ,{dir: BaseDirectory.AppData})
}
else{
await forageStorage.setItem('assets/' + images, fData)
}
}
}
}
}
else{
throw `cannot find file in drive: ${formatedImage}`
else{
errorLogs.push(`Missing ${images}, skipping...`)
}
}
}
}
@@ -409,6 +412,10 @@ async function loadDrive(ACCESS_TOKEN:string, mode: 'backup'|'sync'):Promise<voi
db.didFirstSetup = true
const dbData = encodeRisuSave(db, 'compression')
if(errorLogs.length !== 0){
await alertErrorWait(`Errors: ${errorLogs.join('\n')}`)
}
if(isTauri){
await writeBinaryFile('database/database.bin', dbData, {dir: BaseDirectory.AppData})
relaunch()
@@ -440,6 +447,12 @@ function formatKeys(name:string) {
return getBasename(name).replace(/\_/g, '__').replace(/\./g,'_d').replace(/\//,'_s') + '.png'
}
function newFormatKeys(name:string) {
let n = getBasename(name)
const bf = Buffer.from(n).toString('hex')
return n + '.bin'
}
async function getFilesInFolder(ACCESS_TOKEN:string, nextPageToken=''): Promise<DriveFile[]> {
const url = `https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&pageSize=300` + nextPageToken;