Adding mysql support.

This commit is contained in:
charlesgauthereau
2025-11-01 23:16:32 +01:00
parent 5251e01011
commit 2fdd9f1296
9 changed files with 1388 additions and 14 deletions
+15 -4
View File
@@ -2,7 +2,7 @@ import fs from "node:fs";
import forge from "node-forge";
export async function decryptedDump(file: File, aesKeyHex: string, ivHex: string): Promise<File> {
export async function decryptedDump(file: File, aesKeyHex: string, ivHex: string, fileExtension: string ): Promise<File> {
const privateKeyPem = fs.readFileSync("private/keys/server_private.pem", "utf8");
const privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
@@ -10,7 +10,7 @@ export async function decryptedDump(file: File, aesKeyHex: string, ivHex: string
const encryptedAesKey = forge.util.hexToBytes(aesKeyHex);
const aesKey = privateKey.decrypt(encryptedAesKey, "RSA-OAEP", {
md: forge.md.sha256.create(),
mgf1: { md: forge.md.sha256.create() },
mgf1: {md: forge.md.sha256.create()},
});
// Read encrypted file content
@@ -19,7 +19,7 @@ export async function decryptedDump(file: File, aesKeyHex: string, ivHex: string
// AES decryption
const decipher = forge.cipher.createDecipher("AES-CBC", aesKey);
decipher.start({ iv });
decipher.start({iv});
decipher.update(forge.util.createBuffer(encryptedBuffer.toString("binary")));
const success = decipher.finish();
@@ -33,8 +33,19 @@ export async function decryptedDump(file: File, aesKeyHex: string, ivHex: string
// Return a File so you can use file.arrayBuffer() later
return new File(
[decryptedBuffer],
file.name.replace(/\.enc$/, ".dump"), // rename if needed
file.name.replace(/\.enc$/, fileExtension),
{type: "application/octet-stream"}
);
}
export function getFileExtension(dbType: string) {
switch (dbType) {
case "postgresql":
return ".dump";
case "mysql":
return ".sql";
default:
return ".dump";
}
}
+4 -5
View File
@@ -9,7 +9,7 @@ import {Backup} from "@/db/schema/07_database";
import {and, eq} from "drizzle-orm";
import {env} from "@/env.mjs";
import {withUpdatedAt} from "@/db/utils";
import {decryptedDump} from "./helpers";
import {decryptedDump, getFileExtension} from "./helpers";
export async function POST(
request: Request,
@@ -116,11 +116,10 @@ export async function POST(
{status: 400}
);
}
const decryptedFile = await decryptedDump(file, aesKeyHex, ivHex);
const fileExtension = getFileExtension(database.dbms)
const decryptedFile = await decryptedDump(file, aesKeyHex, ivHex, fileExtension);
const uuid = uuidv4();
const fileName = `${uuid}.dump`;
const fileName = `${uuid}${fileExtension}`;
const buffer = Buffer.from(await decryptedFile.arrayBuffer());
const [settings] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);