UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.21 kB
{"version":3,"file":"get-download-file-name.mjs","names":[],"sources":["../../../src/utils/media/get-download-file-name.ts"],"sourcesContent":["import normalizeMediaKey from \"./normalize-media-key.js\";\n\nconst replaceControlCharacters = (value: string) =>\n\tArray.from(value, (char) => {\n\t\tconst code = char.charCodeAt(0);\n\t\treturn code <= 31 || code === 127 ? \" \" : char;\n\t}).join(\"\");\n\nconst sanitizeDownloadFileName = (value: string) => {\n\tconst basename =\n\t\tvalue.replace(/\\\\/g, \"/\").split(\"/\").filter(Boolean).at(-1) ?? value;\n\n\tconst normalized = replaceControlCharacters(basename)\n\t\t.replace(/\"/g, \"\")\n\t\t.replace(/\\s+/g, \" \")\n\t\t.trim();\n\n\treturn normalized || \"download\";\n};\n\n/**\n * Prefers the stored original file name for downloads and falls back to the\n * canonical key basename when no display name is available.\n */\nconst getDownloadFileName = (props: {\n\tkey: string;\n\tfileName?: string | null;\n\textension?: string | null;\n}) => {\n\tconst persistedFileName = props.fileName?.trim();\n\tif (persistedFileName) return sanitizeDownloadFileName(persistedFileName);\n\n\tconst normalizedKey = normalizeMediaKey(props.key);\n\tconst basename =\n\t\tnormalizedKey.split(\"/\").filter(Boolean).at(-1) ?? normalizedKey;\n\n\tif (!props.extension || basename.endsWith(`.${props.extension}`)) {\n\t\treturn sanitizeDownloadFileName(basename);\n\t}\n\n\treturn sanitizeDownloadFileName(`${basename}.${props.extension}`);\n};\n\nexport default getDownloadFileName;\n"],"mappings":"yCAEA,MAAM,EAA4B,GACjC,MAAM,KAAK,EAAQ,GAAS,CAC3B,IAAM,EAAO,EAAK,WAAW,CAAC,EAC9B,OAAO,GAAQ,IAAM,IAAS,IAAM,IAAM,CAC3C,CAAC,CAAC,CAAC,KAAK,EAAE,EAEL,EAA4B,GAAkB,CACnD,IAAM,EACL,EAAM,QAAQ,MAAO,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,GAAG,EAAE,GAAK,EAOhE,OALmB,EAAyB,CAAQ,CAAC,CACnD,QAAQ,KAAM,EAAE,CAAC,CACjB,QAAQ,OAAQ,GAAG,CAAC,CACpB,KAEc,GAAK,UACtB,EAMM,EAAuB,GAIvB,CACL,IAAM,EAAoB,EAAM,UAAU,KAAK,EAC/C,GAAI,EAAmB,OAAO,EAAyB,CAAiB,EAExE,IAAM,EAAgB,EAAkB,EAAM,GAAG,EAC3C,EACL,EAAc,MAAM,GAAG,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,GAAG,EAAE,GAAK,EAMpD,MAJI,CAAC,EAAM,WAAa,EAAS,SAAS,IAAI,EAAM,WAAW,EACvD,EAAyB,CAAQ,EAGlC,EAAyB,GAAG,EAAS,GAAG,EAAM,WAAW,CACjE"}