Skip to content

Commit

Permalink
refactor(endpoint-media): slugify filename when creating file properties
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Mar 30, 2024
1 parent 56072d0 commit 26a41b2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
15 changes: 10 additions & 5 deletions packages/endpoint-media/lib/file.js
Original file line number Diff line number Diff line change
@@ -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<object>} 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,
};
Expand Down
6 changes: 2 additions & 4 deletions packages/endpoint-media/lib/media-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
11 changes: 3 additions & 8 deletions packages/endpoint-media/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { basename as getBasename } from "node:path";
import {
dateTokens,
formatDate,
getTimeZoneDesignator,
slugify,
supplant,
} from "@indiekit/util";
import newbase60 from "newbase60";
Expand All @@ -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<string>} 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;
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions packages/endpoint-media/test/unit/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/endpoint-media/test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand All @@ -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/);
Expand Down

0 comments on commit 26a41b2

Please sign in to comment.