36 lines
3.1 KiB
JavaScript
36 lines
3.1 KiB
JavaScript
import {readFile} from 'node:fs/promises';
|
|
import {identity} from '../node/auth.mjs';
|
|
import {can} from '../node/permissions.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');
|
|
if(path==='/api/dmpaint')return Response.json({name:'DMPaint',modes:staff?['clan','company','seal','award']:['clan','company']},{headers:{'Cache-Control':'private, no-store'}});
|
|
if(path==='/dmpaint'||path==='/dmpaint/'){
|
|
const mode=url.searchParams.get('mode')||'clan';
|
|
if(!['clan','company','seal','award'].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|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;}
|
|
}
|
|
}
|

Staff