2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2023-09-18 22:08:19 +02:00
|
|
|
const { encodeUriPath } = require('../utils/fileUtils')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
class BackupController {
|
|
|
|
constructor() { }
|
|
|
|
|
2022-11-24 20:14:29 +01:00
|
|
|
getAll(req, res) {
|
|
|
|
res.json({
|
2023-09-22 23:49:01 +02:00
|
|
|
backups: this.backupManager.backups.map(b => b.toJSON()),
|
2023-09-20 23:33:58 +02:00
|
|
|
backupLocation: this.backupManager.backupLocation
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:14:29 +01:00
|
|
|
create(req, res) {
|
2022-03-18 19:44:29 +01:00
|
|
|
this.backupManager.requestCreateBackup(res)
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
async delete(req, res) {
|
2023-06-27 23:41:32 +02:00
|
|
|
await this.backupManager.removeBackup(req.backup)
|
2022-11-24 20:14:29 +01:00
|
|
|
|
|
|
|
res.json({
|
|
|
|
backups: this.backupManager.backups.map(b => b.toJSON())
|
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:41:32 +02:00
|
|
|
upload(req, res) {
|
2021-11-22 03:00:40 +01:00
|
|
|
if (!req.files.file) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.error('[BackupController] Upload backup invalid')
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
this.backupManager.uploadBackup(req, res)
|
|
|
|
}
|
2022-03-18 19:44:29 +01:00
|
|
|
|
2023-06-27 23:41:32 +02:00
|
|
|
/**
|
|
|
|
* api/backups/:id/download
|
|
|
|
*
|
|
|
|
* @param {*} req
|
|
|
|
* @param {*} res
|
|
|
|
*/
|
|
|
|
download(req, res) {
|
|
|
|
if (global.XAccel) {
|
2023-09-18 22:08:19 +02:00
|
|
|
const encodedURI = encodeUriPath(global.XAccel + req.backup.fullPath)
|
|
|
|
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
|
|
|
|
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
|
2022-03-18 19:44:29 +01:00
|
|
|
}
|
2023-09-19 23:37:57 +02:00
|
|
|
|
|
|
|
res.setHeader('Content-disposition', 'attachment; filename=' + req.backup.filename)
|
|
|
|
|
2023-06-27 23:41:32 +02:00
|
|
|
res.sendFile(req.backup.fullPath)
|
|
|
|
}
|
|
|
|
|
2023-07-08 21:40:49 +02:00
|
|
|
apply(req, res) {
|
|
|
|
this.backupManager.requestApplyBackup(req.backup, res)
|
2022-03-18 19:44:29 +01:00
|
|
|
}
|
2022-11-24 20:14:29 +01:00
|
|
|
|
|
|
|
middleware(req, res, next) {
|
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[BackupController] Non-admin user attempting to access backups`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2023-06-27 23:41:32 +02:00
|
|
|
|
|
|
|
if (req.params.id) {
|
|
|
|
req.backup = this.backupManager.backups.find(b => b.id === req.params.id)
|
|
|
|
if (!req.backup) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 20:14:29 +01:00
|
|
|
next()
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-09-19 23:37:57 +02:00
|
|
|
module.exports = new BackupController()
|