<?php
require_once __DIR__ . '/common.php';
$f = $_GET['f'] ?? '';
if (empty($f) || preg_match('/[^a-zA-Z0-9._-]/', $f)) {
    http_response_code(404);
    exit;
}
$path = $UPLOADS_DIR . '/' . $f;
if (!file_exists($path) || !is_file($path)) {
    http_response_code(404);
    exit;
}
$cfg = loadConfig();
$landing = $cfg['landing'] ?? [];
$pathInfo = pathinfo($path);
$stem = $pathInfo['filename'];
$ext = isset($pathInfo['extension']) ? '.' . $pathInfo['extension'] : '';
$vSuffix = 'V' . random_int(1000, 99999);
$cfgName = $landing['update_filename'] ?? $landing['download_filename'] ?? ($stem . $ext);
$baseName = pathinfo($cfgName, PATHINFO_FILENAME) ?: $stem;
$baseExt = pathinfo($cfgName, PATHINFO_EXTENSION);
$baseExt = $baseExt ? '.' . $baseExt : $ext;
$downloadName = $baseName . '_' . $vSuffix . $baseExt;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $downloadName . '"');
header('Content-Length: ' . filesize($path));
readfile($path);
