<?php
// debug_uploads.php
// Place this file in: backend/uploads/gallery/ and open it in your browser.
// It will list files in this directory, show their MIME types, sizes and whether the public URL returns HTTP 200.

$baseUrlPrefix = '/stile-lpg/backend/uploads/gallery/';
$dir = __DIR__;
$files = array_values(array_diff(scandir($dir), ['.','..','debug_uploads.php']));

?><!doctype html>
<html>
<head><meta charset="utf-8"><title>Gallery Uploads Debug</title>
<style>body{font-family:system-ui,Segoe UI,Roboto,Arial;background:#f8fafc;color:#0f172a;padding:20px}table{width:100%;border-collapse:collapse}th,td{padding:8px;border:1px solid #e6edf3;text-align:left}th{background:#0f172a;color:#fff}</style>
</head>
<body>
<h1>Gallery Uploads Debug</h1>
<p>Directory: <code><?php echo htmlspecialchars($dir) ?></code></p>
<?php if(empty($files)): ?>
  <p><strong>No files found in this folder.</strong></p>
<?php else: ?>
  <table>
    <thead><tr><th>Filename</th><th>Exists</th><th>Size</th><th>MIME</th><th>Public URL</th><th>HTTP Status</th></tr></thead>
    <tbody>
<?php
foreach($files as $f) {
    $path = $dir . DIRECTORY_SEPARATOR . $f;
    $exists = file_exists($path);
    $size = $exists ? filesize($path) : 0;
    $mime = '';
    if ($exists && function_exists('finfo_open')) {
        $fi = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($fi, $path);
        finfo_close($fi);
    }
    $publicUrl = $baseUrlPrefix . rawurlencode($f);
    $httpStatus = 'N/A';
    if (filter_var('http://example.org', FILTER_VALIDATE_URL)) {
        // attempt to get headers for the public url
        $fullUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME']) . $publicUrl;
        $headers = @get_headers($fullUrl, 1);
        if ($headers && isset($headers[0])) $httpStatus = $headers[0];
    }
    echo "<tr>\n";
    echo "<td>" . htmlspecialchars($f) . "</td>\n";
    echo "<td>" . ($exists ? '<span style=\"color:green;font-weight:700\">Yes</span>' : '<span style=\"color:red;font-weight:700\">No</span>') . "</td>\n";
    echo "<td>" . ($exists ? number_format($size) . ' bytes' : '-') . "</td>\n";
    echo "<td>" . htmlspecialchars($mime ?: '-') . "</td>\n";
    echo "<td><a href='" . htmlspecialchars($publicUrl) . "' target='_blank'>" . htmlspecialchars($publicUrl) . "</a></td>\n";
    echo "<td>" . htmlspecialchars($httpStatus) . "</td>\n";
    echo "</tr>\n";
}
?>
    </tbody>
  </table>
<?php endif; ?>

<hr>
<h3>Quick conversion (server required)</h3>
<p>If files exist but laptops can't play them, convert to H.264 MP4 using <code>ffmpeg</code> on the server:</p>
<pre>ffmpeg -i input.mov -c:v libx264 -preset fast -crf 23 -c:a aac -b:a 128k output.mp4</pre>
<p>Contact your host or enable ffmpeg on the server to run this automatically on upload.</p>
</body>
</html>
