Skip to content

Commit

Permalink
feat(api,admin):enforce 15MB file limit for dematerialized feasibilit…
Browse files Browse the repository at this point in the history
…y attachments
  • Loading branch information
pierreavizou committed Nov 14, 2024
1 parent 1daed28 commit 6146b85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "./_components/attachments.hook";

const ACCEPTED_FILE_TYPES = ".pdf, .jpg, .jpeg, .png" as const;
const MAX_FILE_SIZE = "2Mo" as const;
const MAX_FILE_SIZE = "15Mo" as const;
const hintMessage = `Formats supportés : ${ACCEPTED_FILE_TYPES} avec un poids maximum de ${MAX_FILE_SIZE}`;

const schema = z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { getDematerializedFeasibilityFileWithAttachmentsByCandidacyId } from "./
import { resetDFFSentToCandidateState } from "./resetDFFSentToCandidateState";
import { allowFileTypeByDocumentType } from "../../../../modules/shared/file/allowFileTypes";

const MAX_UPLOAD_SIZE = 15728640; // 15Mo

export const createOrUpdateAttachments = async ({
candidacyId,
input: {
Expand Down Expand Up @@ -68,19 +70,6 @@ export const createOrUpdateAttachments = async ({
existingFiles.push(...existingOtherAttachmentsFiles);
}

if (existingFiles.length) {
await Promise.all(
existingFiles.map(({ file: { path } }) => deleteFile(path)),
);
await prismaClient.dFFAttachment.deleteMany({
where: {
id: {
in: existingFiles.map(({ id }) => id),
},
},
});
}

const idCardFile = await getUploadedFile(idCard);

const equivalenceOrExemptionProofFile = equivalenceOrExemptionProof
Expand Down Expand Up @@ -139,6 +128,27 @@ export const createOrUpdateAttachments = async ({
}
}

for (const { file } of fileAndIds) {
if (file._buf.length > MAX_UPLOAD_SIZE) {
throw new Error(
`Le fichier ${file.filename} est trop volumineux (${(file._buf.length / 1024 / 1024).toFixed(2)} Mo). La taille maximale autorisée est de ${MAX_UPLOAD_SIZE / 1024 / 1024} Mo.`,
);
}
}

if (existingFiles.length) {
await Promise.all(
existingFiles.map(({ file: { path } }) => deleteFile(path)),
);
await prismaClient.dFFAttachment.deleteMany({
where: {
id: {
in: existingFiles.map(({ id }) => id),
},
},
});
}

for (const { file, filePath } of fileAndIds) {
await uploadFileToS3({
filePath,
Expand Down

0 comments on commit 6146b85

Please sign in to comment.