diff --git a/src/ts/alert.ts b/src/ts/alert.ts index 28cc6288..2d88a090 100644 --- a/src/ts/alert.ts +++ b/src/ts/alert.ts @@ -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', diff --git a/src/ts/drive/drive.ts b/src/ts/drive/drive.ts index 567a60e1..77977c84 100644 --- a/src/ts/drive/drive.ts +++ b/src/ts/drive/drive.ts @@ -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= 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 { const url = `https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&pageSize=300` + nextPageToken;