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