<?php

namespace App\Controllers;

class ImageController
{
    public function serveNewsImage($filename)
    {
        // Bersihkan nama file untuk mencegah directory traversal
        $safeFilename = basename($filename);

        // Path absolut ke folder penyimpanan gambar di public/assets/img/news
        $path = __DIR__ . '/../../public/assets/img/news/' . $safeFilename;

        // Cek apakah file benar-benar ada
        if (!file_exists($path)) {
            http_response_code(404);
            echo "File not found";
            return;
        }

        // Dapatkan tipe MIME file
        $mime = mime_content_type($path);

        // Kirim header tipe konten
        header("Content-Type: $mime");

        // Kirim isi file
        readfile($path);
    }
}
