Skip to content

Commit

Permalink
Fix placeholder gen when MIME is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
WalkingPizza committed Aug 26, 2022
1 parent dca93b0 commit 6732cd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const { getService, isValidMimeType } = require('./utils');
const { getService, canGeneratePlaceholder } = require('./utils');

module.exports = ({ strapi }) => {
/* Generate a placeholder when a new image is uploaded or updates */

const generatePlaceholder = async (event) => {
const { data } = event.params;
if (!isValidMimeType(data.mime)) return;
if (!canGeneratePlaceholder(data)) return;
data.placeholder = await getService(strapi, 'placeholder').generate(data.url);
};

Expand Down
14 changes: 9 additions & 5 deletions server/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
'use strict';

const pluginId = require('./plugin-id');
const mimeTypes = require('mime-types');

/**
* Checks whether the passed MIME type is supported by the plugin, hence whetehr a placeholder can be generated.
* @param {string} mimeType the MIME type of the file we want to generate a placeholder for
* @returns {boolean} whether a placeholder can be generated for the given MIME type
* Checks whether the passed file has a MIME type that is supported by the plugin, hence whether a placeholder can be generated.
* @param {string} file the file we want to generate a placeholder for
* @returns {boolean} whether a placeholder can be generated for the given file
*/

const isValidMimeType = (mimeType) => mimeType.startsWith('image/');
const canGeneratePlaceholder = (file) => {
if (!file.mime) file.mime = mimeTypes.lookup(file.name);
return file.mime?.startsWith('image/');
};

/**
* Helper that retrieves one of the available services of this plugin from Strapi.
Expand All @@ -19,6 +23,6 @@ const isValidMimeType = (mimeType) => mimeType.startsWith('image/');
const getService = (strapi, service) => strapi.plugin(pluginId).service(service);

module.exports = {
isValidMimeType,
canGeneratePlaceholder,
getService,
};

0 comments on commit 6732cd8

Please sign in to comment.