Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
metadataMIME.ml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64(** Guess the mime-type of a file. *) module String = struct include String let contains_at offset ~substring s = let n = String.length substring in if String.length s < offset + n then false else String.sub s offset n = substring end let prefixes = [ "ID3", "audio/mpeg"; "OggS", "audio/ogg"; "%PDF-", "application/pdf"; "\137PNG\013\010\026\010", "image/png"; ] let advanced = let wav s = String.starts_with ~prefix:"RIFF" s && String.contains_at 8 ~substring:"WAVEfmt " s in let avi s = String.starts_with ~prefix:"RIFF" s && String.contains_at 8 ~substring:"AVI " s in [ wav, "audio/wav"; avi, "video/x-msvideo" ] let of_string s = let ans = ref "" in try List.iter (fun (f, mime) -> if f s then ( ans := mime; raise Exit ) ) advanced; List.iter (fun (prefix, mime) -> if String.starts_with ~prefix s then ( ans := mime; raise Exit ) ) prefixes; raise Not_found with | Exit -> !ans let of_file fname = let len = 16 in let buf = Bytes.create len in let ic = open_in fname in let n = input ic buf 0 len in let buf = if n = len then buf else Bytes.sub buf 0 n in let s = Bytes.unsafe_to_string buf in of_string s