diff --git a/packages/endpoint-media/lib/file.js b/packages/endpoint-media/lib/file.js index 335b87815..297a3d076 100644 --- a/packages/endpoint-media/lib/file.js +++ b/packages/endpoint-media/lib/file.js @@ -1,26 +1,31 @@ -import { getDate } from "@indiekit/util"; +import path from "node:path"; +import { getDate, slugify } from "@indiekit/util"; import { fileTypeFromBuffer } from "file-type"; /** * Derive properties from file data - * @param {object} timeZone - Application time zone + * @param {object} publication - Publication configuration * @param {object} file - Original file object + * @param {object} timeZone - Application time zone * @returns {Promise} File properties - * @example fileData('brighton-pier.jpg') => { + * @example fileData('Brighton Pier.jpg') => { * ext: '.jpg' * filename: 'brighton-pier.jpg', * 'content-type': image/jpeg, * published: '2020-07-19T22:59:23.497Z', * } */ -export const getFileProperties = async (timeZone, file) => { +export const getFileProperties = async (publication, file, timeZone) => { const { ext } = await fileTypeFromBuffer(file.data); const published = getPublishedProperty(timeZone); + let basename = path.basename(file.name, ext); + basename = slugify(basename, publication.slugSeparator); + return { "content-type": file.mimetype, ext, - filename: file.name, + filename: `${basename}.${ext}`, md5: file.md5, published, }; diff --git a/packages/endpoint-media/lib/media-data.js b/packages/endpoint-media/lib/media-data.js index d6fccf8be..9432f9f88 100644 --- a/packages/endpoint-media/lib/media-data.js +++ b/packages/endpoint-media/lib/media-data.js @@ -18,10 +18,10 @@ export const mediaData = { debug(`create %O`, { file }); const { hasDatabase, media, timeZone } = application; - const { me, postTypes, slugSeparator } = publication; + const { me, postTypes } = publication; // Media properties - const properties = await getFileProperties(timeZone, file); + const properties = await getFileProperties(publication, file, timeZone); // Get post type configuration const type = await getMediaType(file); @@ -44,13 +44,11 @@ export const mediaData = { typeConfig.media.path, properties, application, - slugSeparator, ); const url = await renderPath( typeConfig.media.url || typeConfig.media.path, properties, application, - slugSeparator, ); properties.url = getCanonicalUrl(url, me); diff --git a/packages/endpoint-media/lib/utils.js b/packages/endpoint-media/lib/utils.js index 0560fa2c8..1f6b038be 100644 --- a/packages/endpoint-media/lib/utils.js +++ b/packages/endpoint-media/lib/utils.js @@ -1,9 +1,7 @@ -import { basename as getBasename } from "node:path"; import { dateTokens, formatDate, getTimeZoneDesignator, - slugify, supplant, } from "@indiekit/util"; import newbase60 from "newbase60"; @@ -29,10 +27,9 @@ export const getMediaProperties = (mediaData) => { * @param {string} path - URI template path * @param {object} properties - Media properties * @param {object} application - Application configuration - * @param {string} separator - Slug separator * @returns {Promise} Path */ -export const renderPath = async (path, properties, application, separator) => { +export const renderPath = async (path, properties, application) => { const dateObject = new Date(properties.published); const serverTimeZone = getTimeZoneDesignator(); const { locale, timeZone } = application; @@ -58,10 +55,8 @@ export const renderPath = async (path, properties, application, separator) => { // Add file extension token tokens.ext = properties.ext; - // Add slugified file name token - let basename = getBasename(properties.filename, properties.ext); - basename = slugify(basename, separator); - tokens.filename = `${basename}.${properties.ext}`; + // Add file name token + tokens.filename = properties.filename; // Add md5 token tokens.md5 = properties.md5; diff --git a/packages/endpoint-media/test/unit/file.js b/packages/endpoint-media/test/unit/file.js index 7d8608f8c..bf9c7283f 100644 --- a/packages/endpoint-media/test/unit/file.js +++ b/packages/endpoint-media/test/unit/file.js @@ -18,15 +18,18 @@ describe("endpoint-media/lib/file", () => { }); it("Derives properties from file data", async () => { + const publication = { + slugSeparator: "-", + }; const file = { data: getFixture("file-types/photo.jpg", false), - name: "photo.jpg", + name: "Photo 1.jpg", md5: "be7d321488de26f2eb38834af7162164", }; - const result = await getFileProperties("UTC", file); + const result = await getFileProperties(publication, file, "UTC"); assert.equal(result.ext, "jpg"); - assert.equal(result.filename, "photo.jpg"); + assert.equal(result.filename, "photo-1.jpg"); assert.equal(result.md5, "be7d321488de26f2eb38834af7162164"); assert.equal(isValid(parseISO(result.published)), true); }); diff --git a/packages/endpoint-media/test/unit/utils.js b/packages/endpoint-media/test/unit/utils.js index 532098099..6c9ebe92f 100644 --- a/packages/endpoint-media/test/unit/utils.js +++ b/packages/endpoint-media/test/unit/utils.js @@ -5,7 +5,7 @@ import { renderPath } from "../../lib/utils.js"; describe("endpoint-media/lib/util", () => { const properties = { ext: "jpg", - filename: "Foo 1.jpg", + filename: "foo-1.jpg", md5: "be7d321488de26f2eb38834af7162164", published: "2020-01-01", }; @@ -16,9 +16,9 @@ describe("endpoint-media/lib/util", () => { }); it("Renders path from URI template and properties", async () => { - const dateToken = await renderPath("{yyyy}/{MM}", properties, {}, "-"); - const fileToken = await renderPath("{filename}", properties, {}, "-"); - const md5Token = await renderPath("{md5}", properties, {}, "-"); + const dateToken = await renderPath("{yyyy}/{MM}", properties, {}); + const fileToken = await renderPath("{filename}", properties, {}); + const md5Token = await renderPath("{md5}", properties, {}); assert.match(dateToken, /^\d{4}\/\d{2}/); assert.match(fileToken, /^foo-1\.jpg/);