From 5e8e3d719611075aa78f976653a805bf9ce43af2 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Fri, 21 Mar 2025 09:24:12 -0700 Subject: [PATCH] Minor refactor to fix static analysis errors --- app/src/Controllers/FileController.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/Controllers/FileController.php b/app/src/Controllers/FileController.php index 6d29d49..c0b9661 100644 --- a/app/src/Controllers/FileController.php +++ b/app/src/Controllers/FileController.php @@ -30,10 +30,7 @@ class FileController } $response = $response->withHeader('Content-Disposition', sprintf('attachment; filename="%s"', $file->getFilename())); - - $response = $response->withHeader('Content-Type', finfo_file( - finfo_open(), (string) $file->getRealPath(), FILEINFO_MIME_TYPE - )); + $response = $response->withHeader('Content-Type', $this->contentType($file)); if ($file->getSize() !== false) { $response = $response->withHeader('Content-Length', (string) $file->getSize()); @@ -43,4 +40,17 @@ class FileController (new StreamFactory)->createStreamFromFile($file->getRealPath()) ); } + + private function contentType(SplFileInfo $file, string $default = 'application/octet-stream'): string + { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + + if ($finfo === false) { + return $default; + } + + $mimeType = finfo_file($finfo, (string) $file->getRealPath(), FILEINFO_MIME_TYPE); + + return $mimeType ? $mimeType : $default; + } }