feat: implement database backup and restore functionality in settings page
- Added UI components for downloading and restoring the application database, enhancing data management capabilities. - Integrated new API routes for system database operations in the backend. - Implemented state management for backup and restore processes, including loading indicators and user notifications.
This commit is contained in:
1 parent
1fefed2aa0
commit
bb07ba0f69
5 files changed
+390
-1
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import type { FastifyPluginAsync } from "fastify"
|
||||
import { appendEvent } from "../modules/events/service/events-service.js"
|
||||
import {
|
||||
exportSystemDatabaseBackup,
|
||||
getSystemDatabasePath,
|
||||
restoreSystemDatabaseBackup,
|
||||
} from "../services/system-database-backup.js"
|
||||
|
||||
const systemDatabaseRoutes: FastifyPluginAsync = async (app) => {
|
||||
app.addContentTypeParser(
|
||||
"application/octet-stream",
|
||||
{ parseAs: "buffer", bodyLimit: 512 * 1024 * 1024 },
|
||||
(_req, body, done) => {
|
||||
done(null, body)
|
||||
},
|
||||
)
|
||||
|
||||
app.get("/system/database/backup", async (_req, reply) => {
|
||||
try {
|
||||
const { filename, buffer } = await exportSystemDatabaseBackup()
|
||||
appendEvent({
|
||||
level: "info",
|
||||
eventType: "system.database.backup",
|
||||
sourceModule: "system",
|
||||
title: "Создан бэкап базы приложения",
|
||||
message: filename,
|
||||
entityType: "system_database",
|
||||
entityId: filename,
|
||||
payload: {
|
||||
filename,
|
||||
sizeBytes: buffer.length,
|
||||
databasePath: getSystemDatabasePath(),
|
||||
},
|
||||
})
|
||||
reply.header("Content-Type", "application/octet-stream")
|
||||
reply.header("Content-Disposition", `attachment; filename="${filename}"`)
|
||||
return reply.send(buffer)
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
return reply.status(500).send({ error: message })
|
||||
}
|
||||
})
|
||||
|
||||
app.post("/system/database/restore", async (req, reply) => {
|
||||
const body = req.body
|
||||
if (!Buffer.isBuffer(body) || body.length === 0) {
|
||||
return reply.status(400).send({ error: "Ожидается тело запроса с файлом SQLite" })
|
||||
}
|
||||
try {
|
||||
await restoreSystemDatabaseBackup(body)
|
||||
appendEvent({
|
||||
level: "warning",
|
||||
eventType: "system.database.restore",
|
||||
sourceModule: "system",
|
||||
title: "Восстановлена база приложения",
|
||||
message: "Данные SQLite заменены из загруженного файла",
|
||||
entityType: "system_database",
|
||||
entityId: "restore",
|
||||
payload: {
|
||||
sizeBytes: body.length,
|
||||
databasePath: getSystemDatabasePath(),
|
||||
},
|
||||
})
|
||||
return reply.send({ ok: true })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
const status = message.includes("уже выполняется") ? 409 : 400
|
||||
return reply.status(status).send({ error: message })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default systemDatabaseRoutes
|
||||
Reference in new issue
Block a user