Can't successfully upload a file to an existing entry from Node JS

The only way I have been able to use the in-built strapi upload service is if you first save the file to a folder. I’m not sure what the implications will be for different hosting providers but you can probably programmatically delete this stored file.

const fetch = require("node-fetch"); // is already a strapi dependency
const mime = require("mime-types"); // is already a strapi dependency
const path = require("path");
const fs = require("fs");

const url = "YOUR_URL";

const filepath = path.basename(url);
const filename = path.basename(filepath, path.extname(filepath));

let dest = fs.createWriteStream(filepath);

await new Promise((resolve, reject) =>
  fetch(url)
    .then((res) => res.body.pipe(dest).on("finish", () => resolve(true))) // you can add more error handling
    .catch((e) => reject(e))
);

const fileStat = fs.statSync(filepath);

try {
  await strapi.plugins.upload.services.upload.upload({
    data: {},
    files: {
      path: filepath,
      name: filename,
      type: mime.lookup(filepath),
      size: fileStat.size,
    },
  });
} catch (e) {
  console.log("Error", e);
}

The problem is the enhanceFile function within the strapi-plugin-upload. It expects the file.path to be a string. You could override this to check if the file is already a buffer (I think you would need to override quite a few more files though).

async enhanceFile(file, fileInfo = {}, metas = {}) {
    console.log('enhanceFile',file)
    let readBuffer;
    try {
      readBuffer = await util.promisify(fs.readFile)(file.path);
    } catch (e) {
      if (e.code === 'ERR_FS_FILE_TOO_LARGE') {
        throw strapi.errors.entityTooLarge('FileTooBig', {
          errors: [
            {
              id: 'Upload.status.sizeLimit',
              message: `${file.name} file is bigger than the limit size!`,
              values: { file: file.name },
            },
          ],
        });
      }
      throw e;
    }

thank you man