Files
suwayomi-material-you-webui/src/screens/settings/Backup.tsx

90 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-04-10 00:44:13 +04:30
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React, { useContext, useEffect } from 'react';
2021-09-09 17:51:22 +04:30
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
2021-04-10 00:44:13 +04:30
import { fromEvent } from 'file-selector';
import client from 'util/client';
2021-10-30 16:10:34 +03:30
import makeToast from 'components/util/Toast';
import ListItemLink from 'components/util/ListItemLink';
import NavbarContext from 'components/context/NavbarContext';
2021-04-10 00:44:13 +04:30
export default function Backup() {
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => {
setTitle('Backup');
setAction(null);
}, []);
2021-04-10 00:44:13 +04:30
const { baseURL } = client.defaults;
const submitBackup = (file: File) => {
2021-08-19 20:42:28 +04:30
if (file.name.toLowerCase().endsWith('proto.gz')) {
const formData = new FormData();
formData.append('backup.proto.gz', file);
2021-10-10 23:49:04 +03:30
makeToast('Restoring backup....', 'info');
client
.post('/api/v1/backup/import/file', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
2021-08-21 06:26:45 +04:30
.then(() => makeToast('Backup restore finished!', 'success'))
.catch(() => makeToast('Backup restore failed!', 'error'));
2021-08-19 20:42:28 +04:30
} else if (file.name.toLowerCase().endsWith('json')) {
2021-08-21 06:26:45 +04:30
makeToast('legacy backups are not supported!', 'error');
2021-08-19 20:42:28 +04:30
} else {
makeToast('invalid file type!', 'error');
}
2021-04-10 00:44:13 +04:30
};
const dropHandler = async (e: Event) => {
e.preventDefault();
const files = await fromEvent(e);
submitBackup(files[0] as File);
};
const dragOverHandler = (e: Event) => {
e.preventDefault();
};
useEffect(() => {
document.addEventListener('drop', dropHandler);
document.addEventListener('dragover', dragOverHandler);
const input = document.getElementById('backup-file');
input?.addEventListener('change', async (evt) => {
const files = await fromEvent(evt);
submitBackup(files[0] as File);
});
return () => {
document.removeEventListener('drop', dropHandler);
document.removeEventListener('dragover', dragOverHandler);
};
}, []);
return (
2021-08-19 20:42:28 +04:30
<>
2021-11-15 18:55:50 +03:30
<List sx={{ padding: 0 }}>
2022-02-21 01:24:42 +03:30
<ListItemLink to={`${baseURL}/api/v1/backup/export/file`} directLink>
<ListItemText primary="Create Backup" secondary="Backup library as a Tachiyomi backup" />
2021-08-19 20:42:28 +04:30
</ListItemLink>
<ListItem button onClick={() => document.getElementById('backup-file')?.click()}>
<ListItemText
primary="Restore Backup"
secondary="You can also drag and drop the backup file here to restore"
/>
</ListItem>
</List>
<input type="file" id="backup-file" style={{ display: 'none' }} />
2021-08-19 20:42:28 +04:30
</>
2021-04-10 00:44:13 +04:30
);
}