Update:Backups API endpoints, add get all backups route, update socket init event payload

This commit is contained in:
advplyr 2022-11-24 13:14:29 -06:00
parent 6af5ac2be1
commit 64a8a046c1
7 changed files with 67 additions and 50 deletions

View File

@ -64,13 +64,11 @@ export default {
showConfirmApply: false,
selectedBackup: null,
isBackingUp: false,
processing: false
processing: false,
backups: []
}
},
computed: {
backups() {
return this.$store.state.backups || []
},
userToken() {
return this.$store.getters['user/getToken']
}
@ -96,9 +94,8 @@ export default {
this.processing = true
this.$axios
.$delete(`/api/backups/${backup.id}`)
.then((backups) => {
console.log('Backup deleted', backups)
this.$store.commit('setBackups', backups)
.then((data) => {
this.setBackups(data.backups || [])
this.$toast.success(this.$strings.ToastBackupDeleteSuccess)
this.processing = false
})
@ -117,10 +114,10 @@ export default {
this.isBackingUp = true
this.$axios
.$post('/api/backups')
.then((backups) => {
.then((data) => {
this.isBackingUp = false
this.$toast.success(this.$strings.ToastBackupCreateSuccess)
this.$store.commit('setBackups', backups)
this.setBackups(data.backups || [])
})
.catch((error) => {
this.isBackingUp = false
@ -136,9 +133,8 @@ export default {
this.$axios
.$post('/api/backups/upload', form)
.then((result) => {
console.log('Upload backup result', result)
this.$store.commit('setBackups', result)
.then((data) => {
this.setBackups(data.backups || [])
this.$toast.success(this.$strings.ToastBackupUploadSuccess)
this.processing = false
})
@ -148,9 +144,29 @@ export default {
this.$toast.error(errorMessage)
this.processing = false
})
},
setBackups(backups) {
backups.sort((a, b) => b.createdAt - a.createdAt)
this.backups = backups
},
loadBackups() {
this.processing = true
this.$axios
.$get('/api/backups')
.then((data) => {
this.setBackups(data.backups || [])
})
.catch((error) => {
console.error('Failed to load backups', error)
this.$toast.error('Failed to load backups')
})
.finally(() => {
this.processing = false
})
}
},
mounted() {
this.loadBackups()
if (this.$route.query.backup) {
this.$toast.success('Backup applied successfully')
this.$router.replace('/config')

View File

@ -132,9 +132,6 @@ export default {
}
})
if (payload.backups && payload.backups.length) {
this.$store.commit('setBackups', payload.backups)
}
if (payload.usersOnline) {
this.$store.commit('users/resetUsers')
payload.usersOnline.forEach((user) => {

View File

@ -21,7 +21,6 @@ export const state = () => ({
processingBatch: false,
previousPath: '/',
showExperimentalFeatures: false,
backups: [],
bookshelfBookIds: [],
openModal: null,
innerModalOpen: false,
@ -245,9 +244,6 @@ export const mutations = {
state.showExperimentalFeatures = val
localStorage.setItem('experimental', val ? 1 : 0)
},
setBackups(state, val) {
state.backups = val.sort((a, b) => b.createdAt - a.createdAt)
},
setOpenModal(state, val) {
state.openModal = val
},

View File

@ -473,15 +473,14 @@ class Server {
await this.db.updateEntity('user', user)
const initialPayload = {
metadataPath: global.MetadataPath,
configPath: global.ConfigPath,
user: client.user.toJSONForBrowser(),
librariesScanning: this.scanner.librariesScanning,
backups: (this.backupManager.backups || []).map(b => b.toJSON())
userId: client.user.id,
username: client.user.username,
librariesScanning: this.scanner.librariesScanning
}
if (user.type === 'root') {
if (user.isAdminOrUp) {
initialPayload.usersOnline = this.getUsersOnline()
}
client.socket.emit('init', initialPayload)
}

View File

@ -3,32 +3,29 @@ const Logger = require('../Logger')
class BackupController {
constructor() { }
async create(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to craete backup`, req.user)
return res.sendStatus(403)
}
getAll(req, res) {
res.json({
backups: this.backupManager.backups.map(b => b.toJSON())
})
}
create(req, res) {
this.backupManager.requestCreateBackup(res)
}
async delete(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to delete backup`, req.user)
return res.sendStatus(403)
}
var backup = this.backupManager.backups.find(b => b.id === req.params.id)
if (!backup) {
return res.sendStatus(404)
}
await this.backupManager.removeBackup(backup)
res.json(this.backupManager.backups.map(b => b.toJSON()))
res.json({
backups: this.backupManager.backups.map(b => b.toJSON())
})
}
async upload(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to upload backup`, req.user)
return res.sendStatus(403)
}
if (!req.files.file) {
Logger.error('[BackupController] Upload backup invalid')
return res.sendStatus(500)
@ -37,10 +34,6 @@ class BackupController {
}
async apply(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to apply backup`, req.user)
return res.sendStatus(403)
}
var backup = this.backupManager.backups.find(b => b.id === req.params.id)
if (!backup) {
return res.sendStatus(404)
@ -48,5 +41,13 @@ class BackupController {
await this.backupManager.requestApplyBackup(backup)
res.sendStatus(200)
}
middleware(req, res, next) {
if (!req.user.isAdminOrUp) {
Logger.error(`[BackupController] Non-admin user attempting to access backups`, req.user)
return res.sendStatus(403)
}
next()
}
}
module.exports = new BackupController()

View File

@ -106,13 +106,20 @@ class BackupManager {
this.backups.push(backup)
}
return res.json(this.backups.map(b => b.toJSON()))
res.json({
backups: this.backups.map(b => b.toJSON())
})
}
async requestCreateBackup(res) {
var backupSuccess = await this.runBackup()
if (backupSuccess) res.json(this.backups.map(b => b.toJSON()))
else res.sendStatus(500)
if (backupSuccess) {
res.json({
backups: this.backups.map(b => b.toJSON())
})
} else {
res.sendStatus(500)
}
}
async requestApplyBackup(backup) {

View File

@ -163,10 +163,11 @@ class ApiRouter {
//
// Backup Routes
//
this.router.post('/backups', BackupController.create.bind(this))
this.router.delete('/backups/:id', BackupController.delete.bind(this))
this.router.get('/backups/:id/apply', BackupController.apply.bind(this))
this.router.post('/backups/upload', BackupController.upload.bind(this))
this.router.get('/backups', BackupController.middleware.bind(this), BackupController.getAll.bind(this))
this.router.post('/backups', BackupController.middleware.bind(this), BackupController.create.bind(this))
this.router.delete('/backups/:id', BackupController.middleware.bind(this), BackupController.delete.bind(this))
this.router.get('/backups/:id/apply', BackupController.middleware.bind(this), BackupController.apply.bind(this))
this.router.post('/backups/upload', BackupController.middleware.bind(this), BackupController.upload.bind(this))
//
// File System Routes