|
1 | 1 | "use server" |
2 | 2 |
|
3 | | -import { DATABASE_FILE } from "@/lib/db" |
| 3 | +import { prisma } from "@/lib/db" |
| 4 | +import { FILE_UPLOAD_PATH } from "@/lib/files" |
| 5 | +import { MODEL_BACKUP } from "@/models/backups" |
4 | 6 | import fs from "fs" |
| 7 | +import { mkdir } from "fs/promises" |
| 8 | +import JSZip from "jszip" |
| 9 | +import path from "path" |
| 10 | + |
| 11 | +const SUPPORTED_BACKUP_VERSIONS = ["1.0"] |
5 | 12 |
|
6 | 13 | export async function restoreBackupAction(prevState: any, formData: FormData) { |
7 | 14 | const file = formData.get("file") as File |
| 15 | + const removeExistingData = formData.get("removeExistingData") === "true" |
| 16 | + |
8 | 17 | if (!file) { |
9 | 18 | return { success: false, error: "No file provided" } |
10 | 19 | } |
11 | 20 |
|
| 21 | + // Restore tables |
12 | 22 | try { |
13 | 23 | const fileBuffer = await file.arrayBuffer() |
14 | 24 | const fileData = Buffer.from(fileBuffer) |
15 | | - fs.writeFileSync(DATABASE_FILE, fileData) |
| 25 | + const zip = await JSZip.loadAsync(fileData) |
| 26 | + |
| 27 | + // Check backup version |
| 28 | + const metadataFile = zip.file("data/metadata.json") |
| 29 | + if (metadataFile) { |
| 30 | + const metadataContent = await metadataFile.async("string") |
| 31 | + try { |
| 32 | + const metadata = JSON.parse(metadataContent) |
| 33 | + if (!metadata.version || !SUPPORTED_BACKUP_VERSIONS.includes(metadata.version)) { |
| 34 | + return { |
| 35 | + success: false, |
| 36 | + error: `Incompatible backup version: ${ |
| 37 | + metadata.version || "unknown" |
| 38 | + }. Supported versions: ${SUPPORTED_BACKUP_VERSIONS.join(", ")}`, |
| 39 | + } |
| 40 | + } |
| 41 | + console.log(`Restoring backup version ${metadata.version} created at ${metadata.timestamp}`) |
| 42 | + } catch (error) { |
| 43 | + console.warn("Could not parse backup metadata:", error) |
| 44 | + } |
| 45 | + } else { |
| 46 | + console.warn("No metadata found in backup, assuming legacy format") |
| 47 | + } |
| 48 | + |
| 49 | + if (removeExistingData) { |
| 50 | + await clearAllTables() |
| 51 | + } |
| 52 | + |
| 53 | + for (const { filename, model, idField } of MODEL_BACKUP) { |
| 54 | + try { |
| 55 | + const jsonFile = zip.file(`data/${filename}`) |
| 56 | + if (jsonFile) { |
| 57 | + const jsonContent = await jsonFile.async("string") |
| 58 | + const restoredCount = await restoreModelFromJSON(model, jsonContent, idField) |
| 59 | + console.log(`Restored ${restoredCount} records from ${filename}`) |
| 60 | + } |
| 61 | + } catch (error) { |
| 62 | + console.error(`Error restoring model from ${filename}:`, error) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Restore files |
| 67 | + try { |
| 68 | + const filesToRestore = Object.keys(zip.files).filter( |
| 69 | + (filename) => filename.startsWith("data/uploads/") && !filename.endsWith("/") |
| 70 | + ) |
| 71 | + |
| 72 | + if (filesToRestore.length > 0) { |
| 73 | + await mkdir(FILE_UPLOAD_PATH, { recursive: true }) |
| 74 | + |
| 75 | + // Extract and save each file |
| 76 | + let restoredFilesCount = 0 |
| 77 | + for (const zipFilePath of filesToRestore) { |
| 78 | + const file = zip.file(zipFilePath) |
| 79 | + if (file) { |
| 80 | + const relativeFilePath = zipFilePath.replace("data/uploads/", "") |
| 81 | + const fileContent = await file.async("nodebuffer") |
| 82 | + |
| 83 | + const filePath = path.join(FILE_UPLOAD_PATH, relativeFilePath) |
| 84 | + const fileName = path.basename(filePath) |
| 85 | + const fileId = path.basename(fileName, path.extname(fileName)) |
| 86 | + const fileDir = path.dirname(filePath) |
| 87 | + await mkdir(fileDir, { recursive: true }) |
| 88 | + |
| 89 | + // Write the file |
| 90 | + fs.writeFileSync(filePath, fileContent) |
| 91 | + restoredFilesCount++ |
| 92 | + |
| 93 | + // Update the file record |
| 94 | + await prisma.file.upsert({ |
| 95 | + where: { id: fileId }, |
| 96 | + update: { |
| 97 | + path: filePath, |
| 98 | + }, |
| 99 | + create: { |
| 100 | + id: relativeFilePath, |
| 101 | + path: filePath, |
| 102 | + filename: fileName, |
| 103 | + mimetype: "application/octet-stream", |
| 104 | + }, |
| 105 | + }) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (error) { |
| 110 | + console.error("Error restoring uploaded files:", error) |
| 111 | + return { |
| 112 | + success: false, |
| 113 | + error: `Error restoring uploaded files: ${error instanceof Error ? error.message : String(error)}`, |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return { success: true, message: `Restore completed successfully` } |
| 118 | + } catch (error) { |
| 119 | + console.error("Error restoring from backup:", error) |
| 120 | + return { |
| 121 | + success: false, |
| 122 | + error: `Error restoring from backup: ${error instanceof Error ? error.message : String(error)}`, |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +async function clearAllTables() { |
| 128 | + // Delete in reverse order to handle foreign key constraints |
| 129 | + for (const { model } of [...MODEL_BACKUP].reverse()) { |
| 130 | + try { |
| 131 | + await model.deleteMany({}) |
| 132 | + } catch (error) { |
| 133 | + console.error(`Error clearing table:`, error) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +async function restoreModelFromJSON(model: any, jsonContent: string, idField: string): Promise<number> { |
| 139 | + if (!jsonContent) return 0 |
| 140 | + |
| 141 | + try { |
| 142 | + const records = JSON.parse(jsonContent) |
| 143 | + |
| 144 | + if (!records || records.length === 0) { |
| 145 | + return 0 |
| 146 | + } |
| 147 | + |
| 148 | + let insertedCount = 0 |
| 149 | + for (const rawRecord of records) { |
| 150 | + const record = processRowData(rawRecord) |
| 151 | + |
| 152 | + try { |
| 153 | + // Skip records that don't have the required ID field |
| 154 | + if (record[idField] === undefined) { |
| 155 | + console.warn(`Skipping record missing required ID field '${idField}'`) |
| 156 | + continue |
| 157 | + } |
| 158 | + |
| 159 | + await model.upsert({ |
| 160 | + where: { [idField]: record[idField] }, |
| 161 | + update: record, |
| 162 | + create: record, |
| 163 | + }) |
| 164 | + insertedCount++ |
| 165 | + } catch (error) { |
| 166 | + console.error(`Error upserting record:`, error) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return insertedCount |
16 | 171 | } catch (error) { |
17 | | - return { success: false, error: "Failed to restore backup" } |
| 172 | + console.error(`Error parsing JSON content:`, error) |
| 173 | + return 0 |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +function processRowData(row: Record<string, any>): Record<string, any> { |
| 178 | + const processedRow: Record<string, any> = {} |
| 179 | + |
| 180 | + for (const [key, value] of Object.entries(row)) { |
| 181 | + if (value === "" || value === "null" || value === undefined) { |
| 182 | + processedRow[key] = null |
| 183 | + continue |
| 184 | + } |
| 185 | + |
| 186 | + // Try to parse JSON for object fields |
| 187 | + if (typeof value === "string" && (value.startsWith("{") || value.startsWith("["))) { |
| 188 | + try { |
| 189 | + processedRow[key] = JSON.parse(value) |
| 190 | + continue |
| 191 | + } catch (e) { |
| 192 | + // Not valid JSON, continue with normal processing |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Handle dates (checking for ISO date format) |
| 197 | + if (typeof value === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/.test(value)) { |
| 198 | + processedRow[key] = new Date(value) |
| 199 | + continue |
| 200 | + } |
| 201 | + |
| 202 | + // Handle numbers |
| 203 | + if (typeof value === "string" && !isNaN(Number(value)) && key !== "id" && !key.endsWith("Code")) { |
| 204 | + // Convert numbers but preserving string IDs |
| 205 | + processedRow[key] = Number(value) |
| 206 | + continue |
| 207 | + } |
| 208 | + |
| 209 | + // Default: keep as is |
| 210 | + processedRow[key] = value |
18 | 211 | } |
19 | 212 |
|
20 | | - return { success: true } |
| 213 | + return processedRow |
21 | 214 | } |
0 commit comments