Minor refactor to fix static analysis errors

This commit is contained in:
Chris Kankiewicz
2025-03-21 09:24:12 -07:00
parent 33075eaa84
commit 5e8e3d7196

View File

@@ -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;
}
}