Search code examples
laravelimagecachingdropbox

Laravel image cache using Dropbox as disk


I'm using this package (https://github.com/spatie/flysystem-dropbox) to storage and get images from Dropbox.

This works fine but images have to load everytime the page is refreshed. I wonder if you know any image cache solution that works in this case and if can please provide with a minimal working example.

Thanks.


Solution

  • One way to solve this would be to create your own caching system. If the image doesn't exist on your local file system, pull it from Dropbox and then save it to the local file system and serve it. If it already exists in the local file system just serve it from the local file system.

    1 Route

    Serve the images from their own route.

    Route::get('images/{filename}', [
        'uses'    => 'ImageController@getImage'
    ]);
    

    2 Controller

    Check the local file system to see if the file already exists, otherwise pull it from dropbox and store it in the local file system.

    <?php 
    
    namespace App\Http\Controllers;
    
    class ImageController extends Controller 
    
        public function __construct()
        {
            parent::__construct();
        }
    
        public function getImage($filename)
        {
            // If the file doesn't exist
            if(!file_exists('/path/to/' . $filename)) {
    
                // 1. Get the image from dropbox
    
                // 2. Save the image to local storage
            }
    
            // 3. Serve the image from local storage
        }
    }