-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'bd2126e954a85806526338c75c8084a52b50f7dd'
- Loading branch information
Showing
16 changed files
with
200 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
dune exec --no-print-directory ./mimetype.exe -- $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
let () = | ||
let fname = Sys.argv.(1) in | ||
let mime = try Metadata.MIME.of_file fname with Not_found -> "unknown" in | ||
print_endline mime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,3 @@ | ||
open MetadataBase | ||
module R = Reader | ||
let parse = MetadataRIFF.parse ~format:"AVI " | ||
|
||
(* Tag normalization. *) | ||
let tagn = | ||
[ | ||
("IART", "artist"); | ||
("ICMT", "comment"); | ||
("ICOP", "copyright"); | ||
("ICRD", "date"); | ||
("ICRD", "date"); | ||
("IGNR", "genre"); | ||
("INAM", "title"); | ||
("IPRD", "album"); | ||
("IPRT", "track"); | ||
("ISFT", "encoder"); | ||
] | ||
|
||
let parse f : metadata = | ||
if R.read f 4 <> "RIFF" then raise Invalid; | ||
let _ (* file size *) = R.int32_le f in | ||
if R.read f 4 <> "AVI " then raise Invalid; | ||
let ans = ref [] in | ||
let chunk () = | ||
let tag = R.read f 4 in | ||
let size = R.int32_le f in | ||
if tag <> "LIST" then R.drop f size | ||
else ( | ||
let subtag = R.read f 4 in | ||
match subtag with | ||
| "INFO" -> | ||
let remaining = ref (size - 4) in | ||
while !remaining > 0 do | ||
let tag = R.read f 4 in | ||
let size = R.int32_le f in | ||
match R.read_tag ~length:(size - 1) ~label:tag f with | ||
| None -> () | ||
| Some s -> | ||
R.drop f 1; | ||
(* null-terminated *) | ||
let padding = size mod 2 in | ||
R.drop f padding; | ||
remaining := !remaining - (8 + size + padding); | ||
let tag = | ||
match List.assoc_opt tag tagn with | ||
| Some tag -> tag | ||
| None -> tag | ||
in | ||
ans := (tag, s) :: !ans | ||
done | ||
| "movi" -> raise Exit (* stop parsing there *) | ||
| _ -> R.drop f (size - 4)) | ||
in | ||
try | ||
while true do | ||
chunk () | ||
done; | ||
assert false | ||
with _ -> List.rev !ans | ||
|
||
let parse_file ?custom_parser file = R.with_file ?custom_parser parse file | ||
let parse_file = MetadataRIFF.parse_file ~format:"AVI " |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
open MetadataBase | ||
module R = Reader | ||
|
||
(* Tag normalization. *) | ||
let tagn = | ||
[ | ||
("IART", "artist"); | ||
("ICMT", "comment"); | ||
("ICOP", "copyright"); | ||
("ICRD", "date"); | ||
("ICRD", "date"); | ||
("IGNR", "genre"); | ||
("INAM", "title"); | ||
("IPRD", "album"); | ||
("IPRT", "track"); | ||
("ISFT", "encoder"); | ||
("ITRK", "track"); | ||
] | ||
|
||
let parse ?format f : metadata = | ||
if R.read f 4 <> "RIFF" then raise Invalid; | ||
let _ (* file size *) = R.int32_le f in | ||
if format <> None && Some (R.read f 4) <> format then raise Invalid; | ||
let ans = ref [] in | ||
let chunk () = | ||
let tag = R.read f 4 in | ||
let size = R.int32_le f in | ||
if tag <> "LIST" then R.drop f size | ||
else ( | ||
let subtag = R.read f 4 in | ||
match subtag with | ||
| "INFO" -> | ||
let remaining = ref (size - 4) in | ||
while !remaining > 0 do | ||
let tag = R.read f 4 in | ||
let size = R.int32_le f in | ||
match R.read_tag ~length:(size - 1) ~label:tag f with | ||
| None -> () | ||
| Some s -> | ||
R.drop f 1; | ||
(* null-terminated *) | ||
let padding = size mod 2 in | ||
R.drop f padding; | ||
remaining := !remaining - (8 + size + padding); | ||
let tag = | ||
match List.assoc_opt tag tagn with | ||
| Some tag -> tag | ||
| None -> tag | ||
in | ||
ans := (tag, s) :: !ans | ||
done | ||
| "movi" -> raise Exit (* stop parsing there *) | ||
| _ -> R.drop f (size - 4)) | ||
in | ||
try | ||
while true do | ||
chunk () | ||
done; | ||
assert false | ||
with _ -> List.rev !ans | ||
|
||
let parse_file ?format ?custom_parser file = R.with_file ?custom_parser (parse ?format) file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
val parse : ?format:string -> MetadataBase.Reader.t -> MetadataBase.metadata | ||
|
||
val parse_file : ?format:string -> ?custom_parser:MetadataBase.custom_parser -> string -> MetadataBase.metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let parse = MetadataRIFF.parse ~format:"WAVE" | ||
|
||
let parse_file = MetadataRIFF.parse_file ~format:"WAVE" |