forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileValidation.js
More file actions
34 lines (29 loc) · 1.1 KB
/
Copy pathfileValidation.js
File metadata and controls
34 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { fileTypeFromBuffer } from "file-type";
import logger from "../config/logger.js";
/**
* Validates the magic bytes of a file buffer against a list of allowed MIME types.
* @param {Buffer} buffer - The file buffer to check.
* @param {string[]} allowedMimeTypes - Array of allowed MIME types (e.g., ['image/jpeg', 'application/pdf']).
* @returns {Promise<boolean>} True if the file type matches one of the allowed types.
*/
export const validateMagicBytes = async (buffer, allowedMimeTypes) => {
if (!buffer || buffer.length === 0) {
logger.warn("Empty buffer provided for magic byte validation");
return false;
}
try {
const type = await fileTypeFromBuffer(buffer);
if (!type) {
logger.warn("Could not determine file type from buffer");
return false;
}
const isValid = allowedMimeTypes.includes(type.mime);
if (!isValid) {
logger.warn(`File type mismatch: expected one of [${allowedMimeTypes.join(", ")}], got ${type.mime}`);
}
return isValid;
} catch (error) {
logger.error("Error validating magic bytes:", error);
return false;
}
};