| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- // lib/storage.js
- import fs from "fs/promises";
- import path from "path";
- const ROOT = process.env.NAS_ROOT_PATH;
- function fullPath(...segments) {
- if (!ROOT) {
- throw new Error("NAS_ROOT_PATH ist nicht gesetzt");
- }
- return path.join(ROOT, ...segments.map(String));
- }
- function sortNumericStrings(a, b) {
- const na = parseInt(a, 10);
- const nb = parseInt(b, 10);
- if (!Number.isNaN(na) && !Number.isNaN(nb)) {
- return na - nb;
- }
- return a.localeCompare(b, "de");
- }
- export async function listBranches() {
- const entries = await fs.readdir(fullPath(), { withFileTypes: true });
- return entries
- .filter(
- (e) =>
- e.isDirectory() &&
- e.name !== "@Recently-Snapshot" &&
- /^NL\d+$/i.test(e.name)
- )
- .map((e) => e.name)
- .sort((a, b) =>
- sortNumericStrings(a.replace("NL", ""), b.replace("NL", ""))
- );
- }
- export async function listYears(branch) {
- const dir = fullPath(branch);
- const entries = await fs.readdir(dir, { withFileTypes: true });
- return entries
- .filter((e) => e.isDirectory() && /^\d{4}$/.test(e.name))
- .map((e) => e.name)
- .sort(sortNumericStrings);
- }
- export async function listMonths(branch, year) {
- const dir = fullPath(branch, year);
- const entries = await fs.readdir(dir, { withFileTypes: true });
- return entries
- .filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
- .map((e) => e.name.padStart(2, "0"))
- .sort(sortNumericStrings);
- }
- export async function listDays(branch, year, month) {
- const dir = fullPath(branch, year, month);
- const entries = await fs.readdir(dir, { withFileTypes: true });
- return entries
- .filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
- .map((e) => e.name.padStart(2, "0"))
- .sort(sortNumericStrings);
- }
- export async function listFiles(branch, year, month, day) {
- const dir = fullPath(branch, year, month, day);
- const entries = await fs.readdir(dir);
- return entries
- .filter((name) => name.toLowerCase().endsWith(".pdf"))
- .sort((a, b) => a.localeCompare(b, "de"))
- .map((name) => ({
- name,
- relativePath: `${branch}/${year}/${month}/${day}/${name}`,
- }));
- }
|