Upload a file to a model in a cron job

Hi,

I currently have a working solution, but it involves a change in the main strapi-plugin-upload code :frowning:

In a nutshell, currently it’s not possible to use anything else than a real file to upload to the Media library. So if you do this in a nodejs service outside of a browser context, you’ll have to store the file first (you can use the strapi.services.upload.provider.upload() method to do this).
The culprit is the enhanceFile method in the Upload service that expects the file to be a File object containing a valid path attribute. A Buffer however has no path, but a buffer attribute that is handled correctly by fs.readFile. This errors out though as there’s no check in place if the path attribute exists.

The solution is a tiny change to allow the Buffer to be passed as well if the path attribute is falsey, as I did in my own fork, see the diff here

You’ll still need to pass in the related model info as well, contained in the data portion of your request, the files (either as Buffer(s) or File(s)) go under the files key. Apparently you could also add a related property (with an object containing the relation values) to the Buffer object, but I’m not yet ready testing this fully.

Hope this helps!

1 Like