web/website/control-panel/quest-category-art.mjs

15 lines
1 KiB
JavaScript

import {readFile} from 'node:fs/promises';
import {QUEST_CATEGORIES,OTHER_CATEGORY} from './public/cp-v2/cp-v2-quest-categories.mjs';
const categories=new Set([...QUEST_CATEGORIES,OTHER_CATEGORY].map(category=>category.id));
// Only the authored category PNGs are public; this is not a directory server.
export async function questCategoryArt(request){
const match=/^\/(?:v2|cp-v2)\/art\/quests\/([a-z0-9-]+)-(icon|banner)\.png$/.exec(new URL(request.url).pathname);
if(!match||!categories.has(match[1]))return null;
if(!['GET','HEAD'].includes(request.method))return new Response('Method not allowed',{status:405,headers:{Allow:'GET, HEAD'}});
try{
const bytes=await readFile(new URL('./public/cp-v2/art/quests/'+match[1]+'-'+match[2]+'.png',import.meta.url));
return new Response(request.method==='HEAD'?null:bytes,{headers:{'Content-Type':'image/png','Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff'}});
}catch(error){if(error.code==='ENOENT')return new Response('Not found',{status:404});throw error;}
}