import {readFile} from 'node:fs/promises'; import {identity} from '../node/auth.mjs'; import {can} from '../node/permissions.mjs'; import {templateModes,templateCatalogue,renderPaintTemplate} from './dmpaint-templates.mjs'; const TYPES={html:'text/html',mjs:'text/javascript',js:'text/javascript',css:'text/css',svg:'image/svg+xml',png:'image/png',jpg:'image/jpeg',gif:'image/gif',json:'application/json',txt:'text/plain',woff:'font/woff',woff2:'font/woff2',ttf:'font/ttf'}; export const DMPAINT_CSP="default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'"; export function createDMPaint(db,{authenticate=request=>identity(request,db),authorise=(user,node)=>can(db,user,node),load=readFile}={}){ return async request=>{ const url=new URL(request.url),path=url.pathname; if(!['GET','HEAD'].includes(request.method))return new Response('Method not allowed',{status:405,headers:{Allow:'GET, HEAD'}}); // The icon is also used by the existing House colours field on other CP surfaces. if(path==='/dmpaint/icon.png')return asset('./public/dmpaint/icon.png','png',request); const user=await authenticate(request); if(!user)return Response.json({message:'Sign in to use DMPaint.'},{status:401,headers:{'Cache-Control':'no-store'}}); const staff=await authorise(user,'web.accounts.assign'); const modes=templateModes.filter(mode=>staff||!['seal','award'].includes(mode)); if(path==='/api/dmpaint')return Response.json({name:'DMPaint',modes},{headers:{'Cache-Control':'private, no-store'}}); if(path==='/dmpaint/templates.json'||path==='/dmpaint/template.svg'){ const mode=url.searchParams.get('mode')||'clan'; if(!templateModes.includes(mode))return new Response('Unknown design type',{status:400}); if(!modes.includes(mode))return new Response('Staff permission required',{status:403}); if(path.endsWith('.json'))return Response.json({templates:templateCatalogue(mode)},{headers:{'Cache-Control':'private, no-store'}}); try{ const input={mode,id:url.searchParams.get('design')}; for(const field of ['title','primary','secondary','trim','outfit'])if(url.searchParams.has(field))input[field]=url.searchParams.get(field); if(mode==='seal'||mode==='award'){ const file=mode==='seal'?'imperial-post-seal-v2.png':'seals/award-network-staff.png'; input.sealData='data:image/png;base64,'+Buffer.from(await load(new URL('../web/assets/'+file,import.meta.url))).toString('base64'); } const svg=renderPaintTemplate(input); return new Response(request.method==='HEAD'?null:svg,{headers:{'Content-Type':'image/svg+xml; charset=utf-8','Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff','Content-Security-Policy':DMPAINT_CSP}}); }catch(error){if(error instanceof TypeError)return new Response(error.message,{status:400});throw error;} } if(path==='/dmpaint'||path==='/dmpaint/'){ const mode=url.searchParams.get('mode')||'clan'; if(!templateModes.includes(mode))return new Response('Unknown design type',{status:400}); if(['seal','award'].includes(mode)&&!staff)return new Response('Staff permission required',{status:403}); return asset('./public/dmpaint/index.html','html',request); } const own=path.match(/^\/dmpaint\/(entry\.mjs|templates\.mjs|theme\.css)$/); if(own)return asset('./public/dmpaint/'+own[1],own[1].split('.').at(-1),request); if(path.startsWith('/dmpaint/vendor/')||path.startsWith('/dmpaint/images/')){ const file=path.startsWith('/dmpaint/images/')?'images/'+path.slice('/dmpaint/images/'.length):path.slice('/dmpaint/vendor/'.length),extension=file.split('.').at(-1); if(!file||!file.split('/').every(part=>/^[a-zA-Z0-9_@][a-zA-Z0-9_.@-]*$/.test(part))||!TYPES[extension]||extension==='html')return new Response('Not found',{status:404}); return asset('./public/vendor/svg-edit/'+file,extension,request); } return new Response('Not found',{status:404}); }; async function asset(path,extension,request){ try{return new Response(request.method==='HEAD'?null:await load(new URL(path,import.meta.url)),{headers:{'Content-Type':TYPES[extension]+(extension==='html'||extension==='js'||extension==='mjs'||extension==='css'?'; charset=utf-8':''),'Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff','Content-Security-Policy':DMPAINT_CSP}});} catch(error){if(error.code==='ENOENT')return new Response('DMPaint asset unavailable',{status:404});throw error;} } }