Skip to content
Advertisement

How to play video from laravel storage directory after verifying the User Login

i have been developing a service where users will be able to access content only and only if they have paid for the content . so i am uploading the content to storage directory and for accessing content i have created routes like this ..

Route::any('storage/course/videos/{video_id}',['uses'=>'StorageController@video','middleware'=>'auth']);

and then in storage controller i am fetching file and returning it as response like this …

public function video($video_id){
    //Some logic for subscription check goes here
        $path = storage_path('app/public/course/videos/'.$video_id);
        // return $path;
        if (!File::exists($path)) {
            abort(404);
        }

        $file = File::get($path);
        $type = File::mimeType($path);

        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
        // $response->header("Accept-Ranges", 'bytes');
         //$response->header("Content-Length", 51265590);
         // $response->header("Content-Disposition" ,"attachment");  //Triggers Download

        return $response;
    }

This trick is working as expected for images but not for videos .Even video download is working but browser fails to play the video each time .Initially i thought there might be some header missing but so i tried to some headers but it didn’t work.

So does anyone what i am doing wrong OR is it even possible to do so OR any other technique to achieve this

Thanks In Advance !!

Advertisement

Answer

It worked after using an additional VideoStream Class that i got from here https://gist.github.com/vluzrmos/d5682ad426525196d069

 public function video($video_id){

        $path = storage_path('app/public/course/videos/'.$video_id);

        if (!File::exists($path)) {
            abort(404);
        }

        $stream = new AppHttpVideoStream($path);

        return response()->stream(function() use ($stream) {
            $stream->start();
        });
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement