System Information
- Strapi Version: 3.4.1
- Operating System: Windows 10
- Database: PostgreSQL
- Node Version:
- NPM Version:
- Yarn Version:
How can I make folders for images in strapi content library?
How can I make folders for images in strapi content library?
Do you have a solution x2? hehe
Also looking for this answer - and it looks like 100+ other people on Github want it too
Hi guys, has anyone found a solution yet?
Hi @dhwind,
I created global middleware file in the src/middlewares folder.
// src/middlewares/uploadMiddleware.js
"use-strict";
module.exports = (config, { strapi }) => {
return async (ctx, next) => {
// Only run this middleware for the upload endpoint
if (ctx.url.startsWith("/upload") && ctx.method === "POST") {
if (ctx.request.body) {
try {
const fileInfo = JSON.parse(ctx?.request?.body?.fileInfo);
const extension = fileInfo.name.split(".").pop().toLowerCase();
if (extension == "pdf") {
fileInfo["folder"] = 4;
} else {
fileInfo["folder"] = 3;
}
ctx.request.body.fileInfo = JSON.stringify(fileInfo);
} catch (error) {
console.error("Error parsing or modifying fileInfo:", error);
}
}
}
// Continue to the next middleware
await next();
};
};
And update config/middlewares.js file.
`module.exports = [
// rest middlewares
"global::uploadMiddleware",
// rest middleware
];
`
But before that, we have to create folders in the media library. folder name will be 1,2,3,4,… according to folder creation order. Then it will save according to logic in middleware.