mirror of
https://github.com/direct-img/direct-img.link.git
synced 2026-10-29 07:24:52 +00:00
Feat: Expire static images after 90 days like R2, and link top images to direct-img in ntfy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+8
-12
@@ -1,6 +1,6 @@
|
||||
// Sends the deploy's outcome to ntfy: what got promoted, how close the account is to the free plan's limits, and what
|
||||
// to do when something needs attention. Runs last in .github/workflows/deploy.yml, even when an earlier step failed.
|
||||
import { readFile, appendFile } from "node:fs/promises";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
const env = process.env;
|
||||
const r = JSON.parse(await readFile("promoted/report.json", "utf8").catch(() => "{}"));
|
||||
@@ -31,21 +31,17 @@ if (r.workers) {
|
||||
}
|
||||
if (r.workersError) lines.push(`**Worker requests:** couldn't read (${esc(r.workersError)})`);
|
||||
|
||||
// The top images, with thumbnails, go on the run's summary page, which the message links to. free/2/orange+cat →
|
||||
// https://free.direct-img.link/orange+cat?i=2 (a static file, so viewing the page doesn't cost Worker requests).
|
||||
if (r.top?.length && env.GITHUB_STEP_SUMMARY) {
|
||||
const zone = env.ZONE || "direct-img.link";
|
||||
const rows = r.top.map(({ id, views }) => {
|
||||
const [f, i, name] = id.split("/"), url = `https://${f === "web" ? "" : `${f}.`}${zone}/${name}${i > 1 ? `?i=${i}` : ""}`;
|
||||
return `| <img src="${url}" height="64"> | [${esc(name.replaceAll("+", " "))}](${url}) | ${f === "web" ? "main" : f} | ${i} | ${fmt(views)} |`;
|
||||
});
|
||||
await appendFile(env.GITHUB_STEP_SUMMARY, `## Top static images (views in the last ${r.days} days)\n\n| | Query | Site | i | Views |\n|---|---|---|---|---|\n${rows.join("\n")}\n`);
|
||||
}
|
||||
// web/2/orange+cat → [orange cat](https://direct-img.link/orange+cat?i=2), so each opens on its site
|
||||
const zone = env.ZONE || "direct-img.link";
|
||||
const link = id => {
|
||||
const [f, i, name] = id.split("/");
|
||||
return `[${esc(name.replaceAll("+", " "))}](https://${f === "web" ? "" : `${f}.`}${zone}/${name}${i > 1 ? `?i=${i}` : ""})${i > 1 ? ` i=${i}` : ""}${f === "web" ? "" : ` (${f})`}`;
|
||||
};
|
||||
|
||||
const failed = env.STATUS !== "success";
|
||||
const body = [
|
||||
lines.join(" \n"),
|
||||
r.top?.length && env.RUN_URL && `[See top images](${env.RUN_URL})`,
|
||||
r.top?.length && `**Top images**\n${r.top.map((t, n) => `${n + 1}. ${link(t.id)} — ${fmt(t.views)}`).join("\n")}`,
|
||||
actions.length && `**⚠ Needs attention**\n${actions.map(a => `- ${a}`).join("\n")}`,
|
||||
].filter(Boolean).join("\n\n") || "No report was written.";
|
||||
console.log(body);
|
||||
|
||||
+11
-7
@@ -4,7 +4,7 @@
|
||||
// promoted/report.json for scripts/notify.js. promoted/ is kept between runs (Actions cache), so only new images download.
|
||||
import { mkdir, readFile, writeFile, link, rm, readdir } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
import { MAX_INDEX, siteOf, folder, normalizeQuery, cacheKey, r2Key } from "../src/_utils/keys.js";
|
||||
import { TTL_SECONDS, MAX_INDEX, siteOf, folder, normalizeQuery, cacheKey, r2Key } from "../src/_utils/keys.js";
|
||||
import { parseFreeSource } from "../src/_utils/free.js";
|
||||
import { parseAiSource } from "../src/_utils/ai.js";
|
||||
|
||||
@@ -83,20 +83,24 @@ async function workerRequests(day) {
|
||||
return { total: Object.values(byScript).reduce((t, r) => t + r, 0), byScript };
|
||||
}
|
||||
|
||||
// The image from the last run's cache, or from R2. Skips a failed search's empty marker and formats browsers can't detect.
|
||||
// The image from the last run's cache, or from R2. Skips a failed search's empty marker, formats browsers can't detect,
|
||||
// and images past TTL_SECONDS, so a popular query gets searched again like any other instead of staying static forever.
|
||||
const expired = uploaded => Date.now() - uploaded >= TTL_SECONDS * 1000;
|
||||
async function fetchImage(img, prev) {
|
||||
const file = `${CACHE}/files/${img.id}`;
|
||||
if (prev[img.id] && await readFile(file).then(() => true, () => false)) return { ...prev[img.id], cached: true };
|
||||
const file = `${CACHE}/files/${img.id}`, p = prev[img.id];
|
||||
if (p?.uploaded && await readFile(file).then(() => true, () => false)) return expired(p.uploaded) ? { skip: "expired" } : { ...p, cached: true };
|
||||
const res = await api(`/accounts/${env.CLOUDFLARE_ACCOUNT_ID}/r2/buckets/${BUCKET}/objects/${img.r2}`);
|
||||
if (res.status === 404) return { skip: "missing" };
|
||||
if (!res.ok) return { skip: "failed", error: `${img.id}: HTTP ${res.status}` };
|
||||
const type = res.headers.get("content-type")?.split(";")[0].trim().toLowerCase(), buf = Buffer.from(await res.arrayBuffer());
|
||||
const uploaded = Date.parse(res.headers.get("last-modified"));
|
||||
if (!buf.length) return { skip: "missing" };
|
||||
if (expired(uploaded)) return { skip: "expired" };
|
||||
if (!TYPES.includes(type)) return { skip: "type" };
|
||||
if (buf.length > 25 * 2 ** 20) return { skip: "size" };
|
||||
await mkdir(dirname(file), { recursive: true });
|
||||
await writeFile(file, buf);
|
||||
return { bytes: buf.length, type };
|
||||
return { bytes: buf.length, type, uploaded };
|
||||
}
|
||||
|
||||
const report = { ok: false, minViews: MIN_VIEWS, days: DAYS, maxFiles: MAX_FILES, maxBytes: MAX_BYTES, skipped: {} };
|
||||
@@ -140,7 +144,7 @@ for (let k = 0; k < ranked.length; k += 4) {
|
||||
} else if (n >= MAX_FILES) report.notFitted = (report.notFitted || 0) + 1;
|
||||
else if (bytes + g.bytes > MAX_BYTES) report.skipped.budget = (report.skipped.budget || 0) + 1;
|
||||
else {
|
||||
chosen[img.id] = { views: img.views, bytes: g.bytes, type: g.type, r2: img.r2 };
|
||||
chosen[img.id] = { views: img.views, bytes: g.bytes, type: g.type, r2: img.r2, uploaded: g.uploaded };
|
||||
bytes += g.bytes;
|
||||
n++;
|
||||
}
|
||||
@@ -167,7 +171,7 @@ Object.assign(report, {
|
||||
ok: true, promoted: ids.length, bytes,
|
||||
added: ids.filter(id => !prev[id]).length, removed: Object.keys(prev).filter(id => !chosen[id]).length,
|
||||
promotedViews: ids.reduce((a, id) => a + chosen[id].views, 0),
|
||||
top: ids.slice(0, 50).map(id => ({ id, views: chosen[id].views })),
|
||||
top: ids.slice(0, 10).map(id => ({ id, views: chosen[id].views })),
|
||||
});
|
||||
await writeFile(`${CACHE}/report.json`, JSON.stringify(report, null, 2));
|
||||
console.log(JSON.stringify(report, null, 2));
|
||||
|
||||
Reference in New Issue
Block a user