import View from"./view.mjs";import Files from"../../files/files.mjs";import{hasFileExtension}from"../../../common/data/validators.mjs";import{handleDocumentAssetUpload}from"../../database/sanitization.mjs";export default class FileUploadView extends View{route="/upload";_methods=["post"];async handlePost(e,s){const{game:o,config:a}=global;let i=null;if(!(e.session.admin||!o.ready&&!a.adminPassword)&&(o.ready&&(i=await db.User.get(e.user)),!i||!i.hasPermission("FILES_UPLOAD")))return s.json({error:`User [${i?.id??""}] does not have permission to upload files.`});let t=e.body.source;const r=e.files.upload,n={...e.body};if(n.uuid){if(!hasFileExtension(r.name,Object.keys(CONST.IMAGE_FILE_EXTENSIONS)))return s.json({error:"Not an image file."});t="data",n.target=await handleDocumentAssetUpload(n.uuid,r)}const d=await Files.upload(t,r,n).catch((e=>({error:e.message||e})));return s.json(d)}}