2023-12-15 00:14:56 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2024-02-29 19:05:03 +01:00
|
|
|
const { MimeDetector } = require("./mime");
|
2024-02-26 22:43:54 +01:00
|
|
|
|
|
|
|
function isTextType(filepath) {
|
|
|
|
try {
|
2024-02-29 19:05:03 +01:00
|
|
|
if (!fs.existsSync(filepath)) return false;
|
|
|
|
const mimeLib = new MimeDetector();
|
|
|
|
const mime = mimeLib.getType(filepath);
|
|
|
|
if (mimeLib.badMimes.includes(mime)) return false;
|
2024-02-26 22:43:54 +01:00
|
|
|
|
|
|
|
const type = mime.split("/")[0];
|
2024-02-29 19:05:03 +01:00
|
|
|
if (mimeLib.nonTextTypes.includes(type)) return false;
|
2024-02-26 22:43:54 +01:00
|
|
|
return true;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 00:14:56 +01:00
|
|
|
|
|
|
|
function trashFile(filepath) {
|
|
|
|
if (!fs.existsSync(filepath)) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const isDir = fs.lstatSync(filepath).isDirectory();
|
|
|
|
if (isDir) return;
|
|
|
|
} catch {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.rmSync(filepath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createdDate(filepath) {
|
|
|
|
try {
|
|
|
|
const { birthtimeMs, birthtime } = fs.statSync(filepath);
|
|
|
|
if (birthtimeMs === 0) throw new Error("Invalid stat for file!");
|
|
|
|
return birthtime.toLocaleString();
|
|
|
|
} catch {
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeToServerDocuments(
|
|
|
|
data = {},
|
|
|
|
filename,
|
|
|
|
destinationOverride = null
|
|
|
|
) {
|
|
|
|
const destination = destinationOverride
|
|
|
|
? path.resolve(destinationOverride)
|
|
|
|
: path.resolve(
|
2024-05-23 21:04:00 +02:00
|
|
|
__dirname,
|
|
|
|
"../../../server/storage/documents/custom-documents"
|
|
|
|
);
|
2023-12-15 00:14:56 +01:00
|
|
|
if (!fs.existsSync(destination))
|
|
|
|
fs.mkdirSync(destination, { recursive: true });
|
2024-01-17 01:04:22 +01:00
|
|
|
const destinationFilePath = path.resolve(destination, filename) + ".json";
|
2023-12-15 00:14:56 +01:00
|
|
|
|
2024-01-17 01:04:22 +01:00
|
|
|
fs.writeFileSync(destinationFilePath, JSON.stringify(data, null, 4), {
|
|
|
|
encoding: "utf-8",
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
// relative location string that can be passed into the /update-embeddings api
|
|
|
|
// that will work since we know the location exists and since we only allow
|
|
|
|
// 1-level deep folders this will always work. This still works for integrations like GitHub and YouTube.
|
|
|
|
location: destinationFilePath.split("/").slice(-2).join("/"),
|
|
|
|
};
|
2023-12-15 00:14:56 +01:00
|
|
|
}
|
|
|
|
|
2023-12-15 20:20:13 +01:00
|
|
|
// When required we can wipe the entire collector hotdir and tmp storage in case
|
|
|
|
// there were some large file failures that we unable to be removed a reboot will
|
|
|
|
// force remove them.
|
|
|
|
async function wipeCollectorStorage() {
|
|
|
|
const cleanHotDir = new Promise((resolve) => {
|
|
|
|
const directory = path.resolve(__dirname, "../../hotdir");
|
|
|
|
fs.readdir(directory, (err, files) => {
|
|
|
|
if (err) resolve();
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
if (file === "__HOTDIR__.md") continue;
|
|
|
|
try {
|
|
|
|
fs.rmSync(path.join(directory, file));
|
2024-05-23 21:04:00 +02:00
|
|
|
} catch { }
|
2023-12-15 20:20:13 +01:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const cleanTmpDir = new Promise((resolve) => {
|
|
|
|
const directory = path.resolve(__dirname, "../../storage/tmp");
|
|
|
|
fs.readdir(directory, (err, files) => {
|
|
|
|
if (err) resolve();
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
if (file === ".placeholder") continue;
|
|
|
|
try {
|
|
|
|
fs.rmSync(path.join(directory, file));
|
2024-05-23 21:04:00 +02:00
|
|
|
} catch { }
|
2023-12-15 20:20:13 +01:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([cleanHotDir, cleanTmpDir]);
|
|
|
|
console.log(`Collector hot directory and tmp storage wiped!`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-01 22:56:35 +02:00
|
|
|
/**
|
|
|
|
* Checks if a given path is within another path.
|
|
|
|
* @param {string} outer - The outer path (should be resolved).
|
|
|
|
* @param {string} inner - The inner path (should be resolved).
|
|
|
|
* @returns {boolean} - Returns true if the inner path is within the outer path, false otherwise.
|
|
|
|
*/
|
|
|
|
function isWithin(outer, inner) {
|
|
|
|
if (outer === inner) return false;
|
|
|
|
const rel = path.relative(outer, inner);
|
|
|
|
return !rel.startsWith("../") && rel !== "..";
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizePath(filepath = "") {
|
|
|
|
const result = path
|
2024-05-23 21:04:00 +02:00
|
|
|
.normalize(filepath.trim())
|
2024-04-01 22:56:35 +02:00
|
|
|
.replace(/^(\.\.(\/|\\|$))+/, "")
|
|
|
|
.trim();
|
|
|
|
if (["..", ".", "/"].includes(result)) throw new Error("Invalid path.");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-15 00:14:56 +01:00
|
|
|
module.exports = {
|
|
|
|
trashFile,
|
2024-02-26 22:43:54 +01:00
|
|
|
isTextType,
|
2023-12-15 00:14:56 +01:00
|
|
|
createdDate,
|
|
|
|
writeToServerDocuments,
|
2023-12-15 20:20:13 +01:00
|
|
|
wipeCollectorStorage,
|
2024-04-01 22:56:35 +02:00
|
|
|
normalizePath,
|
|
|
|
isWithin,
|
2023-12-15 00:14:56 +01:00
|
|
|
};
|