Upload image url

Hello!

After a bit of trial and error, I got this working in V4! My use case is that I fetch the profile image of users who authenticate via third party provider, and then upload that image to my Strapi / S3 bucket.

Here is my implementation. I’m using a beforeCreate lifecycle function in src/extensions/users-permissions/content-types/user/lifecycles.js

module.exports = {
    beforeCreate(event) {

        const axios = require("axios");
        const FormData = require('form-data');

        if (event.params.provider !== "local") {
            try {
                const img = event.params.data.profile_picture_social; // url

                async function fetchAndUploadSocialImage(img) {
                    const response = await axios.get(img, {responseType: "arraybuffer"});

                    const form = new FormData();
                    form.append("files", response.data, `useravatar-${event.params.data.username}.jpg`)
                    
                    const upload = await axios.post(`${process.env.API_URL}/api/upload`, form).catch((error)=> {
                        console.log(error.response.data.error)
                    })
                }
                fetchAndUploadSocialImage(img);
            } catch (error) {
                console.log(error)
            }
        }
    },
}