2023-12-19 00:48:02 +01:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2024-01-19 21:56:00 +01:00
|
|
|
const {
|
|
|
|
purgeVectorCache,
|
|
|
|
purgeSourceDocument,
|
|
|
|
normalizePath,
|
|
|
|
isWithin,
|
|
|
|
documentsPath,
|
|
|
|
} = require(".");
|
2023-06-27 02:20:09 +02:00
|
|
|
const { Document } = require("../../models/documents");
|
|
|
|
const { Workspace } = require("../../models/workspace");
|
|
|
|
|
2024-01-19 21:56:00 +01:00
|
|
|
async function purgeDocument(filename = null) {
|
|
|
|
if (!filename || !normalizePath(filename)) return;
|
|
|
|
|
|
|
|
await purgeVectorCache(filename);
|
|
|
|
await purgeSourceDocument(filename);
|
2023-06-27 02:20:09 +02:00
|
|
|
const workspaces = await Workspace.where();
|
|
|
|
for (const workspace of workspaces) {
|
|
|
|
await Document.removeDocuments(workspace, [filename]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-19 21:56:00 +01:00
|
|
|
async function purgeFolder(folderName = null) {
|
|
|
|
if (!folderName) return;
|
|
|
|
const subFolder = normalizePath(folderName);
|
|
|
|
const subFolderPath = path.resolve(documentsPath, subFolder);
|
|
|
|
const validRemovableSubFolders = fs
|
|
|
|
.readdirSync(documentsPath)
|
|
|
|
.map((folder) => {
|
|
|
|
// Filter out any results which are not folders or
|
|
|
|
// are the protected custom-documents folder.
|
|
|
|
if (folder === "custom-documents") return null;
|
|
|
|
const subfolderPath = path.resolve(documentsPath, folder);
|
|
|
|
if (!fs.lstatSync(subfolderPath).isDirectory()) return null;
|
|
|
|
return folder;
|
|
|
|
})
|
|
|
|
.filter((subFolder) => !!subFolder);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!validRemovableSubFolders.includes(subFolder) ||
|
|
|
|
!fs.existsSync(subFolderPath) ||
|
|
|
|
!isWithin(documentsPath, subFolderPath)
|
|
|
|
)
|
|
|
|
return;
|
2023-12-19 00:48:02 +01:00
|
|
|
|
|
|
|
const filenames = fs
|
2024-01-19 21:56:00 +01:00
|
|
|
.readdirSync(subFolderPath)
|
|
|
|
.map((file) => path.join(subFolderPath, file));
|
2023-12-19 00:48:02 +01:00
|
|
|
const workspaces = await Workspace.where();
|
|
|
|
|
|
|
|
const purgePromises = [];
|
|
|
|
// Remove associated Vector-cache files
|
|
|
|
for (const filename of filenames) {
|
|
|
|
const rmVectorCache = () =>
|
|
|
|
new Promise((resolve) =>
|
|
|
|
purgeVectorCache(filename).then(() => resolve(true))
|
|
|
|
);
|
|
|
|
purgePromises.push(rmVectorCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove workspace document associations
|
|
|
|
for (const workspace of workspaces) {
|
|
|
|
const rmWorkspaceDoc = () =>
|
|
|
|
new Promise((resolve) =>
|
|
|
|
Document.removeDocuments(workspace, filenames).then(() => resolve(true))
|
|
|
|
);
|
|
|
|
purgePromises.push(rmWorkspaceDoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(purgePromises.flat().map((f) => f()));
|
2024-01-19 21:56:00 +01:00
|
|
|
fs.rmSync(subFolderPath, { recursive: true }); // Delete target document-folder and source files.
|
2023-12-19 00:48:02 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-27 02:20:09 +02:00
|
|
|
module.exports = {
|
|
|
|
purgeDocument,
|
2023-12-19 00:48:02 +01:00
|
|
|
purgeFolder,
|
2023-06-27 02:20:09 +02:00
|
|
|
};
|