feat: include uploads folder in network sync (all three subfolders)
Daily sync and Sync Now both pull database + uploads in one operation. PC1 streams uploads/ as a zip via GET /network-backup-files (archiver). PC2 clears cloud-storage, patients, and patient-documents then extracts the fresh copy before resolving. Timeout extended to 5 min for large files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,13 +7,16 @@ import multer from "multer";
|
||||
import { prisma } from "@repo/db/client";
|
||||
import { storage } from "../storage";
|
||||
import { backupDatabaseToPath } from "../services/databaseBackupService";
|
||||
import archiver from "archiver";
|
||||
import {
|
||||
getOrCreateApiKey,
|
||||
regenerateApiKey,
|
||||
readSyncConfig,
|
||||
writeSyncConfig,
|
||||
} from "../services/networkSyncConfigService";
|
||||
import { runNetworkSync } from "../services/networkSyncService";
|
||||
import { runNetworkSync, runNetworkFilesSync } from "../services/networkSyncService";
|
||||
|
||||
const UPLOADS_DIR = path.join(process.cwd(), "uploads");
|
||||
|
||||
const MIGRATIONS_DIR = path.resolve(__dirname, "../../../../packages/db/prisma/migrations");
|
||||
|
||||
@@ -532,6 +535,31 @@ router.get("/network-backup", async (req: Request, res: Response): Promise<any>
|
||||
});
|
||||
});
|
||||
|
||||
// GET /network-backup-files — streams uploads/ as a zip; authenticated by API key header only
|
||||
router.get("/network-backup-files", (req: Request, res: Response): any => {
|
||||
const providedKey = req.headers["x-network-backup-key"] as string | undefined;
|
||||
if (!providedKey) return res.status(401).json({ error: "Missing X-Network-Backup-Key header" });
|
||||
|
||||
const storedKey = getOrCreateApiKey();
|
||||
if (providedKey !== storedKey) return res.status(401).json({ error: "Invalid API key" });
|
||||
|
||||
if (!fs.existsSync(UPLOADS_DIR)) {
|
||||
return res.status(200).end(); // nothing to send
|
||||
}
|
||||
|
||||
res.setHeader("Content-Type", "application/zip");
|
||||
res.setHeader("Content-Disposition", `attachment; filename="network_uploads_${Date.now()}.zip"`);
|
||||
|
||||
const archive = archiver("zip", { zlib: { level: 6 } });
|
||||
archive.on("error", (err) => {
|
||||
if (!res.headersSent) res.status(500).json({ error: "Failed to create archive", details: err.message });
|
||||
});
|
||||
|
||||
archive.pipe(res);
|
||||
archive.directory(UPLOADS_DIR, false); // zip contents without the "uploads" prefix
|
||||
archive.finalize();
|
||||
});
|
||||
|
||||
// ==============================
|
||||
// Network Backup — Receiver Role
|
||||
// ==============================
|
||||
@@ -563,6 +591,7 @@ router.post("/network-sync-now", async (req: Request, res: Response): Promise<an
|
||||
|
||||
try {
|
||||
await runNetworkSync(config.sourceUrl, config.apiKey);
|
||||
await runNetworkFilesSync(config.sourceUrl, config.apiKey);
|
||||
writeSyncConfig({ lastSyncAt: new Date().toISOString(), lastSyncStatus: "success", lastSyncError: null });
|
||||
res.json({ success: true, syncedAt: new Date() });
|
||||
} catch (err: any) {
|
||||
|
||||
Reference in New Issue
Block a user