2023-12-15 00:14:56 +01:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
|
|
|
const {
|
|
|
|
WATCH_DIRECTORY,
|
|
|
|
SUPPORTED_FILETYPE_CONVERTERS,
|
|
|
|
} = require("../utils/constants");
|
2024-04-01 22:56:35 +02:00
|
|
|
const {
|
|
|
|
trashFile,
|
|
|
|
isTextType,
|
|
|
|
normalizePath,
|
|
|
|
isWithin,
|
|
|
|
} = require("../utils/files");
|
2023-12-21 04:41:16 +01:00
|
|
|
const RESERVED_FILES = ["__HOTDIR__.md"];
|
2023-12-15 00:14:56 +01:00
|
|
|
|
2024-03-14 23:43:26 +01:00
|
|
|
async function processSingleFile(targetFilename, options = {}) {
|
2024-04-01 22:56:35 +02:00
|
|
|
const fullFilePath = path.resolve(
|
|
|
|
WATCH_DIRECTORY,
|
|
|
|
normalizePath(targetFilename)
|
|
|
|
);
|
|
|
|
if (!isWithin(path.resolve(WATCH_DIRECTORY), fullFilePath))
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: "Filename is a not a valid path to process.",
|
|
|
|
documents: [],
|
|
|
|
};
|
|
|
|
|
2023-12-15 00:14:56 +01:00
|
|
|
if (RESERVED_FILES.includes(targetFilename))
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: "Filename is a reserved filename and cannot be processed.",
|
2024-01-17 01:04:22 +01:00
|
|
|
documents: [],
|
2023-12-15 00:14:56 +01:00
|
|
|
};
|
|
|
|
if (!fs.existsSync(fullFilePath))
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: "File does not exist in upload directory.",
|
2024-01-17 01:04:22 +01:00
|
|
|
documents: [],
|
2023-12-15 00:14:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const fileExtension = path.extname(fullFilePath).toLowerCase();
|
|
|
|
if (!fileExtension) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: `No file extension found. This file cannot be processed.`,
|
2024-01-17 01:04:22 +01:00
|
|
|
documents: [],
|
2023-12-15 00:14:56 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-26 22:43:54 +01:00
|
|
|
let processFileAs = fileExtension;
|
|
|
|
if (!SUPPORTED_FILETYPE_CONVERTERS.hasOwnProperty(fileExtension)) {
|
|
|
|
if (isTextType(fullFilePath)) {
|
|
|
|
console.log(
|
|
|
|
`\x1b[33m[Collector]\x1b[0m The provided filetype of ${fileExtension} does not have a preset and will be processed as .txt.`
|
|
|
|
);
|
|
|
|
processFileAs = ".txt";
|
|
|
|
} else {
|
|
|
|
trashFile(fullFilePath);
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
reason: `File extension ${fileExtension} not supported for parsing and cannot be assumed as text file type.`,
|
|
|
|
documents: [],
|
|
|
|
};
|
|
|
|
}
|
2023-12-15 00:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const FileTypeProcessor = require(SUPPORTED_FILETYPE_CONVERTERS[
|
2024-02-26 22:43:54 +01:00
|
|
|
processFileAs
|
2023-12-15 00:14:56 +01:00
|
|
|
]);
|
|
|
|
return await FileTypeProcessor({
|
|
|
|
fullFilePath,
|
|
|
|
filename: targetFilename,
|
2024-03-14 23:43:26 +01:00
|
|
|
options,
|
2023-12-15 00:14:56 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
processSingleFile,
|
|
|
|
};
|