mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-19 20:50:09 +01:00
b35feede87
* Add support for fetching single document in documents folder * Add document object to upload + support link scraping via API * hotfixes for documentation * update api docs
55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
const path = require("path");
|
|
const fs = require("fs");
|
|
const {
|
|
WATCH_DIRECTORY,
|
|
SUPPORTED_FILETYPE_CONVERTERS,
|
|
} = require("../utils/constants");
|
|
const { trashFile } = require("../utils/files");
|
|
const RESERVED_FILES = ["__HOTDIR__.md"];
|
|
|
|
async function processSingleFile(targetFilename) {
|
|
const fullFilePath = path.resolve(WATCH_DIRECTORY, targetFilename);
|
|
if (RESERVED_FILES.includes(targetFilename))
|
|
return {
|
|
success: false,
|
|
reason: "Filename is a reserved filename and cannot be processed.",
|
|
documents: [],
|
|
};
|
|
if (!fs.existsSync(fullFilePath))
|
|
return {
|
|
success: false,
|
|
reason: "File does not exist in upload directory.",
|
|
documents: [],
|
|
};
|
|
|
|
const fileExtension = path.extname(fullFilePath).toLowerCase();
|
|
if (!fileExtension) {
|
|
return {
|
|
success: false,
|
|
reason: `No file extension found. This file cannot be processed.`,
|
|
documents: [],
|
|
};
|
|
}
|
|
|
|
if (!Object.keys(SUPPORTED_FILETYPE_CONVERTERS).includes(fileExtension)) {
|
|
trashFile(fullFilePath);
|
|
return {
|
|
success: false,
|
|
reason: `File extension ${fileExtension} not supported for parsing.`,
|
|
documents: [],
|
|
};
|
|
}
|
|
|
|
const FileTypeProcessor = require(SUPPORTED_FILETYPE_CONVERTERS[
|
|
fileExtension
|
|
]);
|
|
return await FileTypeProcessor({
|
|
fullFilePath,
|
|
filename: targetFilename,
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
processSingleFile,
|
|
};
|