Search code examples
laravelvuejs2laravel-spark

Laravel Spark upload profile picture to external driver


I want to override the way Laravel Spark save the profile picture of a user to use an external driver such as S3 for example. I already have my S3 config for the bucket I want to use. What would be the best way to do this? Should I use a completely different route and use a custom endpoint or is there a config somewhere I could change so that Spark uses a different driver?


Solution

  • So ended up doing this

    I added these methods in update-profile-photo.js

    methods: {
        updateProfilePhoto() {
    
            axios.post('/settings/profile/details/profile-picture', this.gatherFormData())
                .then(
                    () => {
                        console.log('Profile picture updated');
                        Bus.$emit('updateUser');
    
                        self.form.finishProcessing();
                    },
                    (error) => {
                        self.form.setErrors(error.response.data.errors);
                    }
                );
        },
        gatherFormData() {
            const data = new FormData();
    
            data.append('photo', this.$refs.photo.files[0]);
    
            return data;
        }
    }
    

    And my Controller looked like this

    public function updateProfilePicture(Request $request)
    {
        $this->validate($request, [
            'photo' => 'required',
        ]);
    
        // Storing the photo
        //get filename with extension
        $filenamewithextension = $request->file('photo')->getClientOriginalName();
    
        //get filename without extension
        $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
    
        //get file extension
        $extension = $request->file('photo')->getClientOriginalExtension();
    
        //filename to store
        $filenametostore = $filename.'_'.time().'.'.$extension;
    
        Storage::disk('s3_users')->put($filenametostore, fopen($request->file('photo'), 'r+'), 'public');
    
        $url = $filenametostore;
    
        $request->user()->forceFill([
            'image_url' => $url
        ])->save();
    
    
        return response()->json(
            array(
                "message" => "Profile picture was updated!",
            )
        );
    
    }