refactor: backup file download. (#297)

pull/298/head
Ryan Wang 2021-02-22 09:22:41 +08:00 committed by GitHub
parent ec25a23111
commit ecc0784a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 10 deletions

View File

@ -30,6 +30,13 @@ backupApi.listWorkDirBackups = () => {
}) })
} }
backupApi.fetchWorkDir = filename => {
return service({
url: `${baseUrl}/work-dir/fetch?filename=${filename}`,
method: 'get'
})
}
backupApi.deleteWorkDirBackup = filename => { backupApi.deleteWorkDirBackup = filename => {
return service({ return service({
url: `${baseUrl}/work-dir`, url: `${baseUrl}/work-dir`,
@ -55,6 +62,13 @@ backupApi.listExportedData = () => {
}) })
} }
backupApi.fetchData = filename => {
return service({
url: `${baseUrl}/data/fetch?filename=${filename}`,
method: 'get'
})
}
backupApi.deleteExportedData = filename => { backupApi.deleteExportedData = filename => {
return service({ return service({
url: `${baseUrl}/data`, url: `${baseUrl}/data`,
@ -83,6 +97,13 @@ backupApi.listExportedMarkdowns = () => {
}) })
} }
backupApi.fetchMarkdown = filename => {
return service({
url: `${baseUrl}/markdown/fetch?filename=${filename}`,
method: 'get'
})
}
backupApi.deleteExportedMarkdown = filename => { backupApi.deleteExportedMarkdown = filename => {
return service({ return service({
url: `${baseUrl}/markdown/export`, url: `${baseUrl}/markdown/export`,

View File

@ -40,7 +40,8 @@
<a-list-item-meta> <a-list-item-meta>
<a <a
slot="title" slot="title"
:href="backup.downloadLink" href="javascript:void(0)"
@click="handleDownloadBackupPackage(backup)"
> >
<a-icon <a-icon
type="schedule" type="schedule"
@ -151,6 +152,23 @@ export default {
this.handleListBackups() this.handleListBackups()
}) })
}, },
handleDownloadBackupPackage(item) {
backupApi
.fetchWorkDir(item.filename)
.then((response) => {
var downloadElement = document.createElement('a')
var href = new window.URL(response.data.data.downloadLink)
downloadElement.href = href
downloadElement.download = response.data.data.filename
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
window.URL.revokeObjectURL(href)
})
.catch((error) => {
this.$message.error(error.data.message)
})
},
onClose() { onClose() {
this.$emit('close', false) this.$emit('close', false)
}, },

View File

@ -40,7 +40,8 @@
<a-list-item-meta> <a-list-item-meta>
<a <a
slot="title" slot="title"
:href="file.downloadLink" href="javascript:void(0)"
@click="handleDownloadBackupFile(file)"
> >
<a-icon <a-icon
type="schedule" type="schedule"
@ -89,19 +90,19 @@ export default {
backuping: false, backuping: false,
loading: false, loading: false,
backupErrored: false, backupErrored: false,
files: [] files: [],
} }
}, },
model: { model: {
prop: 'visible', prop: 'visible',
event: 'close' event: 'close',
}, },
props: { props: {
visible: { visible: {
type: Boolean, type: Boolean,
required: false, required: false,
default: true default: true,
} },
}, },
methods: { methods: {
handleAfterVisibleChanged(visible) { handleAfterVisibleChanged(visible) {
@ -113,7 +114,7 @@ export default {
this.loading = true this.loading = true
backupApi backupApi
.listExportedData() .listExportedData()
.then(response => { .then((response) => {
this.files = response.data.data this.files = response.data.data
}) })
.finally(() => { .finally(() => {
@ -151,9 +152,26 @@ export default {
this.handleListBackups() this.handleListBackups()
}) })
}, },
handleDownloadBackupFile(item) {
backupApi
.fetchData(item.filename)
.then((response) => {
var downloadElement = document.createElement('a')
var href = new window.URL(response.data.data.downloadLink)
downloadElement.href = href
downloadElement.download = response.data.data.filename
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
window.URL.revokeObjectURL(href)
})
.catch(() => {
this.$message.error('下载失败!')
})
},
onClose() { onClose() {
this.$emit('close', false) this.$emit('close', false)
} },
} },
} }
</script> </script>

View File

@ -40,7 +40,8 @@
<a-list-item-meta> <a-list-item-meta>
<a <a
slot="title" slot="title"
:href="file.downloadLink" href="javascript:void(0)"
@click="handleDownloadMarkdownPackage(file)"
> >
<a-icon <a-icon
type="schedule" type="schedule"
@ -158,6 +159,23 @@ export default {
this.handleListBackups() this.handleListBackups()
}) })
}, },
handleDownloadMarkdownPackage(item) {
backupApi
.fetchMarkdown(item.filename)
.then((response) => {
var downloadElement = document.createElement('a')
var href = new window.URL(response.data.data.downloadLink)
downloadElement.href = href
downloadElement.download = response.data.data.filename
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
window.URL.revokeObjectURL(href)
})
.catch(() => {
this.$message.error('下载失败!')
})
},
onClose() { onClose() {
this.$emit('close', false) this.$emit('close', false)
}, },