Files
ignis/server/routes/vault.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

2026-03-10 22:31:01 +01:00
const express = require("express");
const fs = require("fs");
const config = require("../config");
const path = require("path");
2026-03-07 09:51:37 +01:00
const router = express.Router();
2026-03-11 22:08:30 +01:00
// GET /api/vault/list - returns all discovered vaults (re-scans on each call)
2026-03-10 22:31:01 +01:00
router.get("/list", (req, res) => {
config.refreshVaults();
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
const list = Object.entries(config.vaults).map(([id, vaultPath]) => ({
id,
name: id,
path: vaultPath,
}));
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
res.json(list);
});
2026-03-11 22:08:30 +01:00
// GET /api/vault/info?vault=<id> - returns info for a specific vault
2026-03-10 22:31:01 +01:00
router.get("/info", (req, res) => {
const vaultId = req.query.vault || config.defaultVaultId;
const vaultPath = config.getVaultPath(vaultId);
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
if (!vaultPath) {
return res.status(404).json({ error: "Vault not found", id: vaultId });
}
2026-03-17 12:38:30 +01:00
2026-03-07 09:51:37 +01:00
res.json({
2026-03-10 22:31:01 +01:00
id: vaultId,
name: vaultId,
path: vaultPath,
2026-03-07 09:51:37 +01:00
platform: process.platform,
2026-03-12 22:46:53 +01:00
version: config.obsidianVersion,
2026-03-07 09:51:37 +01:00
});
});
2026-03-11 22:08:30 +01:00
// POST /api/vault/create { name } - create a new vault in VAULT_ROOT
2026-03-10 22:31:01 +01:00
router.post("/create", async (req, res) => {
const name = req.body?.name;
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
if (!name || /[\/\\:*?"<>|]/.test(name)) {
return res.status(400).json({ error: "Invalid vault name" });
}
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
const vaultPath = path.join(config.vaultRoot, name);
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
try {
await fs.promises.mkdir(vaultPath, { recursive: false });
await fs.promises.mkdir(path.join(vaultPath, ".obsidian"), {
recursive: false,
});
2026-03-17 12:38:30 +01:00
2026-03-18 02:38:36 +01:00
// Install ignis-bridge plugin
const pluginDir = path.join(
vaultPath,
".obsidian",
"plugins",
"ignis-bridge",
);
await fs.promises.mkdir(pluginDir, { recursive: true });
const pluginSrcDir = path.join(__dirname, "..", "..", "plugin");
await fs.promises.copyFile(
path.join(pluginSrcDir, "manifest.json"),
path.join(pluginDir, "manifest.json"),
);
await fs.promises.copyFile(
path.join(pluginSrcDir, "main.js"),
path.join(pluginDir, "main.js"),
);
// Enable the plugin
await fs.promises.writeFile(
path.join(vaultPath, ".obsidian", "community-plugins.json"),
JSON.stringify(["ignis-bridge"]),
);
2026-03-10 22:31:01 +01:00
config.refreshVaults();
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
res.json({ ok: true, id: name, path: vaultPath });
} catch (e) {
if (e.code === "EEXIST") {
return res.status(409).json({ error: "Vault already exists" });
}
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
res.status(500).json({ error: e.message, code: e.code });
}
});
2026-03-11 22:08:30 +01:00
// DELETE /api/vault/remove?vault=<id> - remove a vault from disk
2026-03-10 22:31:01 +01:00
router.delete("/remove", async (req, res) => {
const vaultId = req.query.vault;
const vaultPath = config.getVaultPath(vaultId);
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
if (!vaultPath) {
return res.status(404).json({ error: "Vault not found" });
}
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
try {
await fs.promises.rm(vaultPath, { recursive: true });
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
config.refreshVaults();
2026-03-17 12:38:30 +01:00
2026-03-10 22:31:01 +01:00
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message, code: e.code });
}
});
2026-03-07 09:51:37 +01:00
module.exports = router;