<?php
namespace App\Controllers;

use Core\Controller;

class DownloadController extends Controller
{
    public function format()
    {
        if (isset($_GET['file'])) {
            $fileName = basename($_GET['file']); // hindari traversal folder
            $filePath = __DIR__ . '/../../public/downloads/' . $fileName;

            if (file_exists($filePath)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                header('Content-Disposition: attachment; filename="' . $fileName . '"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($filePath));
                readfile($filePath);
                exit;
            } else {
                echo "File tidak ditemukan.";
            }
        } else {
            echo "Parameter file tidak valid.";
        }
    }
    
    public function format_excel()
    {
        if (isset($_GET['file'])) {
            $fileName = basename($_GET['file']); // hindari traversal folder
            $filePath = __DIR__ . '/../../public/downloads/' . $fileName;

            if (file_exists($filePath)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                header('Content-Disposition: attachment; filename="' . $fileName . '"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($filePath));
                readfile($filePath);
                exit;
            } else {
                echo "File tidak ditemukan.";
            }
        } else {
            echo "Parameter file tidak valid.";
        }
    }
    
}
