simioluwatomi's avatar

Spatie Media Library Integration

Currently, in a project of mine, users upload heavy audio files to the application. Because of the file sizes (~ 150MB) each, I decided to upload directly to s3 using pre-signed URLs. This has been working fine so far.

However, I discovered the Spatie Media Library package last week and decided it play with it. I have used the library to replace all image uploads and I'm loving it. However, uploading to s3 is becoming a pain.

This is the method that performs the file upload. When written like this, it actually uploads a file to the tmp folder in the Storage directory.

public function create(Request $request)
{
        if ($request->hasFile('filepond')) {
            $file = $request->file('filepond');
            return Storage::put('tmp', $file);
        }
        return response()->json('false');
}

However, the version below does not work with the Spatie Media Library

public function create(Request $request)
{
        $user = User::find(auth()->id());
        if ($request->hasFile('filepond')) {
            //This does not work
            $user->addMediaFromRequest('filepond')->toMediaCollection('logos', 's3');
            //This also does not work            
            $user->addMedia($request->file('filepond'))->toMediaCollection('logos', 's3');
            return response()->json('true');
        }
        return response()->json('false');
}

With both throwing the Call to a member function addMedia() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function addMedia() on null or Call to a member function addMediaFromRequest() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function addMediaFromRequest() on null

Please help... Thanks

0 likes
3 replies
simioluwatomi's avatar

It turns out I was not applying a middleware on the Controller that was why anything Auth related was null.

Please or to participate in this conversation.