2024-08-12 00:01:25 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2024-06-20 00:14:37 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('../libs/fsExtra')
|
2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2024-06-20 00:14:37 +02:00
|
|
|
const Database = require('../Database')
|
|
|
|
const fileUtils = require('../utils/fileUtils')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObject
|
|
|
|
* @property {import('../models/User')} user
|
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
class BackupController {
|
2024-06-19 00:10:49 +02:00
|
|
|
constructor() {}
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/backups
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-24 20:14:29 +01:00
|
|
|
getAll(req, res) {
|
|
|
|
res.json({
|
2024-06-19 00:10:49 +02:00
|
|
|
backups: this.backupManager.backups.map((b) => b.toJSON()),
|
2024-07-05 19:27:49 +02:00
|
|
|
backupLocation: this.backupManager.backupPath,
|
2024-07-05 19:58:42 +02:00
|
|
|
backupPathEnvSet: this.backupManager.backupPathEnvSet
|
2023-09-20 23:33:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/backups
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-24 20:14:29 +01:00
|
|
|
create(req, res) {
|
2022-03-18 19:44:29 +01:00
|
|
|
this.backupManager.requestCreateBackup(res)
|
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/backups/:id
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} 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({
|
2024-06-19 00:10:49 +02:00
|
|
|
backups: this.backupManager.backups.map((b) => b.toJSON())
|
2022-11-24 20:14:29 +01:00
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/backups/upload
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
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
|
|
|
|
2024-06-20 00:14:37 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/backups/path
|
|
|
|
* Update the backup path
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-06-20 00:14:37 +02:00
|
|
|
*/
|
|
|
|
async updatePath(req, res) {
|
|
|
|
// Validate path is not empty and is a string
|
|
|
|
if (!req.body.path || !req.body.path?.trim?.()) {
|
|
|
|
Logger.error('[BackupController] Update backup path invalid')
|
|
|
|
return res.status(400).send('Invalid request body. Must include path.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBackupPath = fileUtils.filePathToPOSIX(Path.resolve(req.body.path))
|
|
|
|
|
|
|
|
if (newBackupPath === this.backupManager.backupPath) {
|
|
|
|
Logger.debug(`[BackupController] Backup path unchanged: ${newBackupPath}`)
|
|
|
|
return res.status(200).send('Backup path unchanged')
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.info(`[BackupController] Updating backup path to "${newBackupPath}" from "${this.backupManager.backupPath}"`)
|
|
|
|
|
|
|
|
// Check if backup path is set in environment variable
|
|
|
|
if (process.env.BACKUP_PATH) {
|
|
|
|
Logger.warn(`[BackupController] Backup path is set in environment variable BACKUP_PATH. Backup path will be reverted on server restart.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate backup path is writable and create folder if it does not exist
|
|
|
|
try {
|
|
|
|
const direxists = await fs.pathExists(newBackupPath)
|
|
|
|
if (!direxists) {
|
|
|
|
// If folder does not exist try to make it
|
|
|
|
await fs.mkdir(newBackupPath)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[BackupController] updatePath: Failed to ensure backup path "${newBackupPath}"`, error)
|
|
|
|
return res.status(400).send(`Invalid backup path "${req.body.path}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
Database.serverSettings.backupPath = newBackupPath
|
|
|
|
await Database.updateServerSettings()
|
|
|
|
|
|
|
|
await this.backupManager.reload()
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:41:32 +02:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* GET: /api/backups/:id/download
|
2024-06-19 00:10:49 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-06-27 23:41:32 +02:00
|
|
|
*/
|
|
|
|
download(req, res) {
|
|
|
|
if (global.XAccel) {
|
2024-06-20 00:14:37 +02:00
|
|
|
const encodedURI = fileUtils.encodeUriPath(global.XAccel + req.backup.fullPath)
|
2023-09-18 22:08:19 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-16 21:12:33 +01:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* GET: /api/backups/:id/apply
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
2024-06-19 00:10:49 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-03-16 21:12:33 +01:00
|
|
|
*/
|
2023-07-08 21:40:49 +02:00
|
|
|
apply(req, res) {
|
2024-03-16 21:12:33 +01:00
|
|
|
this.backupManager.requestApplyBackup(this.apiCacheManager, req.backup, res)
|
2022-03-18 19:44:29 +01:00
|
|
|
}
|
2022-11-24 20:14:29 +01:00
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2022-11-24 20:14:29 +01:00
|
|
|
middleware(req, res, next) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[BackupController] Non-admin user "${req.user.username}" attempting to access backups`)
|
2022-11-24 20:14:29 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2023-06-27 23:41:32 +02:00
|
|
|
|
|
|
|
if (req.params.id) {
|
2024-06-19 00:10:49 +02:00
|
|
|
req.backup = this.backupManager.backups.find((b) => b.id === req.params.id)
|
2023-06-27 23:41:32 +02:00
|
|
|
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()
|