JavaScript 56.7%
TypeScript 42.6%
1import { readFile, writeFile } from "fs/promises";2import { existsSync } from "fs";3import { getFilePath } from "./storage";4import { encryptBuffer, decryptBuffer } from "./filecrypto";5import { prisma } from "./prisma";67// Chiffrement/déchiffrement explicite d'un fichier (action groupée "chiffrer/déchiffrer").8// Format auto-porté géré par filecrypto. Le flag File.isEncrypted indique l'état sur disque.910export async function encryptFile(fileId: string): Promise<boolean> {11 const file = await prisma.file.findUnique({ where: { id: fileId } });12 if (!file || file.isEncrypted) return false;1314 const filePath = getFilePath(file.storagePath);15 if (!existsSync(filePath)) return false;1617 const plain = await readFile(filePath);18 await writeFile(filePath, encryptBuffer(plain));19 await prisma.file.update({20 where: { id: fileId },21 data: { isEncrypted: true, encryptionIV: "v2" },22 });23 return true;24}2526export async function decryptFile(fileId: string): Promise<Buffer | null> {27 const file = await prisma.file.findUnique({ where: { id: fileId } });28 if (!file) return null;2930 const filePath = getFilePath(file.storagePath);31 if (!existsSync(filePath)) return null;3233 const raw = await readFile(filePath);34 return file.isEncrypted ? decryptBuffer(raw) : raw;35}3637export async function removeEncryption(fileId: string): Promise<boolean> {38 const file = await prisma.file.findUnique({ where: { id: fileId } });39 if (!file || !file.isEncrypted) return false;4041 const filePath = getFilePath(file.storagePath);42 if (!existsSync(filePath)) return false;4344 const raw = await readFile(filePath);45 await writeFile(filePath, decryptBuffer(raw));46 await prisma.file.update({47 where: { id: fileId },48 data: { isEncrypted: false, encryptionIV: null },49 });50 return true;51}52