Add dedicated rename support logging
This commit is contained in:
@@ -39,6 +39,7 @@ import { encryptBackup, decryptBackup } from "./backup-crypto";
|
||||
import { getAuditLogPath, initAuditLog, logAuditEvent, shutdownAuditLog } from "./audit-log";
|
||||
import { getDebugSetupCheck } from "./debug-setup";
|
||||
import { buildLinkExportSelection, serializeLinkExportText } from "./link-export";
|
||||
import { getRenameLogPath, initRenameLog, shutdownRenameLog } from "./rename-log";
|
||||
import { buildAccountSummary, diffAccountSummary } from "./support-data";
|
||||
import { buildSupportBundle, getSupportBundleDefaultFileName } from "./support-bundle";
|
||||
import { getTraceConfig, getTraceLogPath, initTraceLog, logTraceEvent, setTraceEnabled, shutdownTraceLog } from "./trace-log";
|
||||
@@ -82,6 +83,7 @@ export class AppController {
|
||||
initPackageLogs(this.storagePaths.baseDir);
|
||||
initItemLogs(this.storagePaths.baseDir);
|
||||
initAuditLog(this.storagePaths.baseDir);
|
||||
initRenameLog(this.storagePaths.baseDir);
|
||||
initTraceLog(this.storagePaths.baseDir);
|
||||
this.settings = loadSettings(this.storagePaths);
|
||||
const session = loadSession(this.storagePaths);
|
||||
@@ -186,6 +188,10 @@ export class AppController {
|
||||
return getAuditLogPath();
|
||||
}
|
||||
|
||||
public getRenameLogPath(): string | null {
|
||||
return getRenameLogPath();
|
||||
}
|
||||
|
||||
public getTraceLogPath(): string | null {
|
||||
return getTraceLogPath();
|
||||
}
|
||||
@@ -643,6 +649,7 @@ export class AppController {
|
||||
shutdownSessionLog();
|
||||
shutdownPackageLogs();
|
||||
shutdownItemLogs();
|
||||
shutdownRenameLog();
|
||||
this.audit("INFO", "App beendet");
|
||||
shutdownTraceLog();
|
||||
shutdownAuditLog();
|
||||
|
||||
@@ -9,6 +9,7 @@ import { logger, getLogFilePath } from "./logger";
|
||||
import { getItemLogPath as getPersistedItemLogPath } from "./item-log";
|
||||
import { getSessionLogPath } from "./session-log";
|
||||
import { getPackageLogPath as getPersistedPackageLogPath } from "./package-log";
|
||||
import { getRenameLogPath } from "./rename-log";
|
||||
import { createStoragePaths, loadHistory, loadSettings } from "./storage";
|
||||
import { buildAccountSummary, buildRedactedSettingsPayload, buildStatsPayload, summarizeHistoryEntry } from "./support-data";
|
||||
import { buildSupportBundle, getSupportBundleDefaultFileName } from "./support-bundle";
|
||||
@@ -38,6 +39,7 @@ const DEBUG_ENDPOINTS: DebugEndpointDescriptor[] = [
|
||||
{ method: "GET", path: "/log", queryExample: "lines=100&grep=keyword", description: "Legacy alias for the main application log tail." },
|
||||
{ method: "GET", path: "/logs/main", queryExample: "lines=100&grep=keyword", description: "Reads the main application log tail." },
|
||||
{ method: "GET", path: "/logs/audit", queryExample: "lines=100&grep=keyword", description: "Reads the audit log for support-relevant UI and admin actions." },
|
||||
{ method: "GET", path: "/logs/rename", queryExample: "lines=100&grep=keyword", description: "Reads the dedicated rename and MKV move log." },
|
||||
{ method: "GET", path: "/logs/trace", queryExample: "lines=100&grep=keyword", description: "Reads the optional support trace log." },
|
||||
{ method: "GET", path: "/logs/session", queryExample: "lines=100&grep=keyword", description: "Reads the session log tail." },
|
||||
{ method: "GET", path: "/logs/package", queryExample: "package=Release&lines=100&grep=keyword", description: "Reads the package log for a specific package name or id." },
|
||||
@@ -240,7 +242,7 @@ function buildAiManifest(baseDir: string): Record<string, unknown> {
|
||||
"If remote access is needed, ask the user only for the server IP or DNS name.",
|
||||
"Call /meta first to confirm the server is reachable and to re-read the endpoint list.",
|
||||
"Use /self-check or /debug/setup to quickly verify whether token, host, manifest, trace, disk space, and log sizes are in a good support state.",
|
||||
"Use /diagnostics for an overview, then drill into /logs/item, /logs/package, /status, /packages, /items, /settings, /accounts, /stats, /history, or /logs/trace.",
|
||||
"Use /diagnostics for an overview, then drill into /logs/item, /logs/package, /logs/rename, /status, /packages, /items, /settings, /accounts, /stats, /history, or /logs/trace.",
|
||||
"If a full handoff is needed, download /support/bundle as a ZIP."
|
||||
],
|
||||
auth: {
|
||||
@@ -257,6 +259,7 @@ function buildAiManifest(baseDir: string): Record<string, unknown> {
|
||||
tokenFile: path.join(baseDir, "debug_token.txt"),
|
||||
mainLogFile: getLogFilePath(),
|
||||
auditLogFile: getAuditLogPath(),
|
||||
renameLogFile: getRenameLogPath(),
|
||||
traceLogFile: getTraceLogPath(),
|
||||
traceConfigFile: getTraceConfigPath(),
|
||||
sessionLogFile: getSessionLogPath(),
|
||||
@@ -483,6 +486,7 @@ function handleRequest(req: http.IncomingMessage, res: http.ServerResponse): voi
|
||||
logPaths: {
|
||||
main: getLogFilePath(),
|
||||
audit: getAuditLogPath(),
|
||||
rename: getRenameLogPath(),
|
||||
session: getSessionLogPath(),
|
||||
trace: getTraceLogPath()
|
||||
},
|
||||
@@ -522,6 +526,19 @@ function handleRequest(req: http.IncomingMessage, res: http.ServerResponse): voi
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname === "/logs/rename") {
|
||||
const count = normalizeLinesParam(url.searchParams.get("lines"), 100);
|
||||
const grep = url.searchParams.get("grep") || "";
|
||||
const logPath = getRenameLogPath();
|
||||
const lines = filterLines(readLogTailFromFile(logPath, count), grep);
|
||||
jsonResponse(res, 200, {
|
||||
path: logPath,
|
||||
lines,
|
||||
count: lines.length
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname === "/logs/trace") {
|
||||
const count = normalizeLinesParam(url.searchParams.get("lines"), 100);
|
||||
const grep = url.searchParams.get("grep") || "";
|
||||
@@ -847,6 +864,10 @@ function handleRequest(req: http.IncomingMessage, res: http.ServerResponse): voi
|
||||
path: getAuditLogPath(),
|
||||
lines: filterLines(readLogTailFromFile(getAuditLogPath(), lineCount), grep)
|
||||
},
|
||||
rename: {
|
||||
path: getRenameLogPath(),
|
||||
lines: filterLines(readLogTailFromFile(getRenameLogPath(), lineCount), grep)
|
||||
},
|
||||
trace: {
|
||||
path: getTraceLogPath(),
|
||||
config: getTraceConfig(),
|
||||
|
||||
@@ -279,6 +279,8 @@ function getSupportBundleEstimate(
|
||||
+ Number(logSummary.mainBackup.exists)
|
||||
+ Number(logSummary.audit.exists)
|
||||
+ Number(logSummary.auditBackup.exists)
|
||||
+ Number(logSummary.rename.exists)
|
||||
+ Number(logSummary.renameBackup.exists)
|
||||
+ Number(logSummary.session.exists)
|
||||
+ Number(logSummary.trace.exists)
|
||||
+ Number(logSummary.traceBackup.exists)
|
||||
@@ -317,6 +319,8 @@ export function getDebugSetupCheck(baseDir: string): DebugSetupCheckResult {
|
||||
mainBackup: getFileSizeInfo(path.join(baseDir, "rd_downloader.log.old")),
|
||||
audit: getFileSizeInfo(path.join(baseDir, "audit.log")),
|
||||
auditBackup: getFileSizeInfo(path.join(baseDir, "audit.log.old")),
|
||||
rename: getFileSizeInfo(path.join(baseDir, "rename.log")),
|
||||
renameBackup: getFileSizeInfo(path.join(baseDir, "rename.log.old")),
|
||||
session: getFileSizeInfo(sessionLogPath),
|
||||
trace: getFileSizeInfo(traceLogPath),
|
||||
traceBackup: getFileSizeInfo(path.join(baseDir, "trace.log.old")),
|
||||
@@ -330,6 +334,8 @@ export function getDebugSetupCheck(baseDir: string): DebugSetupCheckResult {
|
||||
logSummary.mainBackup.bytes,
|
||||
logSummary.audit.bytes,
|
||||
logSummary.auditBackup.bytes,
|
||||
logSummary.rename.bytes,
|
||||
logSummary.renameBackup.bytes,
|
||||
logSummary.session.bytes,
|
||||
logSummary.trace.bytes,
|
||||
logSummary.traceBackup.bytes,
|
||||
|
||||
@@ -55,6 +55,7 @@ import { validateFileAgainstManifest } from "./integrity";
|
||||
import { logger } from "./logger";
|
||||
import { ensureItemLog, getItemLogPath as getPersistedItemLogPath, logItemEvent as writeItemLogEvent } from "./item-log";
|
||||
import { ensurePackageLog, getPackageLogPath as getPersistedPackageLogPath, logPackageEvent as writePackageLogEvent } from "./package-log";
|
||||
import { logRenameEvent as writeRenameLogEvent } from "./rename-log";
|
||||
import { StoragePaths, saveSession, saveSessionAsync, saveSettings, saveSettingsAsync } from "./storage";
|
||||
import { compactErrorText, ensureDirPath, filenameFromUrl, formatEta, humanSize, looksLikeOpaqueFilename, nowMs, sanitizeFilename, sleep } from "./utils";
|
||||
|
||||
@@ -185,6 +186,28 @@ function inspectPackageItemDiskState(pkg: PackageEntry, item: DownloadItem): Pac
|
||||
}
|
||||
}
|
||||
|
||||
function stripArchiveSuffixForMatching(fileName: string): string {
|
||||
const trimmed = path.basename(String(fileName || "").trim());
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
let next = trimmed.replace(/\.(?:part\d+\.rar|zip\.\d+|7z\.\d+|rar|r\d{2,3}|zip|7z|\d{3})$/i, "");
|
||||
next = next.replace(/\.part\d+$/i, "").replace(/\.vol\d+[+\d]*$/i, "");
|
||||
return next.toLowerCase();
|
||||
}
|
||||
|
||||
function isPreferredArchiveEntryPointName(fileName: string): boolean {
|
||||
const normalized = path.basename(String(fileName || "").trim()).toLowerCase();
|
||||
if (!normalized) {
|
||||
return false;
|
||||
}
|
||||
return /\.part0*1\.rar$/.test(normalized)
|
||||
|| (/\.rar$/.test(normalized) && !/\.part\d+\.rar$/.test(normalized) && !/\.r\d{2,3}$/.test(normalized))
|
||||
|| /\.zip\.001$/.test(normalized)
|
||||
|| /\.7z\.001$/.test(normalized)
|
||||
|| (/\.001$/.test(normalized) && !/\.(zip|7z)\.001$/.test(normalized));
|
||||
}
|
||||
|
||||
function getDownloadStallTimeoutMs(): number {
|
||||
const fromEnv = Number(process.env.RD_STALL_TIMEOUT_MS ?? NaN);
|
||||
if (Number.isFinite(fromEnv) && fromEnv >= 2000 && fromEnv <= 600000) {
|
||||
@@ -1438,6 +1461,141 @@ export class DownloadManager extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
private logItemOnly(
|
||||
item: DownloadItem,
|
||||
level: "INFO" | "WARN" | "ERROR",
|
||||
message: string,
|
||||
fields?: Record<string, unknown>
|
||||
): void {
|
||||
const pkg = this.session.packages[item.packageId];
|
||||
this.ensureItemLogForItem(item);
|
||||
writeItemLogEvent(item.id, level, message, {
|
||||
packageId: item.packageId,
|
||||
packageName: pkg?.name || "",
|
||||
itemId: item.id,
|
||||
fileName: item.fileName,
|
||||
status: item.status,
|
||||
targetPath: item.targetPath,
|
||||
...fields
|
||||
});
|
||||
}
|
||||
|
||||
private collectRenameMatchTokensForItem(pkg: PackageEntry, item: DownloadItem): string[] {
|
||||
const tokens = new Set<string>();
|
||||
const maybeAdd = (value: string | null | undefined): void => {
|
||||
const normalized = String(value || "").trim().toLowerCase();
|
||||
if (!normalized || normalized.length < 4) {
|
||||
return;
|
||||
}
|
||||
tokens.add(normalized);
|
||||
};
|
||||
|
||||
maybeAdd(stripArchiveSuffixForMatching(item.fileName || ""));
|
||||
maybeAdd(stripArchiveSuffixForMatching(item.targetPath ? path.basename(item.targetPath) : ""));
|
||||
const diskPath = resolvePackageItemDiskPath(pkg, item);
|
||||
if (diskPath) {
|
||||
maybeAdd(stripArchiveSuffixForMatching(path.basename(diskPath)));
|
||||
}
|
||||
const episodeToken = extractEpisodeToken(item.fileName || path.basename(item.targetPath || ""));
|
||||
if (episodeToken) {
|
||||
maybeAdd(episodeToken);
|
||||
}
|
||||
return [...tokens].sort((a, b) => b.length - a.length);
|
||||
}
|
||||
|
||||
private inferItemForMediaLog(
|
||||
pkg: PackageEntry,
|
||||
...candidates: Array<string | null | undefined>
|
||||
): { item: DownloadItem | null; matchedBy: string | null } {
|
||||
const items = pkg.itemIds
|
||||
.map((itemId) => this.session.items[itemId])
|
||||
.filter(Boolean) as DownloadItem[];
|
||||
if (items.length === 0) {
|
||||
return { item: null, matchedBy: null };
|
||||
}
|
||||
if (items.length === 1) {
|
||||
return { item: items[0] || null, matchedBy: items[0] ? "single_item_package" : null };
|
||||
}
|
||||
|
||||
const haystack = candidates
|
||||
.map((value) => String(value || "").trim().toLowerCase())
|
||||
.filter(Boolean)
|
||||
.join(" || ");
|
||||
if (!haystack) {
|
||||
return { item: null, matchedBy: null };
|
||||
}
|
||||
|
||||
let bestItem: DownloadItem | null = null;
|
||||
let bestScore = 0;
|
||||
let bestMatchedBy: string | null = null;
|
||||
let bestPreferredEntry = false;
|
||||
let ambiguous = false;
|
||||
|
||||
for (const item of items) {
|
||||
const fileName = item.fileName || path.basename(item.targetPath || "");
|
||||
const preferredEntry = isPreferredArchiveEntryPointName(fileName);
|
||||
let score = preferredEntry ? 5 : 0;
|
||||
let matchedBy: string | null = preferredEntry ? "entry_point" : null;
|
||||
|
||||
const episodeToken = extractEpisodeToken(fileName);
|
||||
if (episodeToken && haystack.includes(episodeToken.toLowerCase())) {
|
||||
score = 110 + (preferredEntry ? 5 : 0);
|
||||
matchedBy = "episode_token";
|
||||
} else {
|
||||
for (const token of this.collectRenameMatchTokensForItem(pkg, item)) {
|
||||
if (haystack.includes(token)) {
|
||||
score = Math.max(score, Math.min(100, 40 + token.length) + (preferredEntry ? 5 : 0));
|
||||
matchedBy = token === episodeToken?.toLowerCase() ? "episode_token" : `token:${token}`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (score > bestScore || (score === bestScore && score > 0 && preferredEntry && !bestPreferredEntry)) {
|
||||
bestItem = item;
|
||||
bestScore = score;
|
||||
bestMatchedBy = matchedBy;
|
||||
bestPreferredEntry = preferredEntry;
|
||||
ambiguous = false;
|
||||
continue;
|
||||
}
|
||||
if (score > 0 && score === bestScore && preferredEntry === bestPreferredEntry) {
|
||||
ambiguous = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (ambiguous || !bestItem || bestScore <= 0) {
|
||||
return { item: null, matchedBy: null };
|
||||
}
|
||||
return { item: bestItem, matchedBy: bestMatchedBy };
|
||||
}
|
||||
|
||||
private logRenameProcess(
|
||||
pkg: PackageEntry,
|
||||
level: "INFO" | "WARN" | "ERROR",
|
||||
stage: "auto-rename" | "mkv-move",
|
||||
message: string,
|
||||
fields?: Record<string, unknown>,
|
||||
item?: DownloadItem | null,
|
||||
matchedBy?: string | null
|
||||
): void {
|
||||
writeRenameLogEvent(level, message, {
|
||||
stage,
|
||||
packageId: pkg.id,
|
||||
packageName: pkg.name,
|
||||
...(item ? { itemId: item.id, fileName: item.fileName } : {}),
|
||||
...(matchedBy ? { matchedBy } : {}),
|
||||
...fields
|
||||
});
|
||||
if (item) {
|
||||
this.logItemOnly(item, level, message, {
|
||||
stage,
|
||||
...(matchedBy ? { matchedBy } : {}),
|
||||
...fields
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public setSettings(next: AppSettings): void {
|
||||
const previous = this.settings;
|
||||
next.totalDownloadedAllTime = Math.max(next.totalDownloadedAllTime || 0, this.settings.totalDownloadedAllTime || 0);
|
||||
@@ -2793,6 +2951,10 @@ export class DownloadManager extends EventEmitter {
|
||||
extractDir,
|
||||
videoFiles: videoFiles.length
|
||||
});
|
||||
this.logRenameProcess(pkg, "INFO", "auto-rename", "Auto-Rename Scan gestartet", {
|
||||
extractDir,
|
||||
videoFiles: videoFiles.length
|
||||
});
|
||||
}
|
||||
let renamed = 0;
|
||||
|
||||
@@ -2840,6 +3002,12 @@ export class DownloadManager extends EventEmitter {
|
||||
const targetBaseName = buildAutoRenameBaseNameFromFoldersWithOptions(folderCandidates, sourceBaseName, {
|
||||
forceEpisodeForSeasonFolder: true
|
||||
});
|
||||
const resolveRenameItem = (...extra: Array<string | null | undefined>): { item: DownloadItem | null; matchedBy: string | null } => {
|
||||
if (!pkg) {
|
||||
return { item: null, matchedBy: null };
|
||||
}
|
||||
return this.inferItemForMediaLog(pkg, sourcePath, sourceName, folderCandidates.join(" "), targetBaseName || "", ...extra);
|
||||
};
|
||||
if (!targetBaseName) {
|
||||
if (pkg) {
|
||||
this.logPackageForPackage(pkg, "WARN", "Auto-Rename übersprungen: kein Zielname", {
|
||||
@@ -2847,6 +3015,13 @@ export class DownloadManager extends EventEmitter {
|
||||
sourceBaseName,
|
||||
folders: folderCandidates.join(", ")
|
||||
});
|
||||
const resolved = resolveRenameItem();
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename übersprungen: kein Zielname", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
sourceBaseName,
|
||||
folders: folderCandidates.join(", ")
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
logger.info(`Auto-Rename: kein Zielname für ${sourceName} (folders=${folderCandidates.join(", ")})`);
|
||||
continue;
|
||||
@@ -2859,6 +3034,16 @@ export class DownloadManager extends EventEmitter {
|
||||
targetPath = this.buildSafeAutoRenameTargetPath(sourcePath, fallbackBaseName, sourceExt);
|
||||
if (targetPath) {
|
||||
logger.warn(`Auto-Rename Fallback wegen Pfadlänge: ${sourceName} -> ${path.basename(targetPath)}`);
|
||||
if (pkg) {
|
||||
const resolved = resolveRenameItem(targetPath, fallbackBaseName);
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename Fallback wegen Pfadlänge gewählt", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath,
|
||||
targetBaseName,
|
||||
fallbackBaseName
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!targetPath) {
|
||||
@@ -2867,6 +3052,16 @@ export class DownloadManager extends EventEmitter {
|
||||
targetPath = this.buildSafeAutoRenameTargetPath(sourcePath, veryShortFallback, sourceExt);
|
||||
if (targetPath) {
|
||||
logger.warn(`Auto-Rename Kurz-Fallback wegen Pfadlänge: ${sourceName} -> ${path.basename(targetPath)}`);
|
||||
if (pkg) {
|
||||
const resolved = resolveRenameItem(targetPath, veryShortFallback);
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename Kurz-Fallback wegen Pfadlänge gewählt", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath,
|
||||
targetBaseName,
|
||||
fallbackBaseName: veryShortFallback
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2878,11 +3073,27 @@ export class DownloadManager extends EventEmitter {
|
||||
sourceBaseName,
|
||||
targetBaseName
|
||||
});
|
||||
const resolved = resolveRenameItem();
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename übersprungen: Zielpfad ungültig", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
sourceBaseName,
|
||||
targetBaseName
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
logger.warn(`Auto-Rename übersprungen (Zielpfad zu lang/ungültig): ${sourcePath}`);
|
||||
continue;
|
||||
}
|
||||
if (pathKey(targetPath) === pathKey(sourcePath)) {
|
||||
if (pkg) {
|
||||
const resolved = resolveRenameItem(targetPath);
|
||||
this.logRenameProcess(pkg, "INFO", "auto-rename", "Auto-Rename übersprungen: Name bereits passend", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath,
|
||||
targetBaseName
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (await this.existsAsync(targetPath)) {
|
||||
@@ -2891,6 +3102,13 @@ export class DownloadManager extends EventEmitter {
|
||||
sourceName,
|
||||
targetPath
|
||||
});
|
||||
const resolved = resolveRenameItem(targetPath);
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename übersprungen: Ziel existiert", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath,
|
||||
targetBaseName
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
logger.warn(`Auto-Rename übersprungen (Ziel existiert): ${targetPath}`);
|
||||
continue;
|
||||
@@ -2904,6 +3122,14 @@ export class DownloadManager extends EventEmitter {
|
||||
targetPath,
|
||||
sourceName
|
||||
});
|
||||
const resolved = resolveRenameItem(targetPath);
|
||||
this.logRenameProcess(pkg, "INFO", "auto-rename", "Auto-Rename durchgeführt", {
|
||||
sourcePath,
|
||||
targetPath,
|
||||
sourceName,
|
||||
targetBaseName,
|
||||
folders: folderCandidates.join(", ")
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
logger.info(`Auto-Rename: ${sourceName} -> ${path.basename(targetPath)}`);
|
||||
renamed += 1;
|
||||
@@ -2926,6 +3152,16 @@ export class DownloadManager extends EventEmitter {
|
||||
await this.renamePathWithExdevFallback(sourcePath, fallbackPath);
|
||||
logger.warn(`Auto-Rename Fallback wegen Pfadlänge: ${sourceName} -> ${path.basename(fallbackPath)}`);
|
||||
renamed += 1;
|
||||
if (pkg) {
|
||||
const resolved = resolveRenameItem(fallbackPath, fallbackBaseName);
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename Fallback durchgeführt", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath: fallbackPath,
|
||||
targetBaseName,
|
||||
fallbackBaseName
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
fallbackRenamed = true;
|
||||
break;
|
||||
} catch {
|
||||
@@ -2942,6 +3178,15 @@ export class DownloadManager extends EventEmitter {
|
||||
sourceName,
|
||||
error: compactErrorText(error)
|
||||
});
|
||||
const resolved = resolveRenameItem(targetPath);
|
||||
this.logRenameProcess(pkg, "WARN", "auto-rename", "Auto-Rename fehlgeschlagen", {
|
||||
sourcePath,
|
||||
sourceName,
|
||||
targetPath,
|
||||
targetBaseName,
|
||||
folders: folderCandidates.join(", "),
|
||||
error: compactErrorText(error)
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2952,6 +3197,10 @@ export class DownloadManager extends EventEmitter {
|
||||
this.logPackageForPackage(pkg, "INFO", "Auto-Rename abgeschlossen", {
|
||||
renamed
|
||||
});
|
||||
this.logRenameProcess(pkg, "INFO", "auto-rename", "Auto-Rename abgeschlossen", {
|
||||
extractDir,
|
||||
renamed
|
||||
});
|
||||
}
|
||||
}
|
||||
return renamed;
|
||||
@@ -3152,6 +3401,12 @@ export class DownloadManager extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
this.logRenameProcess(pkg, "INFO", "mkv-move", "MKV-Sammelordner Scan gestartet", {
|
||||
sourceDir,
|
||||
targetDir,
|
||||
mkvFiles: mkvFiles.length
|
||||
});
|
||||
|
||||
const reservedTargets = new Set<string>();
|
||||
let moved = 0;
|
||||
let skipped = 0;
|
||||
@@ -3174,6 +3429,12 @@ export class DownloadManager extends EventEmitter {
|
||||
}
|
||||
if (sourceSize === 0) {
|
||||
logger.warn(`MKV-Sammelordner: überspringe 0-Byte-Datei ${path.basename(sourcePath)}`);
|
||||
const resolved = this.inferItemForMediaLog(pkg, sourcePath, path.basename(sourcePath), targetDir);
|
||||
this.logRenameProcess(pkg, "WARN", "mkv-move", "MKV übersprungen: 0-Byte-Datei", {
|
||||
sourcePath,
|
||||
targetDir,
|
||||
sourceSize
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
@@ -3184,6 +3445,12 @@ export class DownloadManager extends EventEmitter {
|
||||
const existingStat = await fs.promises.stat(idealTargetPath);
|
||||
if (existingStat.size === sourceSize) {
|
||||
logger.info(`MKV-Sammelordner: Duplikat übersprungen (gleiche Größe ${humanSize(sourceSize)}): ${path.basename(sourcePath)}`);
|
||||
const resolved = this.inferItemForMediaLog(pkg, sourcePath, path.basename(sourcePath), idealTargetPath);
|
||||
this.logRenameProcess(pkg, "INFO", "mkv-move", "MKV-Duplikat übersprungen", {
|
||||
sourcePath,
|
||||
targetPath: idealTargetPath,
|
||||
sourceSize
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
// Remove the duplicate source file to avoid future re-processing
|
||||
try { await fs.promises.unlink(sourcePath); } catch { /* ignore */ }
|
||||
skipped += 1;
|
||||
@@ -3207,6 +3474,12 @@ export class DownloadManager extends EventEmitter {
|
||||
targetPath,
|
||||
sourceSize
|
||||
});
|
||||
const resolved = this.inferItemForMediaLog(pkg, sourcePath, path.basename(sourcePath), targetPath);
|
||||
this.logRenameProcess(pkg, "INFO", "mkv-move", "MKV verschoben", {
|
||||
sourcePath,
|
||||
targetPath,
|
||||
sourceSize
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
} catch (error) {
|
||||
failed += 1;
|
||||
logger.warn(`MKV verschieben fehlgeschlagen: ${sourcePath} -> ${targetPath} (${compactErrorText(error)})`);
|
||||
@@ -3215,6 +3488,13 @@ export class DownloadManager extends EventEmitter {
|
||||
targetPath,
|
||||
error: compactErrorText(error)
|
||||
});
|
||||
const resolved = this.inferItemForMediaLog(pkg, sourcePath, path.basename(sourcePath), targetPath);
|
||||
this.logRenameProcess(pkg, "WARN", "mkv-move", "MKV verschieben fehlgeschlagen", {
|
||||
sourcePath,
|
||||
targetPath,
|
||||
sourceSize,
|
||||
error: compactErrorText(error)
|
||||
}, resolved.item, resolved.matchedBy);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3230,6 +3510,13 @@ export class DownloadManager extends EventEmitter {
|
||||
}
|
||||
|
||||
logger.info(`MKV-Sammelordner: pkg=${pkg.name}, packageId=${packageId}, moved=${moved}, skipped=${skipped}, failed=${failed}, target=${targetDir}`);
|
||||
this.logRenameProcess(pkg, "INFO", "mkv-move", "MKV-Sammelordner abgeschlossen", {
|
||||
sourceDir,
|
||||
targetDir,
|
||||
moved,
|
||||
skipped,
|
||||
failed
|
||||
});
|
||||
}
|
||||
|
||||
public cancelPackage(packageId: string): void {
|
||||
|
||||
@@ -550,6 +550,13 @@ function registerIpcHandlers(): void {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle(IPC_CHANNELS.OPEN_RENAME_LOG, async () => {
|
||||
const logPath = controller.getRenameLogPath();
|
||||
if (logPath) {
|
||||
await shell.openPath(logPath);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle(IPC_CHANNELS.OPEN_SESSION_LOG, async () => {
|
||||
const logPath = controller.getSessionLogPath();
|
||||
if (logPath) {
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
type RenameLogLevel = "INFO" | "WARN" | "ERROR";
|
||||
|
||||
const RENAME_LOG_MAX_FILE_BYTES = Number(process.env.RD_RENAME_LOG_MAX_BYTES || 10 * 1024 * 1024);
|
||||
const RENAME_LOG_RETENTION_DAYS = Number(process.env.RD_RENAME_LOG_RETENTION_DAYS || 30);
|
||||
|
||||
let renameLogPath: string | null = null;
|
||||
|
||||
function sanitizeFieldValue(value: unknown): string {
|
||||
if (value === undefined || value === null) {
|
||||
return "";
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value.replace(/\r?\n/g, "\\n");
|
||||
}
|
||||
if (typeof value === "number" || typeof value === "boolean") {
|
||||
return String(value);
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(value).replace(/\r?\n/g, "\\n");
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
function formatFields(fields?: Record<string, unknown>): string {
|
||||
if (!fields) {
|
||||
return "";
|
||||
}
|
||||
const parts = Object.entries(fields)
|
||||
.filter(([, value]) => value !== undefined && value !== null && sanitizeFieldValue(value) !== "")
|
||||
.map(([key, value]) => `${key}=${sanitizeFieldValue(value)}`);
|
||||
return parts.length > 0 ? ` | ${parts.join(" | ")}` : "";
|
||||
}
|
||||
|
||||
function rotateIfNeeded(filePath: string): void {
|
||||
try {
|
||||
const stat = fs.statSync(filePath);
|
||||
if (stat.size < RENAME_LOG_MAX_FILE_BYTES) {
|
||||
return;
|
||||
}
|
||||
const backup = `${filePath}.old`;
|
||||
try {
|
||||
fs.rmSync(backup, { force: true });
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
fs.renameSync(filePath, backup);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
function cleanupOldBackup(filePath: string): void {
|
||||
const backup = `${filePath}.old`;
|
||||
try {
|
||||
const stat = fs.statSync(backup);
|
||||
const cutoff = Date.now() - RENAME_LOG_RETENTION_DAYS * 24 * 60 * 60 * 1000;
|
||||
if (stat.mtimeMs < cutoff) {
|
||||
fs.rmSync(backup, { force: true });
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
export function initRenameLog(baseDir: string): void {
|
||||
renameLogPath = path.join(baseDir, "rename.log");
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(renameLogPath), { recursive: true });
|
||||
cleanupOldBackup(renameLogPath);
|
||||
if (!fs.existsSync(renameLogPath)) {
|
||||
fs.writeFileSync(renameLogPath, "", "utf8");
|
||||
}
|
||||
rotateIfNeeded(renameLogPath);
|
||||
if (!fs.existsSync(renameLogPath)) {
|
||||
fs.writeFileSync(renameLogPath, "", "utf8");
|
||||
}
|
||||
fs.appendFileSync(renameLogPath, `=== Rename-Log Start: ${new Date().toISOString()} ===\n`, "utf8");
|
||||
} catch {
|
||||
renameLogPath = null;
|
||||
}
|
||||
}
|
||||
|
||||
export function logRenameEvent(level: RenameLogLevel, message: string, fields?: Record<string, unknown>): void {
|
||||
if (!renameLogPath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
rotateIfNeeded(renameLogPath);
|
||||
if (!fs.existsSync(renameLogPath)) {
|
||||
fs.writeFileSync(renameLogPath, "", "utf8");
|
||||
}
|
||||
fs.appendFileSync(
|
||||
renameLogPath,
|
||||
`${new Date().toISOString()} [${level}] ${message}${formatFields(fields)}\n`,
|
||||
"utf8"
|
||||
);
|
||||
} catch {
|
||||
// ignore write errors
|
||||
}
|
||||
}
|
||||
|
||||
export function getRenameLogPath(): string | null {
|
||||
if (!renameLogPath) {
|
||||
return null;
|
||||
}
|
||||
return fs.existsSync(renameLogPath) ? renameLogPath : null;
|
||||
}
|
||||
|
||||
export function shutdownRenameLog(): void {
|
||||
if (!renameLogPath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
fs.appendFileSync(renameLogPath, `=== Rename-Log Ende: ${new Date().toISOString()} ===\n`, "utf8");
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
renameLogPath = null;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { getAuditLogPath } from "./audit-log";
|
||||
import { getDebugSetupCheck } from "./debug-setup";
|
||||
import { getLogFilePath } from "./logger";
|
||||
import { getPackageLogPath } from "./package-log";
|
||||
import { getRenameLogPath } from "./rename-log";
|
||||
import { getSessionLogPath } from "./session-log";
|
||||
import { createStoragePaths, loadHistory, loadSettings } from "./storage";
|
||||
import { buildAccountSummary, buildRedactedSettingsPayload, buildStatsPayload, summarizeHistoryEntry } from "./support-data";
|
||||
@@ -120,6 +121,8 @@ export function buildSupportBundle(manager: DownloadManager, baseDir: string): B
|
||||
addFileIfExists(zip, `${getLogFilePath()}.old`, "logs/rd_downloader.log.old");
|
||||
addFileIfExists(zip, getAuditLogPath(), "logs/audit.log");
|
||||
addFileIfExists(zip, getAuditLogPath() ? `${getAuditLogPath()}.old` : null, "logs/audit.log.old");
|
||||
addFileIfExists(zip, getRenameLogPath(), "logs/rename.log");
|
||||
addFileIfExists(zip, getRenameLogPath() ? `${getRenameLogPath()}.old` : null, "logs/rename.log.old");
|
||||
addFileIfExists(zip, getSessionLogPath(), "logs/session.log");
|
||||
addFileIfExists(zip, getTraceLogPath(), "logs/trace.log");
|
||||
addFileIfExists(zip, getTraceLogPath() ? `${getTraceLogPath()}.old` : null, "logs/trace.log.old");
|
||||
|
||||
Reference in New Issue
Block a user