Stop scheduled prunes at the quota target
Pruning previously deleted every photo in the plan; it now halts once usage is back under the cleanup target, so a plan computed earlier cannot over-delete. If the library is already below target the cleanup is cancelled and the owner is notified instead. Notification emails also skip the "open Luminary" link when no public URL is configured, and the admin panel keys the prune action off the plan's freed bytes rather than the warning flag. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
+54
-28
@@ -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) || '<p>No individual items were identified.</p>'}
|
||||
<p>This should free about <strong>${fmtBytes(plan.freedBytes)}</strong> and leave your
|
||||
library around ${fmtBytes(plan.projectedBytes)}.</p>
|
||||
<p><a href="${escapeHtml(appUrl())}" style="display:inline-block;padding:9px 16px;background:#3d7eff;color:#fff;text-decoration:none;border-radius:6px">Review your library</a></p>
|
||||
${link ? `<p><a href="${escapeHtml(link)}" style="display:inline-block;padding:9px 16px;background:#3d7eff;color:#fff;text-decoration:none;border-radius:6px">Review your library</a></p>` : ''}
|
||||
<p style="color:#777;font-size:12px">Deleting photos or emptying Trash before the scheduled
|
||||
time will cancel or reduce the cleanup.<br>— Luminary</p>
|
||||
</div>
|
||||
@@ -227,6 +227,7 @@ async function pruneWarningEmail(user, state, plan, pruneAt) {
|
||||
}
|
||||
|
||||
async function quotaWarningEmail(user, state) {
|
||||
const link = appUrl();
|
||||
const subject = `Luminary: your storage is ${state.percent}% full`;
|
||||
const text = [
|
||||
`Hi ${user.name || user.email},`,
|
||||
@@ -234,8 +235,7 @@ async function quotaWarningEmail(user, state) {
|
||||
`You are nearing your Luminary storage quota: ${fmtBytes(state.used)} used of ${fmtBytes(state.limit)} (${state.percent}%).`,
|
||||
'',
|
||||
'Free some space by deleting photos you no longer need or emptying Trash. If the quota is exceeded, an administrator can schedule a 24-hour storage cleanup.',
|
||||
'',
|
||||
`Open Luminary: ${appUrl()}`,
|
||||
...(link ? ['', `Open Luminary: ${link}`] : []),
|
||||
'',
|
||||
'— Luminary',
|
||||
].join('\n');
|
||||
@@ -247,7 +247,7 @@ async function quotaWarningEmail(user, state) {
|
||||
<strong>${fmtBytes(state.limit)}</strong> Luminary quota (${state.percent}%).</p>
|
||||
<p>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.</p>
|
||||
<p><a href="${escapeHtml(appUrl())}" style="display:inline-block;padding:9px 16px;background:#3d7eff;color:#fff;text-decoration:none;border-radius:6px">Open Luminary</a></p>
|
||||
${link ? `<p><a href="${escapeHtml(link)}" style="display:inline-block;padding:9px 16px;background:#3d7eff;color:#fff;text-decoration:none;border-radius:6px">Open Luminary</a></p>` : ''}
|
||||
<p style="color:#777;font-size:12px">— Luminary</p>
|
||||
</div>
|
||||
`;
|
||||
@@ -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: '<p>This is a test message from Luminary. Quota warnings and prune notices are configured correctly.</p>',
|
||||
});
|
||||
}
|
||||
|
||||
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: `
|
||||
<div style="font-family:Arial,Helvetica,sans-serif;line-height:1.55;color:#222;max-width:640px">
|
||||
<h2 style="margin:0 0 12px">Scheduled cleanup cancelled</h2>
|
||||
<p>Hi ${escapeHtml(user.name || user.email)},</p>
|
||||
<p>Your scheduled Luminary storage cleanup was cancelled because your library is
|
||||
now below the cleanup target. Nothing was deleted.</p>
|
||||
<p style="color:#777;font-size:12px">— Luminary</p>
|
||||
</div>
|
||||
`,
|
||||
}).catch((err) => console.error(`[luminary] cancel email to ${user.email} failed: ${err.message}`));
|
||||
continue;
|
||||
}
|
||||
const before = stateBefore.used;
|
||||
const result = await executePlan(row.user_id, plan, targetBytes);
|
||||
const after = quotaUsage(row.user_id);
|
||||
saveQuota(row.user_id, {
|
||||
pruneAt: null,
|
||||
@@ -386,6 +412,7 @@ async function pruneDueAccounts() {
|
||||
pruneNotifiedAt: null,
|
||||
});
|
||||
const state = quotaState(row.user_id);
|
||||
const link = appUrl();
|
||||
await sendAppMail({
|
||||
to: user.email,
|
||||
subject: 'Luminary: your scheduled storage cleanup is complete',
|
||||
@@ -394,8 +421,7 @@ async function pruneDueAccounts() {
|
||||
'',
|
||||
`The scheduled Luminary cleanup removed ${result.removedPhotos} photo${result.removedPhotos === 1 ? '' : 's'} and freed ${fmtBytes(before - after)}.`,
|
||||
`Your library is now ${fmtBytes(after)} of ${fmtBytes(state.limit)} (${state.percent}%).`,
|
||||
'',
|
||||
`Open Luminary: ${appUrl()}`,
|
||||
...(link ? ['', `Open Luminary: ${link}`] : []),
|
||||
'',
|
||||
'— Luminary',
|
||||
].join('\n'),
|
||||
|
||||
Reference in New Issue
Block a user