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',
43 * Sniff the mime-type from the given file content while running the result
44 * through an allow-list to ensure a web-safe result.
45 * Takes the content as a reference since the value may be quite large.
47 public function sniff(string &$content): string
49 $fInfo = new finfo(FILEINFO_MIME_TYPE);
50 $mime = $fInfo->buffer($content) ?: 'application/octet-stream';
52 if (in_array($mime, $this->safeMimes)) {
56 [$category] = explode('/', $mime, 2);
57 if ($category === 'text') {
61 return 'application/octet-stream';