Files
api/services/nginx/app/file_server.php
T
2026-06-01 23:12:00 +02:00

123 lines
3.9 KiB
PHP

<?php
// Get the file name from the URL
$file = $_SERVER['REQUEST_URI'];
$isPreview = $_GET['preview'] ?? false;
// Remove query string if present
$file = strtok($file, '?');
// Require authentication for direct /files/ access
if (str_contains($file, '/files/')) {
$headers = getallheaders();
$token = $_GET['token'] ?? $_POST['token'] ?? ($headers['Authorization'] ?? null);
if (!empty($token)) {
$token = str_replace('Bearer ', '', $token);
}
if (empty($token) || !(new \classes\authentication())->validate_token($token)) {
header('HTTP/1.1 401 Unauthorized');
echo 'Unauthorized';
exit;
}
}
$isPDF = false;
$isPDFStore = false;
$isAttachment = false;
// Check if the filetype is .pdf
if (preg_match('/\.pdf$/', $file)) {
$isPDF = true;
// Check if the file name contains "temp_file_"
if (!str_contains($file, 'temp_file_')) {
$isPDFStore = true;
} else {
$isAttachment = true;
}
}
// Require a filename
if (empty($file)) {
echo 'No file specified';
exit;
}
// Require an extension
if (!preg_match('/\.[a-zA-Z0-9]+$/', $file)) {
echo 'No file extension specified';
exit;
}
// Check if the file is an attachment
if (str_contains($file, 'temp_file_')) {
$isAttachment = true;
}
if ($isPDF && $isPDFStore) {
$file = str_replace('/modules/washcertificates/output/certificates/', '', $file);
// Download the file from minio, and send it to the client
$wash_certificate_store = new \classes\wash_certificate_store();
// Check if the certificate exists
if (!$wash_certificate_store->isFileInStore($file)) {
echo 'Certificate not found in store' . $file;
//header('HTTP/1.1 404 Not Found');
exit;
}
// Download the certificate from the store to /tmp
$certificate_path = $wash_certificate_store->download($file);
// Send the certificate to the client
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Length: ' . filesize($certificate_path));
readfile($certificate_path);
// Delete the certificate from /tmp after sending it
unlink($certificate_path);
exit;
}
// If the file is an attachment, serve it as a download
if ($isAttachment) {
$attachment_store = new \classes\attachment_store();
$file = str_replace('/files/', '', $file);
// Check if the file exists in the attachment store
if (!$attachment_store->isFileInStore($file)) {
echo 'Attachment '. $file .' not found in store';
exit;
}
// Download the file from the store to /tmp
$file_path = $attachment_store->download($file);
// Send the file to the client
if ($isPreview) {
header('Content-Type: ' . mime_content_type($file_path));
header('Content-Disposition: inline; filename="' . basename($file_path) . '"');
} else {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
}
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
// Delete the file from /tmp after sending it
unlink($file_path);
exit;
}
// Check if the file might be a temporary static file
if (!$isPDF) {
$uploads = new \classes\upload_store();
$file = str_replace('/files/', '', $file);
// Check if the file exists in the upload store
if (!$uploads->isFileInStore($file)) {
echo 'File not found in store';
exit;
}
// Download the file from the store to /tmp
$file_path = $uploads->download($file);
// Send the file to the client
$mime_type = mime_content_type($file_path);
header('Content-Type: ' . $mime_type);
header('Content-Disposition: inline; filename="' . basename($file_path) . '"');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
// Delete the file from /tmp after sending it
unlink($file_path);
exit;
}