<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;

abstract class Controller
{
    public function imageIfExists(string $path, $cacheLimit = 60 * 60 * 72){
        if(!Storage::disk('local')->exists($path)){
            return response()->json(['error' => 'Image not found'], 404);
        }

        $fullPath = Storage::disk('local')->path($path);
        $lastModified = filemtime($fullPath);

        return response()->file($fullPath, [
            'Content-Type'  => 'image/png',
            'Cache-Control' => 'public, max-age='.$cacheLimit, // 24 hours
            'Last-Modified' => gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
            'ETag'          => md5($path . $lastModified),
        ]);
    }
}
