System Information
- Strapi Version: 4.1.9
- Operating System: MacOS
- Database: Postgres
- Node Version: 16
- NPM Version: 6.14
- Yarn Version: 1.19.1
Hi,
I create a pdf file server side in a controller and want to upload it using my strapi upload provider.
In Strapi V3, I used to do it calling the upload function from upload plugin directly, using an object containing all data, like that :
const uploadAndLinkDocument = async (document, name, refId, ref, field) => {
// add generated document
const uploadService = strapi.plugins.upload.services.upload;
// Transform stream files to buffer
const parts = await toArray(document);
const buffers = parts.map((part) =>
_.isBuffer(part) ? part : Buffer.from(part)
);
const buffer = Buffer.concat(buffers);
const data = {
fileInfo: { name },
refId,
ref,
field,
};
await uploadService.upload({
data,
files: {
name,
buffer: true,
path: buffer,
type: "application/pdf",
size: document.size,
},
});
};
But in strapi v4, this fails because the enhanceFile method from upload controller tries to read file.path as it’s a real file. Line 118 :
fs.createReadStream(file.path);
So this throws an error with a buffer.
Does anyone knows how to use strapi upload provider to upload a generated Buffer ?
Thanks !