Upload a file to a model in a cron job

System Information
  • Strapi Version: 3.17
  • Operating System: macosx
  • Database: mongodb
  • Node Version: 10.0.0
  • NPM Version: 6.0.0

Hey!

Im trying to upload a file in my cron job in the strapi backend.

What Im trying to do:

  • creating a pdf with pdfkit and get a buffer of it
  • creating a new model entry with strapi services
  • adding the pdf as a new file to the model

my model has 3 fields: field1, field2 and pdf

I tried different things but always get different errors or no error at all (but still no file was uploaded).

things like:

  • add file as buffer object
  • add buffer of path
  • add file object with path, mime, size etc

didnt work out.

What already works:

  • Creating the model entry
  • Creating the pdf
  • Reading the pdf

some code

const newEntry = await strapi.query('mymodel').create({
   field1,
   field2
});

// here I create a pdf
createPdf();

const pdf = fs.readFileSync('config/something/mynew.pdf');

// how to add the file as param?
const files = [??????];

await strapi.entityService.uploadFiles(newEntry,
    files, 
    {
      model: 'mymodel',
    }
);

I also tried it with using with

strapi.plugins['upload'].services.upload.upload

but I just cant figure out how I need to add the files parameter to make it work. Can someone give me some directions?

I only found examples of people uploading with requests to localhost:1337/upload from the frontend. I could do that from the backend too but how would I add the authorization token of the admin user in the cron job before making the request?

1 Like

coudl you find any solution?

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

I’m actually stuck at the very same problem.
How should I pass the files parameter to the initial code snippet?
The file that I wish to upload is located on a server folder and I’m able to access it, however I’m for some reason unable to understand how should I pass it to this function - similarly as in your initial example fs.readFileSync('config/something/mynew.pdf')
or simply as a path?