diff --git a/indiekit.config.js b/indiekit.config.js index f52f728df..291ba0998 100644 --- a/indiekit.config.js +++ b/indiekit.config.js @@ -38,6 +38,15 @@ const config = { published: { required: true }, }, }, + photo: { + media: { + resize: { + width: 100, + height: 50, + fit: "fill", + }, + }, + }, jam: { name: "Jam", post: { diff --git a/package-lock.json b/package-lock.json index 83b919f35..2d55de759 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18360,7 +18360,8 @@ "@indiekit/util": "^1.0.0-beta.6", "express": "^4.17.1", "file-type": "^19.0.0", - "newbase60": "^1.3.1" + "newbase60": "^1.3.1", + "sharp": "^0.33.2" }, "engines": { "node": ">=20" diff --git a/packages/endpoint-media/lib/controllers/action.js b/packages/endpoint-media/lib/controllers/action.js index 9d55a83fb..83867fa8d 100644 --- a/packages/endpoint-media/lib/controllers/action.js +++ b/packages/endpoint-media/lib/controllers/action.js @@ -1,6 +1,7 @@ import { IndiekitError } from "@indiekit/error"; import { mediaContent } from "../media-content.js"; import { mediaData } from "../media-data.js"; +import { mediaTransform } from "../media-transform.js"; import { checkScope } from "../scope.js"; /** @@ -25,6 +26,7 @@ export const actionController = async (request, response, next) => { } let data; + let file; let content; switch (action) { case "media": { @@ -35,8 +37,10 @@ export const actionController = async (request, response, next) => { ); } - data = await mediaData.create(application, publication, files.file); - content = await mediaContent.upload(publication, data, files.file); + file = await mediaTransform(publication, files.file); + + data = await mediaData.create(application, publication, file); + content = await mediaContent.upload(publication, data, file); break; } diff --git a/packages/endpoint-media/lib/media-transform.js b/packages/endpoint-media/lib/media-transform.js new file mode 100644 index 000000000..28956c188 --- /dev/null +++ b/packages/endpoint-media/lib/media-transform.js @@ -0,0 +1,22 @@ +import sharp from "sharp"; +import { getMediaType } from "./file.js"; + +/** + * Apply media transformation + * @param {object} publication - Publication configuration + * @param {object} file - File + * @returns {Promise} Media file + */ +export const mediaTransform = async (publication, file) => { + const { postTypes } = publication; + + const type = await getMediaType(file); + + const typeConfig = postTypes[type]; + + const { resize } = typeConfig.media; + + file.data = await sharp(file.data).resize(resize).toBuffer(); + + return file; +}; diff --git a/packages/endpoint-media/package.json b/packages/endpoint-media/package.json index 6fa4249bb..d9c0a4701 100644 --- a/packages/endpoint-media/package.json +++ b/packages/endpoint-media/package.json @@ -37,7 +37,8 @@ "@indiekit/util": "^1.0.0-beta.6", "express": "^4.17.1", "file-type": "^19.0.0", - "newbase60": "^1.3.1" + "newbase60": "^1.3.1", + "sharp": "^0.33.2" }, "publishConfig": { "access": "public"