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