Files
Foundry-VTT-Docker/resources/app/dist/database/sanitization.mjs
2025-01-04 00:34:03 +01:00

1 line
1.9 KiB
JavaScript

import fs from"node:fs";import path from"node:path";import{cleanHTML}from"./validators.mjs";import Files from"../files/files.mjs";import{fromUuid}from"../core/utils.mjs";import{randomID}from"../../common/utils/helpers.mjs";import{EmbeddedCollectionField,EmbeddedDocumentField,FilePathField}from"../../common/data/fields.mjs";export function sanitizeFilePathField(e,{assetPath:t,document:i,documentId:n,fieldPath:o,user:a}={}){if(null==e||""===e)return e;if("string"!=typeof e)return null;if(this.base64)return e;const r=e.match(/^data:([a-z]+)\/([a-z0-9]+);base64,(.*)/);if(!r)return e;if(a&&!a.can("FILES_UPLOAD"))throw new Error(`You lack FILES_UPLOAD permission and may not upload base64 data to the ${this.name} field.`);const[d,s]=r.slice(2,4);n??=i._id;const l=`${[n,...o].filterJoin("-")}.${d}`,m=path.join(t,l);return fs.mkdirSync(t,{recursive:!0}),fs.writeFileSync(m,Buffer.from(s,"base64")),global.logger.info(`Extracted base64 asset: "${m}"`),Files.standardizePath(path.relative(global.paths.data,m))}export async function handleDocumentAssetUpload(e,t){const i=await fromUuid(e),n=i.constructor.extractedAssetPath,o=t.name.split("."),a=o.pop();return t.name=`${o.join(".")}-${randomID()}.${a}`,t.name=i.parent?[i.parent.id,i.collectionName,i.id,t.name].join("-"):t.name,fs.mkdirSync(n,{recursive:!0}),Files.standardizePath(path.relative(global.paths.data,n))}export function sanitizeHTMLField(e,t={}){return null==e||""===e?e:"string"!=typeof e?"":cleanHTML(e)}export function sanitizeEmbeddedCollectionField(e,t={}){const i=t.document?.[t.fieldPath.at(-1)];for(const[n,o]of e.entries()){const a=i?.get(o._id);e[n]=this.model.sanitizeUserInput(o,{...t,document:a,documentId:o._id,fieldPath:t.fieldPath.concat([o._id])})}return e}export function sanitizeEmbeddedDocumentField(e,t={}){const i=t.document?.[t.fieldPath.at(-1)];return this.model.sanitizeUserInput(e,{...t,document:i,documentId:e?._id})}