Allow hotlinking OG images

This commit is contained in:
Jade Ellis
2024-07-23 23:04:17 +01:00
parent 69badb0c98
commit 555990a5ad
@@ -37,6 +37,7 @@ export async function GET({ url }) {
if (width > 10000 || ratio > 50) { if (width > 10000 || ratio > 50) {
throw error(400, 'Image too big') throw error(400, 'Image too big')
} }
let image;
if (!cache.has(slug + "/" + dateParts?.join("-") + "/" + width + "/" + ratio)) { if (!cache.has(slug + "/" + dateParts?.join("-") + "/" + width + "/" + ratio)) {
// let start = new Date(dateParts[0] || 1, dateParts[1] || 0, dateParts[2] || 0); // let start = new Date(dateParts[0] || 1, dateParts[1] || 0, dateParts[2] || 0);
@@ -123,24 +124,22 @@ export async function GET({ url }) {
} }
}); });
const image = resvg.render().asPng(); image = resvg.render().asPng();
cache.set(slug + "/" + dateParts?.join("-") + "/" + width, image) cache.set(slug + "/" + dateParts?.join("-") + "/" + width, image)
return new Response(image, {
headers: {
'content-type': 'image/png'
}
});
; ;
} else { } else {
return new Response(cache.get(slug + "/" + dateParts?.join("-") + "/" + width), { image = cache.get(slug + "/" + dateParts?.join("-") + "/" + width) as Buffer
}
return new Response(image, {
headers: { headers: {
'Content-Type': 'image/png', 'Content-Type': 'image/png',
'Cache-Control': format({ 'Cache-Control': format({
public: true, public: true,
// immutable: true // immutable: true
maxAge: 60 * 60 * 24 maxAge: 60 * 60 * 24
}) }),
'Cross-Origin-Resource-Policy': 'cross-origin'
} }
}); });
}
} }