3 namespace BookStack\Util;
8 * Helper class to sniff out the mime-type of content resulting in
9 * a mime-type that's relatively safe to serve to a browser.
11 class WebSafeMimeSniffer
16 protected $safeMimes = [
18 'application/octet-stream',
51 * Sniff the mime-type from the given file content while running the result
52 * through an allow-list to ensure a web-safe result.
53 * Takes the content as a reference since the value may be quite large.
55 public function sniff(string &$content): string
57 $fInfo = new finfo(FILEINFO_MIME_TYPE);
58 $mime = $fInfo->buffer($content) ?: 'application/octet-stream';
60 if (in_array($mime, $this->safeMimes)) {
64 [$category] = explode('/', $mime, 2);
65 if ($category === 'text') {
69 return 'application/octet-stream';