generated from Dicken/dickendock
Bugfixes (Dark Mode, Dropdown-Kontrast, IP-Suche), Lesezeichen-Ausbau (Farbe, Beschreibung, Favicon), Zuletzt-besucht, Spaeter-lesen, kombinierter Import-Export, Scanner-Reconciliation, Admin-Ueberarbeitung
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import * as XLSX from "xlsx";
|
||||
import { ServiceCreateSchema, ServiceReorderSchema, ServiceUpdateSchema } from "@launchpad/shared";
|
||||
import * as deviceRepo from "../db/repositories/devices.js";
|
||||
import * as serviceRepo from "../db/repositories/services.js";
|
||||
import * as categoryRepo from "../db/repositories/categories.js";
|
||||
|
||||
interface ServiceListQuery {
|
||||
deviceId?: string;
|
||||
@@ -11,45 +9,6 @@ interface ServiceListQuery {
|
||||
favorite?: string;
|
||||
}
|
||||
|
||||
interface ExportRow {
|
||||
displayName: string;
|
||||
category: string;
|
||||
alias: string;
|
||||
favorite: string;
|
||||
visible: string;
|
||||
order: number;
|
||||
hostname: string;
|
||||
port: number;
|
||||
https: string;
|
||||
url: string;
|
||||
deviceHostname: string;
|
||||
deviceIp: string;
|
||||
}
|
||||
|
||||
function buildExportRows(): ExportRow[] {
|
||||
const services = serviceRepo.listServices();
|
||||
const devices = deviceRepo.listDevices();
|
||||
const deviceById = new Map(devices.map((d) => [d.id, d]));
|
||||
|
||||
return services.map((s) => {
|
||||
const device = deviceById.get(s.deviceId);
|
||||
return {
|
||||
displayName: s.displayName,
|
||||
category: s.category ?? "",
|
||||
alias: s.alias.join(";"),
|
||||
favorite: s.favorite ? "true" : "false",
|
||||
visible: s.visible ? "true" : "false",
|
||||
order: s.order,
|
||||
hostname: s.hostname,
|
||||
port: s.port,
|
||||
https: s.https ? "true" : "false",
|
||||
url: s.url,
|
||||
deviceHostname: device?.hostname ?? "",
|
||||
deviceIp: device?.ip ?? "",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function serviceRoutes(app: FastifyInstance): Promise<void> {
|
||||
app.get("/api/services", async (request) => {
|
||||
const query = request.query as ServiceListQuery;
|
||||
@@ -84,136 +43,6 @@ export async function serviceRoutes(app: FastifyInstance): Promise<void> {
|
||||
return reply.code(201).send(service);
|
||||
});
|
||||
|
||||
// Müssen vor der /:id-Route stehen, damit "export"/"import" nicht als ID interpretiert wird.
|
||||
app.get("/api/services/export", async (request, reply) => {
|
||||
const query = request.query as { format?: string };
|
||||
const format = query.format === "xlsx" ? "xlsx" : query.format === "json" ? "json" : "csv";
|
||||
const rows = buildExportRows();
|
||||
|
||||
if (format === "json") {
|
||||
reply.header("Content-Disposition", 'attachment; filename="launchpad-services.json"');
|
||||
reply.type("application/json");
|
||||
return rows;
|
||||
}
|
||||
|
||||
const worksheet = XLSX.utils.json_to_sheet(rows);
|
||||
|
||||
if (format === "xlsx") {
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "Dienste");
|
||||
const buffer = XLSX.write(workbook, { type: "buffer", bookType: "xlsx" }) as Buffer;
|
||||
reply.header("Content-Disposition", 'attachment; filename="launchpad-services.xlsx"');
|
||||
reply.type("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
return reply.send(buffer);
|
||||
}
|
||||
|
||||
const csv = XLSX.utils.sheet_to_csv(worksheet);
|
||||
reply.header("Content-Disposition", 'attachment; filename="launchpad-services.csv"');
|
||||
reply.type("text/csv; charset=utf-8");
|
||||
return reply.send(csv);
|
||||
});
|
||||
|
||||
app.post("/api/services/import", async (request, reply) => {
|
||||
const body = request.body as { format?: string; content?: string } | undefined;
|
||||
if (!body?.content) {
|
||||
return reply.code(400).send({ error: "Kein Dateiinhalt übermittelt" });
|
||||
}
|
||||
|
||||
let rows: Record<string, unknown>[];
|
||||
try {
|
||||
if (body.format === "json") {
|
||||
const text = Buffer.from(body.content, "base64").toString("utf-8");
|
||||
rows = JSON.parse(text);
|
||||
} else {
|
||||
const buffer = Buffer.from(body.content, "base64");
|
||||
const workbook = XLSX.read(buffer, { type: "buffer" });
|
||||
const sheetName = workbook.SheetNames[0];
|
||||
rows = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { defval: "" });
|
||||
}
|
||||
} catch (err) {
|
||||
return reply.code(400).send({
|
||||
error: "Datei konnte nicht gelesen werden",
|
||||
detail: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
|
||||
let imported = 0;
|
||||
let skipped = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
try {
|
||||
const deviceHostname = String(row.deviceHostname ?? "").trim();
|
||||
const deviceIp = String(row.deviceIp ?? "").trim();
|
||||
const hostname = String(row.hostname ?? deviceHostname).trim();
|
||||
const port = Number(row.port);
|
||||
const displayName = String(row.displayName ?? "").trim();
|
||||
const url = String(row.url ?? "").trim();
|
||||
|
||||
if (!displayName || !hostname || !port || !url) {
|
||||
errors.push(`Zeile übersprungen (Pflichtfelder fehlen): ${JSON.stringify(row)}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Gerät über IP finden, sonst über Hostname, sonst neu anlegen.
|
||||
const allDevices = deviceRepo.listDevices();
|
||||
let device = deviceIp ? allDevices.find((d) => d.ip === deviceIp) : undefined;
|
||||
if (!device && deviceHostname) {
|
||||
device = allDevices.find((d) => d.hostname === deviceHostname);
|
||||
}
|
||||
if (!device) {
|
||||
device = deviceRepo.createDevice({
|
||||
hostname: deviceHostname || hostname,
|
||||
ip: deviceIp || hostname,
|
||||
});
|
||||
}
|
||||
|
||||
// Bereits vorhanden (gleiches Gerät + Port)? -> überspringen, nicht
|
||||
// doppelt anlegen und nichts Bestehendes verändern/löschen.
|
||||
const existing = serviceRepo.listServicesByDevice(device.id).find((s) => s.port === port);
|
||||
if (existing) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const category = row.category ? String(row.category).trim() : undefined;
|
||||
|
||||
serviceRepo.createService({
|
||||
deviceId: device.id,
|
||||
displayName,
|
||||
hostname,
|
||||
url,
|
||||
https: String(row.https ?? "").toLowerCase() === "true",
|
||||
port,
|
||||
category: category || undefined,
|
||||
alias: row.alias
|
||||
? String(row.alias)
|
||||
.split(";")
|
||||
.map((a) => a.trim())
|
||||
.filter(Boolean)
|
||||
: undefined,
|
||||
favorite: String(row.favorite ?? "").toLowerCase() === "true",
|
||||
order:
|
||||
row.order !== undefined && row.order !== "" ? Number(row.order) : undefined,
|
||||
visible:
|
||||
row.visible !== undefined && row.visible !== ""
|
||||
? String(row.visible).toLowerCase() === "true"
|
||||
: undefined,
|
||||
});
|
||||
|
||||
if (category) {
|
||||
categoryRepo.ensureCategory(category);
|
||||
}
|
||||
|
||||
imported++;
|
||||
} catch (err) {
|
||||
errors.push(err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}
|
||||
|
||||
return { imported, skipped, errors };
|
||||
});
|
||||
|
||||
// Muss vor der /:id-Route stehen, damit "reorder" nicht als ID interpretiert wird.
|
||||
app.patch("/api/services/reorder", async (request, reply) => {
|
||||
const parsed = ServiceReorderSchema.safeParse(request.body);
|
||||
|
||||
Reference in New Issue
Block a user