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, showConfirmApply: false,
selectedBackup: null, selectedBackup: null,
isBackingUp: false, isBackingUp: false,
processing: false processing: false,
backups: []
} }
}, },
computed: { computed: {
backups() {
return this.$store.state.backups || []
},
userToken() { userToken() {
return this.$store.getters['user/getToken'] return this.$store.getters['user/getToken']
} }
@ -96,9 +94,8 @@ export default {
this.processing = true this.processing = true
this.$axios this.$axios
.$delete(`/api/backups/${backup.id}`) .$delete(`/api/backups/${backup.id}`)
.then((backups) => { .then((data) => {
console.log('Backup deleted', backups) this.setBackups(data.backups || [])
this.$store.commit('setBackups', backups)
this.$toast.success(this.$strings.ToastBackupDeleteSuccess) this.$toast.success(this.$strings.ToastBackupDeleteSuccess)
this.processing = false this.processing = false
}) })
@ -117,10 +114,10 @@ export default {
this.isBackingUp = true this.isBackingUp = true
this.$axios this.$axios
.$post('/api/backups') .$post('/api/backups')
.then((backups) => { .then((data) => {
this.isBackingUp = false this.isBackingUp = false
this.$toast.success(this.$strings.ToastBackupCreateSuccess) this.$toast.success(this.$strings.ToastBackupCreateSuccess)
this.$store.commit('setBackups', backups) this.setBackups(data.backups || [])
}) })
.catch((error) => { .catch((error) => {
this.isBackingUp = false this.isBackingUp = false
@ -136,9 +133,8 @@ export default {
this.$axios this.$axios
.$post('/api/backups/upload', form) .$post('/api/backups/upload', form)
.then((result) => { .then((data) => {
console.log('Upload backup result', result) this.setBackups(data.backups || [])
this.$store.commit('setBackups', result)
this.$toast.success(this.$strings.ToastBackupUploadSuccess) this.$toast.success(this.$strings.ToastBackupUploadSuccess)
this.processing = false this.processing = false
}) })
@ -148,9 +144,29 @@ export default {
this.$toast.error(errorMessage) this.$toast.error(errorMessage)
this.processing = false 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() { mounted() {
this.loadBackups()
if (this.$route.query.backup) { if (this.$route.query.backup) {
this.$toast.success('Backup applied successfully') this.$toast.success('Backup applied successfully')
this.$router.replace('/config') 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) { if (payload.usersOnline) {
this.$store.commit('users/resetUsers') this.$store.commit('users/resetUsers')
payload.usersOnline.forEach((user) => { payload.usersOnline.forEach((user) => {

View File

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

View File

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

View File

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

View File

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

View File

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