mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
feat: backup + restore
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { DATABASE_FILE } from "@/lib/db"
|
||||
import { FILE_UPLOAD_PATH } from "@/lib/files"
|
||||
import { MODEL_BACKUP } from "@/models/backups"
|
||||
import fs, { readdirSync } from "fs"
|
||||
import JSZip from "jszip"
|
||||
import { NextResponse } from "next/server"
|
||||
import path from "path"
|
||||
|
||||
const MAX_FILE_SIZE = 64 * 1024 * 1024 // 64MB
|
||||
const BACKUP_VERSION = "1.0"
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const zip = new JSZip()
|
||||
@@ -14,8 +17,29 @@ export async function GET(request: Request) {
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
|
||||
const databaseFile = fs.readFileSync(DATABASE_FILE)
|
||||
rootFolder.file("database.sqlite", databaseFile)
|
||||
// Add metadata with version information
|
||||
rootFolder.file(
|
||||
"metadata.json",
|
||||
JSON.stringify(
|
||||
{
|
||||
version: BACKUP_VERSION,
|
||||
timestamp: new Date().toISOString(),
|
||||
models: MODEL_BACKUP.map((m) => m.filename),
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
)
|
||||
|
||||
// Backup models
|
||||
for (const { filename, model } of MODEL_BACKUP) {
|
||||
try {
|
||||
const jsonContent = await tableToJSON(model)
|
||||
rootFolder.file(filename, jsonContent)
|
||||
} catch (error) {
|
||||
console.error(`Error exporting table ${filename}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
const uploadsFolder = rootFolder.folder("uploads")
|
||||
if (!uploadsFolder) {
|
||||
@@ -25,7 +49,23 @@ export async function GET(request: Request) {
|
||||
|
||||
const uploadedFiles = getAllFilePaths(FILE_UPLOAD_PATH)
|
||||
uploadedFiles.forEach((file) => {
|
||||
uploadsFolder.file(file.replace(FILE_UPLOAD_PATH, ""), fs.readFileSync(file))
|
||||
try {
|
||||
// Check file size before reading
|
||||
const stats = fs.statSync(file)
|
||||
if (stats.size > MAX_FILE_SIZE) {
|
||||
console.warn(
|
||||
`Skipping large file ${file} (${Math.round(stats.size / 1024 / 1024)}MB > ${
|
||||
MAX_FILE_SIZE / 1024 / 1024
|
||||
}MB limit)`
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(file)
|
||||
uploadsFolder.file(file.replace(FILE_UPLOAD_PATH, ""), fileContent)
|
||||
} catch (error) {
|
||||
console.error(`Error reading file ${file}:`, error)
|
||||
}
|
||||
})
|
||||
const archive = await zip.generateAsync({ type: "blob" })
|
||||
|
||||
@@ -60,3 +100,13 @@ function getAllFilePaths(dirPath: string): string[] {
|
||||
readDirectory(dirPath)
|
||||
return filePaths
|
||||
}
|
||||
|
||||
async function tableToJSON(model: any): Promise<string> {
|
||||
const data = await model.findMany()
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
return "[]"
|
||||
}
|
||||
|
||||
return JSON.stringify(data, null, 2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user