WIP: add download data backup

pull/1/head
I Made Setia Baruna 2020-11-05 17:27:12 +07:00
parent 333a7b6928
commit 925355bc32
2 changed files with 24 additions and 1 deletions

View File

@ -15,6 +15,8 @@
const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']; const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'];
const SCOPES = 'https://www.googleapis.com/auth/drive.appdata'; const SCOPES = 'https://www.googleapis.com/auth/drive.appdata';
let remoteSave = null;
$: localSaveExists = $updateTime !== null; $: localSaveExists = $updateTime !== null;
onMount(() => { onMount(() => {
@ -52,6 +54,8 @@
async function compareLocalSave() { async function compareLocalSave() {
try { try {
const data = await getData(); const data = await getData();
remoteSave = data;
const remoteTime = dayjs(data[UPDATE_TIME_KEY]); const remoteTime = dayjs(data[UPDATE_TIME_KEY]);
if ($updateTime !== null && remoteTime.diff($updateTime) !== 0) { if ($updateTime !== null && remoteTime.diff($updateTime) !== 0) {
console.log('DRIVE SYNC CONFLICT!'); console.log('DRIVE SYNC CONFLICT!');
@ -60,6 +64,7 @@
{ {
remoteTime: remoteTime, remoteTime: remoteTime,
localTime: $updateTime, localTime: $updateTime,
downloadBackup: exportData,
}, },
{ {
closeButton: false, closeButton: false,
@ -176,4 +181,21 @@
}, },
); );
} }
function exportData() {
downloadData(getLocalSaveJson(), 'paimon-moe-local-data');
downloadData(JSON.stringify(remoteSave), 'paimon-moe-drive-data');
}
function downloadData(data, name) {
const fileLink = document.createElement('a');
const filename = `${name}.json`;
const dataStr = encodeURIComponent(data);
fileLink.setAttribute('href', `data:text/json;charset=utf-8,${dataStr}`);
fileLink.setAttribute('download', filename);
document.body.appendChild(fileLink);
fileLink.click();
}
</script> </script>

View File

@ -6,6 +6,7 @@
export let remoteTime; export let remoteTime;
export let localTime; export let localTime;
export let downloadBackup = () => {};
const remoteFormatted = remoteTime.format('dddd, MMMM D, YYYY h:mm A'); const remoteFormatted = remoteTime.format('dddd, MMMM D, YYYY h:mm A');
const localFormatted = localTime.format('dddd, MMMM D, YYYY h:mm A'); const localFormatted = localTime.format('dddd, MMMM D, YYYY h:mm A');
@ -42,7 +43,7 @@
</Button> </Button>
</div> </div>
<div class="flex mt-6 justify-end"> <div class="flex mt-6 justify-end">
<Button className="w-full md:w-auto"> <Button className="w-full md:w-auto" on:click={downloadBackup}>
<Icon path={mdiContentSave} className="mr-1" />Download Both Data <Icon path={mdiContentSave} className="mr-1" />Download Both Data
</Button> </Button>
</div> </div>