mirror of
https://github.com/zadam/trilium.git
synced 2025-11-06 13:26:01 +01:00
fix(fs_sync): cls errors in router
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import express from "express";
|
|
||||||
import becca from "../../becca/becca.js";
|
import becca from "../../becca/becca.js";
|
||||||
import BFileSystemMapping from "../../becca/entities/bfile_system_mapping.js";
|
import BFileSystemMapping from "../../becca/entities/bfile_system_mapping.js";
|
||||||
import fileSystemSyncInit from "../../services/file_system_sync_init.js";
|
import fileSystemSyncInit from "../../services/file_system_sync_init.js";
|
||||||
@@ -8,8 +7,7 @@ import log from "../../services/log.js";
|
|||||||
import ValidationError from "../../errors/validation_error.js";
|
import ValidationError from "../../errors/validation_error.js";
|
||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { router, asyncApiRoute, apiRoute } from "../route_api.js";
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
interface FileStat {
|
interface FileStat {
|
||||||
isFile: boolean;
|
isFile: boolean;
|
||||||
@@ -19,8 +17,7 @@ interface FileStat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get all file system mappings
|
// Get all file system mappings
|
||||||
router.get("/mappings", (req, res) => {
|
apiRoute("get", "/mappings", () => {
|
||||||
try {
|
|
||||||
const mappings = Object.values(becca.fileSystemMappings || {}).map(mapping => ({
|
const mappings = Object.values(becca.fileSystemMappings || {}).map(mapping => ({
|
||||||
mappingId: mapping.mappingId,
|
mappingId: mapping.mappingId,
|
||||||
noteId: mapping.noteId,
|
noteId: mapping.noteId,
|
||||||
@@ -37,24 +34,19 @@ router.get("/mappings", (req, res) => {
|
|||||||
dateModified: mapping.dateModified
|
dateModified: mapping.dateModified
|
||||||
}));
|
}));
|
||||||
|
|
||||||
res.json(mappings);
|
return mappings;
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error getting file system mappings: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to get file system mappings" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get a specific file system mapping
|
// Get a specific file system mapping
|
||||||
router.get("/mappings/:mappingId", (req, res) => {
|
apiRoute("get", "/mappings/:mappingId", (req) => {
|
||||||
try {
|
|
||||||
const { mappingId } = req.params;
|
const { mappingId } = req.params;
|
||||||
const mapping = becca.fileSystemMappings[mappingId];
|
const mapping = becca.fileSystemMappings[mappingId];
|
||||||
|
|
||||||
if (!mapping) {
|
if (!mapping) {
|
||||||
return res.status(404).json({ error: "Mapping not found" });
|
return [404, { error: "Mapping not found" }];
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({
|
return {
|
||||||
mappingId: mapping.mappingId,
|
mappingId: mapping.mappingId,
|
||||||
noteId: mapping.noteId,
|
noteId: mapping.noteId,
|
||||||
filePath: mapping.filePath,
|
filePath: mapping.filePath,
|
||||||
@@ -68,16 +60,11 @@ router.get("/mappings/:mappingId", (req, res) => {
|
|||||||
syncErrors: mapping.syncErrors,
|
syncErrors: mapping.syncErrors,
|
||||||
dateCreated: mapping.dateCreated,
|
dateCreated: mapping.dateCreated,
|
||||||
dateModified: mapping.dateModified
|
dateModified: mapping.dateModified
|
||||||
});
|
};
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error getting file system mapping: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to get file system mapping" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a new file system mapping
|
// Create a new file system mapping
|
||||||
router.post("/mappings", async (req, res) => {
|
asyncApiRoute("post", "/mappings", async (req) => {
|
||||||
try {
|
|
||||||
const {
|
const {
|
||||||
noteId,
|
noteId,
|
||||||
filePath,
|
filePath,
|
||||||
@@ -138,7 +125,7 @@ router.post("/mappings", async (req, res) => {
|
|||||||
|
|
||||||
log.info(`Created file system mapping ${mapping.mappingId} for note ${noteId} -> ${normalizedPath}`);
|
log.info(`Created file system mapping ${mapping.mappingId} for note ${noteId} -> ${normalizedPath}`);
|
||||||
|
|
||||||
res.status(201).json({
|
return [201, {
|
||||||
mappingId: mapping.mappingId,
|
mappingId: mapping.mappingId,
|
||||||
noteId: mapping.noteId,
|
noteId: mapping.noteId,
|
||||||
filePath: mapping.filePath,
|
filePath: mapping.filePath,
|
||||||
@@ -148,26 +135,16 @@ router.post("/mappings", async (req, res) => {
|
|||||||
preserveHierarchy: mapping.preserveHierarchy,
|
preserveHierarchy: mapping.preserveHierarchy,
|
||||||
contentFormat: mapping.contentFormat,
|
contentFormat: mapping.contentFormat,
|
||||||
excludePatterns: mapping.excludePatterns
|
excludePatterns: mapping.excludePatterns
|
||||||
});
|
}];
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof ValidationError) {
|
|
||||||
res.status(400).json({ error: error.message });
|
|
||||||
} else {
|
|
||||||
log.error(`Error creating file system mapping: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to create file system mapping" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update a file system mapping
|
// Update a file system mapping
|
||||||
router.put("/mappings/:mappingId", async (req, res) => {
|
asyncApiRoute("put", "/mappings/:mappingId", async (req) => {
|
||||||
try {
|
|
||||||
const { mappingId } = req.params;
|
const { mappingId } = req.params;
|
||||||
const mapping = becca.fileSystemMappings[mappingId];
|
const mapping = becca.fileSystemMappings[mappingId];
|
||||||
|
|
||||||
if (!mapping) {
|
if (!mapping) {
|
||||||
return res.status(404).json({ error: "Mapping not found" });
|
return [404, { error: "Mapping not found" }];
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -225,7 +202,7 @@ router.put("/mappings/:mappingId", async (req, res) => {
|
|||||||
|
|
||||||
log.info(`Updated file system mapping ${mappingId}`);
|
log.info(`Updated file system mapping ${mappingId}`);
|
||||||
|
|
||||||
res.json({
|
return {
|
||||||
mappingId: mapping.mappingId,
|
mappingId: mapping.mappingId,
|
||||||
noteId: mapping.noteId,
|
noteId: mapping.noteId,
|
||||||
filePath: mapping.filePath,
|
filePath: mapping.filePath,
|
||||||
@@ -235,98 +212,61 @@ router.put("/mappings/:mappingId", async (req, res) => {
|
|||||||
preserveHierarchy: mapping.preserveHierarchy,
|
preserveHierarchy: mapping.preserveHierarchy,
|
||||||
contentFormat: mapping.contentFormat,
|
contentFormat: mapping.contentFormat,
|
||||||
excludePatterns: mapping.excludePatterns
|
excludePatterns: mapping.excludePatterns
|
||||||
});
|
};
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof ValidationError) {
|
|
||||||
res.status(400).json({ error: error.message });
|
|
||||||
} else {
|
|
||||||
log.error(`Error updating file system mapping: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to update file system mapping" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delete a file system mapping
|
// Delete a file system mapping
|
||||||
router.delete("/mappings/:mappingId", (req, res) => {
|
apiRoute("delete", "/mappings/:mappingId", (req) => {
|
||||||
try {
|
|
||||||
const { mappingId } = req.params;
|
const { mappingId } = req.params;
|
||||||
const mapping = becca.fileSystemMappings[mappingId];
|
const mapping = becca.fileSystemMappings[mappingId];
|
||||||
|
|
||||||
if (!mapping) {
|
if (!mapping) {
|
||||||
return res.status(404).json({ error: "Mapping not found" });
|
return [404, { error: "Mapping not found" }];
|
||||||
}
|
}
|
||||||
|
|
||||||
mapping.markAsDeleted();
|
mapping.markAsDeleted();
|
||||||
|
|
||||||
log.info(`Deleted file system mapping ${mappingId}`);
|
log.info(`Deleted file system mapping ${mappingId}`);
|
||||||
|
|
||||||
res.json({ success: true });
|
return { success: true };
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error deleting file system mapping: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to delete file system mapping" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Trigger full sync for a mapping
|
// Trigger full sync for a mapping
|
||||||
router.post("/mappings/:mappingId/sync", async (req, res) => {
|
asyncApiRoute("post", "/mappings/:mappingId/sync", async (req) => {
|
||||||
try {
|
|
||||||
const { mappingId } = req.params;
|
const { mappingId } = req.params;
|
||||||
|
|
||||||
if (!fileSystemSyncInit.isInitialized()) {
|
if (!fileSystemSyncInit.isInitialized()) {
|
||||||
return res.status(503).json({ error: "File system sync is not initialized" });
|
return [503, { error: "File system sync is not initialized" }];
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await fileSystemSyncInit.fullSync(mappingId);
|
const result = await fileSystemSyncInit.fullSync(mappingId);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
res.json(result);
|
return result;
|
||||||
} else {
|
} else {
|
||||||
res.status(400).json(result);
|
return [400, result];
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error triggering sync: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to trigger sync" });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get sync status for all mappings
|
// Get sync status for all mappings
|
||||||
router.get("/status", (req, res) => {
|
apiRoute("get", "/status", () => {
|
||||||
try {
|
return fileSystemSyncInit.getStatus();
|
||||||
const status = fileSystemSyncInit.getStatus();
|
|
||||||
res.json(status);
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error getting sync status: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to get sync status" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Enable/disable file system sync
|
// Enable file system sync
|
||||||
router.post("/enable", async (req, res) => {
|
asyncApiRoute("post", "/enable", async () => {
|
||||||
try {
|
|
||||||
await fileSystemSyncInit.enable();
|
await fileSystemSyncInit.enable();
|
||||||
res.json({ success: true, message: "File system sync enabled" });
|
return { success: true, message: "File system sync enabled" };
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error enabling file system sync: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to enable file system sync" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/disable", async (req, res) => {
|
// Disable file system sync
|
||||||
try {
|
asyncApiRoute("post", "/disable", async () => {
|
||||||
await fileSystemSyncInit.disable();
|
await fileSystemSyncInit.disable();
|
||||||
res.json({ success: true, message: "File system sync disabled" });
|
return { success: true, message: "File system sync disabled" };
|
||||||
} catch (error) {
|
|
||||||
log.error(`Error disabling file system sync: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to disable file system sync" });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate file path
|
// Validate file path
|
||||||
router.post("/validate-path", async (req, res) => {
|
asyncApiRoute("post", "/validate-path", async (req) => {
|
||||||
try {
|
|
||||||
const { filePath } = req.body;
|
const { filePath } = req.body;
|
||||||
|
|
||||||
if (!filePath) {
|
if (!filePath) {
|
||||||
@@ -347,20 +287,11 @@ router.post("/validate-path", async (req, res) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({
|
return {
|
||||||
path: normalizedPath,
|
path: normalizedPath,
|
||||||
exists,
|
exists,
|
||||||
stats
|
stats
|
||||||
});
|
};
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof ValidationError) {
|
|
||||||
res.status(400).json({ error: error.message });
|
|
||||||
} else {
|
|
||||||
log.error(`Error validating file path: ${error}`);
|
|
||||||
res.status(500).json({ error: "Failed to validate file path" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user