add legacy image rewrite helper

src/lib/legacy-image.ts exports rewriteLegacyImageUrl(url) and
rewriteLegacyImageUrlsInHtml(html) which translate WP origin URLs
(https://www.broadcastbeat.com/wp-content/uploads/...) to local paths
under /legacy/... served from the new persistent storage volume mounted
at /app/public/legacy/.

src/lib/legacy-image-map.json holds 3,856 URL→path entries built in
Phase 2: 3,839 featured images (mostly converted to webp, q=85) plus
27 ad creatives plus the site logo.

Use this helper at server-render time when emitting <img>/<Image> for
imported WP articles, so the 707 MB on-host cache replaces external
fetches to the origin.
This commit is contained in:
Ryan Salazar
2026-05-08 00:04:34 +00:00
parent 0d51dade6e
commit ef1921ca7c
2 changed files with 3892 additions and 0 deletions

File diff suppressed because it is too large Load Diff

34
src/lib/legacy-image.ts Normal file
View File

@@ -0,0 +1,34 @@
import map from "./legacy-image-map.json";
const LEGACY_MAP = map as Record<string, string>;
const LEGACY_HOSTS = [
"https://www.broadcastbeat.com",
"https://broadcastbeat.com",
"http://www.broadcastbeat.com",
"http://broadcastbeat.com",
];
export function rewriteLegacyImageUrl(url: string | null | undefined): string {
if (!url) return "";
// Direct map hit (canonical https://www.broadcastbeat.com/...)
const direct = LEGACY_MAP[url];
if (direct) return direct;
// Try alternate-host normalization
for (const host of LEGACY_HOSTS) {
if (url.startsWith(host)) {
const path = url.slice(host.length);
const canonical = `https://www.broadcastbeat.com${path}`;
const hit = LEGACY_MAP[canonical];
if (hit) return hit;
}
}
return url;
}
export function rewriteLegacyImageUrlsInHtml(html: string): string {
return html.replace(
/(https?:\/\/(?:www\.)?broadcastbeat\.com\/wp-content\/uploads\/[^"'\s)]+)/g,
(m) => rewriteLegacyImageUrl(m),
);
}