-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathBackupController.js
72 lines (59 loc) · 1.73 KB
/
BackupController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const Logger = require('../Logger')
const { encodeUriPath } = require('../utils/fileUtils')
class BackupController {
constructor() { }
getAll(req, res) {
res.json({
backups: this.backupManager.backups.map(b => b.toJSON()),
backupLocation: this.backupManager.backupLocation
})
}
create(req, res) {
this.backupManager.requestCreateBackup(res)
}
async delete(req, res) {
await this.backupManager.removeBackup(req.backup)
res.json({
backups: this.backupManager.backups.map(b => b.toJSON())
})
}
upload(req, res) {
if (!req.files.file) {
Logger.error('[BackupController] Upload backup invalid')
return res.sendStatus(500)
}
this.backupManager.uploadBackup(req, res)
}
/**
* api/backups/:id/download
*
* @param {*} req
* @param {*} res
*/
download(req, res) {
if (global.XAccel) {
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()
}
res.setHeader('Content-disposition', 'attachment; filename=' + req.backup.filename)
res.sendFile(req.backup.fullPath)
}
apply(req, res) {
this.backupManager.requestApplyBackup(req.backup, res)
}
middleware(req, res, next) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to access backups`, req.user)
return res.sendStatus(403)
}
if (req.params.id) {
req.backup = this.backupManager.backups.find(b => b.id === req.params.id)
if (!req.backup) {
return res.sendStatus(404)
}
}
next()
}
}
module.exports = new BackupController()