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