version 2, with image optimization (unfortunately the stream is replaced by buffer)
const bytesToKbytes = (bytes) => Math.round((bytes / 1000) * 100) / 100;
const createAndAssignTmpWorkingDirectoryToFiles = () => fse.mkdtemp(path.join(os.tmpdir(), 'strapi-upload-'));
const media = await Promise.all(photos.map(async photo => {
const file = await axios.get(photo.url, {
responseType: 'arraybuffer',
});
const fileNameNoExt = path.basename(photo.name, path.extname(photo.name));
const entity = {
name: `${jsonData.name} ${fileNameNoExt}`,
hash: `${slugify(jsonData.name)}_${fileNameNoExt}`,
ext: path.extname(photo.name),
mime: lookup(photo.name),
size: bytesToKbytes(Number(file.headers['content-length'])),
provider: config.provider,
tmpWorkingDirectory: await createAndAssignTmpWorkingDirectoryToFiles(),
getStream: () => Readable.from(file.data),
};
await strapi.plugin('upload').service('upload').uploadImage(entity)
return strapi
.query('plugin::upload.file')
.create({ data: entity });
}));