diff --git a/server/db.mjs b/server/db.mjs index 5ced803..25efdb1 100644 --- a/server/db.mjs +++ b/server/db.mjs @@ -116,7 +116,7 @@ db.exec(` CREATE TABLE IF NOT EXISTS user_quota ( user_id TEXT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, - bytes_limit INTEGER NOT NULL DEFAULT 100 * 1024 * 1024 * 1024, + bytes_limit INTEGER NOT NULL DEFAULT (100 * 1024 * 1024 * 1024), prune_at INTEGER, prune_plan TEXT, prune_notified_at INTEGER, diff --git a/server/quota.mjs b/server/quota.mjs index c9b7715..6c18d92 100644 --- a/server/quota.mjs +++ b/server/quota.mjs @@ -40,7 +40,7 @@ const fmtDate = (ms) => minute: '2-digit', }); -const appUrl = () => PUBLIC_URL || 'http://localhost:8787'; +const appUrl = () => PUBLIC_URL; function safePlan(value) { if (!value) return null; @@ -185,6 +185,7 @@ function escapeHtml(value) { async function pruneWarningEmail(user, state, plan, pruneAt) { const when = fmtDate(pruneAt); + const link = appUrl(); const subject = `Luminary: ${fmtBytes(plan.freedBytes)} will be pruned in 24 hours`; const text = [ `Hi ${user.name || user.email},`, @@ -196,8 +197,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) { planText(plan) || '• No individual items were identified.', '', `This should free about ${fmtBytes(plan.freedBytes)} and leave your library around ${fmtBytes(plan.projectedBytes)}.`, - '', - `Review your library now: ${appUrl()}`, + ...(link ? ['', `Review your library now: ${link}`] : []), '', 'Deleting photos or emptying Trash before the scheduled time will cancel or reduce the cleanup.', '— Luminary', @@ -218,7 +218,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) { ${planHtml(plan) || '
No individual items were identified.
'}This should free about ${fmtBytes(plan.freedBytes)} and leave your library around ${fmtBytes(plan.projectedBytes)}.
- + ${link ? `` : ''}Deleting photos or emptying Trash before the scheduled
time will cancel or reduce the cleanup.
— Luminary
Delete photos you no longer need or empty Trash to free space. If the quota is exceeded, an administrator can schedule a 24-hour storage cleanup.
- + ${link ? `` : ''}— Luminary
`; @@ -321,30 +321,21 @@ export function quotasForAdmin() { }); } -export async function sendTestNotification(to) { - if (!mailStatus().configured) { - throw new Error('Add the Gmail address and app password first'); - } - await sendAppMail({ - to, - subject: 'Luminary email notifications are working', - text: 'This is a test message from Luminary. Quota warnings and prune notices are configured correctly.', - html: 'This is a test message from Luminary. Quota warnings and prune notices are configured correctly.
', - }); -} - -async function executePlan(userId, plan) { +async function executePlan(userId, plan, targetBytes) { let removedPhotos = 0; let freedBytes = 0; + let current = quotaUsage(userId); const eventIds = new Set(); - for (const event of plan.events ?? []) eventIds.add(event.id); - for (const event of plan.events ?? []) { + if (current <= targetBytes) break; + eventIds.add(event.id); for (const photoId of event.photos ?? []) { const photo = getPhoto(photoId); if (!photo) continue; - freedBytes += Number(photo.size) || 0; + const bytes = Number(photo.size) || 0; + freedBytes += bytes; + current -= bytes; removePhoto(photoId); clearDenoised(photoId); await removeAsset(photoId); @@ -352,9 +343,12 @@ async function executePlan(userId, plan) { } } for (const entry of plan.photos ?? []) { + if (current <= targetBytes) break; const photo = getPhoto(entry.id); if (!photo) continue; - freedBytes += Number(photo.size) || 0; + const bytes = Number(photo.size) || 0; + freedBytes += bytes; + current -= bytes; removePhoto(entry.id); clearDenoised(entry.id); await removeAsset(entry.id); @@ -377,8 +371,40 @@ async function pruneDueAccounts() { continue; } try { - const before = quotaUsage(row.user_id); - const result = await executePlan(row.user_id, plan); + const stateBefore = quotaState(row.user_id); + const targetBytes = Math.floor(stateBefore.limit * QUOTA_TARGET_RATIO); + if (stateBefore.used <= targetBytes) { + saveQuota(row.user_id, { + pruneAt: null, + prunePlan: null, + pruneNotifiedAt: null, + }); + const link = appUrl(); + await sendAppMail({ + to: user.email, + subject: 'Luminary: scheduled storage cleanup cancelled', + text: [ + `Hi ${user.name || user.email},`, + '', + 'Your scheduled Luminary storage cleanup was cancelled because your library is now below the cleanup target. Nothing was deleted.', + ...(link ? ['', `Open Luminary: ${link}`] : []), + '', + '— Luminary', + ].join('\n'), + html: ` +Hi ${escapeHtml(user.name || user.email)},
+Your scheduled Luminary storage cleanup was cancelled because your library is + now below the cleanup target. Nothing was deleted.
+— Luminary
+