getMimeType($realPath); // Create BinaryFileResponse for efficient file serving $response = new BinaryFileResponse($realPath); $response->headers->set('Content-Type', $mimeType); $response->setContentDisposition( ResponseHeaderBag::DISPOSITION_INLINE, $filename ); // Add cache headers (cache for 30 days) $response->setPublic(); $response->setMaxAge(2592000); $response->setSharedMaxAge(2592000); return $response; } /** * Get MIME type of a file */ private function getMimeType(string $path): string { $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); $mimes = [ 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'pdf' => 'application/pdf', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'txt' => 'text/plain', 'zip' => 'application/zip', ]; if (isset($mimes[$extension])) { return $mimes[$extension]; } if (function_exists('mime_content_type')) { return mime_content_type($path); } return 'application/octet-stream'; } }