2024-07-18 21:35:17 +03:00
|
|
|
import assetPath from "../services/asset_path.js";
|
2024-07-18 21:37:45 +03:00
|
|
|
import path from "path";
|
2024-07-19 00:18:35 +03:00
|
|
|
import { fileURLToPath } from "url";
|
2024-07-18 21:37:45 +03:00
|
|
|
import express from "express";
|
2025-04-24 14:55:11 +03:00
|
|
|
import { getResourceDir, isDev } from "../services/utils.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type serveStatic from "serve-static";
|
2025-05-17 10:50:52 +03:00
|
|
|
import proxy from "express-http-proxy";
|
2024-12-14 10:05:38 +02:00
|
|
|
|
2025-03-08 16:01:53 +01:00
|
|
|
const persistentCacheStatic = (root: string, options?: serveStatic.ServeStaticOptions<express.Response<unknown, Record<string, unknown>>>) => {
|
2025-01-22 19:08:38 +01:00
|
|
|
if (!isDev) {
|
2023-05-07 15:23:46 +02:00
|
|
|
options = {
|
2025-01-09 18:07:02 +02:00
|
|
|
maxAge: "1y",
|
2023-05-07 15:23:46 +02:00
|
|
|
...options
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return express.static(root, options);
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-19 18:16:46 +02:00
|
|
|
async function register(app: express.Application) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const srcRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
|
2025-04-24 14:55:11 +03:00
|
|
|
const resourceDir = getResourceDir();
|
|
|
|
|
|
2025-01-22 19:08:38 +01:00
|
|
|
if (isDev) {
|
2025-04-23 10:24:05 +03:00
|
|
|
const publicUrl = process.env.TRILIUM_PUBLIC_SERVER;
|
|
|
|
|
if (!publicUrl) {
|
|
|
|
|
throw new Error("Missing TRILIUM_PUBLIC_SERVER");
|
|
|
|
|
}
|
2025-05-19 20:04:51 +03:00
|
|
|
app.use(assetPath + `/@fs`, proxy(publicUrl, {
|
|
|
|
|
proxyReqPathResolver: (req) => assetPath + `/@fs` + req.url
|
2025-05-17 10:50:52 +03:00
|
|
|
}))
|
2024-12-14 10:10:10 +02:00
|
|
|
} else {
|
2025-05-19 20:04:51 +03:00
|
|
|
app.use(`/${assetPath}/src`, persistentCacheStatic(path.join(resourceDir, "public", "src")));
|
2025-04-24 14:55:11 +03:00
|
|
|
app.use(`/${assetPath}/stylesheets`, persistentCacheStatic(path.join(resourceDir, "public", "stylesheets")));
|
|
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(resourceDir, "public", "libraries")));
|
2025-04-24 22:36:10 +03:00
|
|
|
app.use(`/${assetPath}/fonts`, persistentCacheStatic(path.join(resourceDir, "public", "fonts")));
|
2025-04-25 14:06:33 +03:00
|
|
|
app.use(`/${assetPath}/translations/`, persistentCacheStatic(path.join(resourceDir, "public", "translations")));
|
2025-04-24 14:55:11 +03:00
|
|
|
app.use(`/${assetPath}/images`, persistentCacheStatic(path.join(resourceDir, "assets", "images")));
|
2025-05-02 19:23:29 +03:00
|
|
|
app.use(`/${assetPath}/app-dist/doc_notes`, persistentCacheStatic(path.join(resourceDir, "assets", "doc_notes")));
|
2024-12-14 10:10:10 +02:00
|
|
|
}
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/fonts`, express.static(path.join(srcRoot, "public/fonts")));
|
2025-04-23 10:06:37 +03:00
|
|
|
app.use(`/assets/vX/images`, express.static(path.join(srcRoot, "..", "images")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/stylesheets`, express.static(path.join(srcRoot, "public/stylesheets")));
|
2025-04-18 21:59:14 +03:00
|
|
|
app.use(`/${assetPath}/libraries`, persistentCacheStatic(path.join(srcRoot, "public/libraries")));
|
2025-01-09 18:07:02 +02:00
|
|
|
app.use(`/assets/vX/libraries`, express.static(path.join(srcRoot, "..", "libraries")));
|
2023-11-22 19:58:54 +01:00
|
|
|
}
|
2023-05-07 15:23:46 +02:00
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2023-05-07 15:23:46 +02:00
|
|
|
register
|
|
|
|
|
};
|