fix(log): fall back to user's Desktop before AppData, keep daily-log naming

If the configured log path (or the default exe-adjacent path) isn't
writable, we now try the current user's Desktop first — that's where
users actually look — and only fall back to AppData if Desktop is
also unavailable. The daily-log filename suffix is preserved on the
fallback file so the format stays consistent.
This commit is contained in:
Administrator 2026-04-19 11:46:50 +02:00
parent fdac28040d
commit 415162e058

44
main.js
View File

@ -113,6 +113,25 @@ function getLogFilePath() {
return _dailyLogPath; return _dailyLogPath;
} }
function buildFallbackLogName(dir) {
// Match the daily-log naming when enabled, so fallback files stay consistent.
const config = configStore.load();
const useDailyLog = config && config.globalSettings && config.globalSettings.sessionLog;
if (!useDailyLog) return path.join(dir, 'fileuploader.log');
const now = new Date();
const pad = (n) => String(n).padStart(2, '0');
const today = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
return path.join(dir, `fileuploader-${today}.log`);
}
function getSafeDesktopDir() {
try {
const desktop = app.getPath('desktop');
if (desktop && fs.existsSync(desktop)) return desktop;
} catch {}
return null;
}
let _uploadLogFallbackWarned = false; let _uploadLogFallbackWarned = false;
function appendUploadLog(hoster, link, fileName) { function appendUploadLog(hoster, link, fileName) {
const now = new Date(); const now = new Date();
@ -129,11 +148,30 @@ function appendUploadLog(hoster, link, fileName) {
tryWrite(getLogFilePath()); tryWrite(getLogFilePath());
return; return;
} catch (err) { } catch (err) {
debugLog(`appendUploadLog primary failed (${err.message}); using fallback`); debugLog(`appendUploadLog primary failed (${err.message}); trying desktop fallback`);
} }
// Fallback 1: current user's Desktop (visible, easy to find).
const desktop = getSafeDesktopDir();
if (desktop) {
try {
const fallbackPath = buildFallbackLogName(desktop);
tryWrite(fallbackPath);
if (!_uploadLogFallbackWarned) {
_uploadLogFallbackWarned = true;
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('upload-log-fallback', { fallbackPath });
}
}
return;
} catch (err) {
debugLog(`appendUploadLog desktop fallback failed (${err.message}); trying userData`);
}
}
// Fallback 2: userData (always writable by the current user).
try { try {
const fallbackPath = path.join(app.getPath('userData'), 'fileuploader-fallback.log'); const fallbackPath = buildFallbackLogName(app.getPath('userData'));
tryWrite(fallbackPath); tryWrite(fallbackPath);
if (!_uploadLogFallbackWarned) { if (!_uploadLogFallbackWarned) {
_uploadLogFallbackWarned = true; _uploadLogFallbackWarned = true;
@ -142,7 +180,7 @@ function appendUploadLog(hoster, link, fileName) {
} }
} }
} catch (err) { } catch (err) {
debugLog(`appendUploadLog fallback also failed: ${err.message}`); debugLog(`appendUploadLog all fallbacks failed: ${err.message}`);
} }
} }