Skip to content

Commit

Permalink
feat(endpoint-media): media transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Feb 28, 2024
1 parent 3c1d6af commit 245fd20
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
9 changes: 9 additions & 0 deletions indiekit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const config = {
published: { required: true },
},
},
photo: {
media: {
resize: {
width: 100,
height: 50,
fit: "fill",
},
},
},
jam: {
name: "Jam",
post: {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/endpoint-media/lib/controllers/action.js
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -25,6 +26,7 @@ export const actionController = async (request, response, next) => {
}

let data;
let file;
let content;
switch (action) {
case "media": {
Expand All @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/endpoint-media/lib/media-transform.js
Original file line number Diff line number Diff line change
@@ -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<object>} 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;
};
3 changes: 2 additions & 1 deletion packages/endpoint-media/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 245fd20

Please sign in to comment.