togethere.cloud/public_html/api/admin_task_file.php

69 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/admin_bootstrap.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/file_api_client.php';
$pdo = admin_get_pdo();
admin_require_auth($pdo);
$fileId = isset($_GET['file_id']) ? (int)$_GET['file_id'] : 0;
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($fileId <= 0 && $id <= 0) {
http_response_code(400);
header('Content-Type: text/plain; charset=utf-8');
echo 'Nieprawidłowe ID pliku';
exit;
}
try {
$row = null;
if ($fileId > 0) {
$stmt = $pdo->prepare(
'SELECT file_name, file_mime, file_size, file_path '
. 'FROM admin_task_files WHERE id = :id LIMIT 1'
);
$stmt->execute([':id' => $fileId]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
$stmt = $pdo->prepare(
'SELECT file_name, file_mime, file_size, file_path '
. 'FROM admin_tasks WHERE id = :id LIMIT 1'
);
$stmt->execute([':id' => $id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
}
$hasFilePath = !empty($row['file_path']);
if (!$row || !$hasFilePath) {
http_response_code(404);
header('Content-Type: text/plain; charset=utf-8');
echo 'Brak pliku';
exit;
}
if ($hasFilePath) {
$storedName = basename((string)$row['file_path']);
$subfolder = dirname((string)$row['file_path']);
try {
$fileApi = get_file_api_client();
$fileApi->proxyFile($subfolder, $storedName, false);
} catch (RuntimeException $e) {
$code = (int)$e->getCode();
http_response_code($code === 404 ? 404 : 500);
header('Content-Type: text/plain; charset=utf-8');
echo 'Błąd pobierania pliku: ' . $e->getMessage();
}
exit;
}
} catch (Throwable $e) {
http_response_code(500);
header('Content-Type: text/plain; charset=utf-8');
echo 'Błąd pobierania pliku';
exit;
}