Files
api/services/nginx/app/file_server.php
T
Jeppe Bundgaard 09fbbc11bb Add support for handling /files/ requests and temp file attachments in file_server.php
- Updated `file_server.php` to mark files with `temp_file_` in their name as attachments.
- Enhanced routing logic in `index.php` to load `file_server.php` for `/files/` requests.
2025-09-22 17:29:05 +02:00

97 lines
3.2 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, '?');
$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;
}
}
// 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';
//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;
}