Overriding Service Methods in Strapi V4’s Upload Plugin

Sebastian Scholl
2 min readFeb 8, 2023
Photo by Sigmund on Unsplash

Strapi CMS is a popular, open-source headless CMS that provides a flexible and easy-to-use platform for building web applications. One of its core plugins is the upload provider, but the default implementation ships with some biased configurations that can lead to trouble.

In this article, I’ll show you a simple method for overriding the default formatFileInfo method in Strapi to stop it from automatically changing and adding a hash to uploaded filenames.

The Problem

In some cases, we want to preserve the original filename of an uploaded file/object. By default, Strapi will change it using the following method in its source code:

const generateFileName = (name) => {
const baseName = nameToSlug(name, { separator: '_', lowercase: false });

return `${baseName}_${randomSuffix()}`;
};

The Solution

To resolve this issue, we need to override the default formatFileInfo method, which leverages the generateFileName function.

Overriding a plugin method

  1. Open the src/index.js file in your project and add the following code inside the register method. What we’re doing is swapping out the plugin’s service method with our own. Note that this will work…

--

--