Created
June 11, 2017 01:01
-
-
Save richardlehane/07b95162f8c4ed1b6857e93c00e4307d to your computer and use it in GitHub Desktop.
Getting MIME from results
This file contains hidden or 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
| /* | |
| Sorry Steffen, documentation of the siegfried package needs a lot of attention! | |
| Also, I've pretty much just focussed on sf tool's needs without making the package very ergonomic/ easy to use in other contexts. | |
| The reason there is no method to directly access a MIME type is that I've tried to make Identifications as abstract as possible. | |
| I.e. even though the three supported Identifiers (PRONOM, LOC and MIMEInfo) all have a MIME field, it may in future be desirable | |
| to add another Identifier type that doesn't have this information (though I admit that is unlikely!). | |
| The interfaces for Identifier (a set of signatures) and Identification (a result returned by an Identifier) are both defined in | |
| the pkg/core package: http://godoc.org/github.com/richardlehane/siegfried/pkg/core | |
| You can see the available methods for an Identification here: http://godoc.org/github.com/richardlehane/siegfried/pkg/core#Identification | |
| In order to access MIME types you need to use that .Values() method - the MIME type will be in the slice returned there. | |
| OK, so now the question is... how do you know what element of that slice is the MIME type ahead of time? You can programmatically get | |
| that information by using the .Fields() method of the relevant Identifier. The slice returned by this method has field names in the same | |
| order as the .Values() slice. | |
| There is a helper method in the siegfried package called .Label() which will automatically pair field names and values together. | |
| This method returns slices like [][2]string{{"ns","pronom"},{"id","fmt/123"},{"mime","text/plain}} | |
| Alternatively, if you know exactly what Identifier you'll be using - then you can directly access the right element of the .Values() | |
| slice by using the right index or (more safely) by doing a type assertion to a concrete Identification type like pronom.Identification. | |
| Examples of these three approaches below. | |
| */ | |
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "os" | |
| "github.com/richardlehane/siegfried" | |
| "github.com/richardlehane/siegfried/pkg/pronom" | |
| ) | |
| func main() { | |
| s, err := siegfried.Load("/Users/richardlehane/siegfried/default.sig") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| f, err := os.Open("sample/README") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer f.Close() | |
| ids, err := s.Identify(f, "README", "") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // Label approach - which is Identifier agnostic. | |
| for _, id := range ids { | |
| for _, kv := range s.Label(id) { | |
| switch kv[0] { | |
| case "mime", "MIME", "MIMEType": // could probably just use "mime" as I think that is the label all three Identifiers use for this field | |
| fmt.Println(kv[1]) | |
| } | |
| } | |
| } | |
| // The Label() approach has advantage of being Identifier agnostic, but is a little slow and cumbersome. | |
| // If we know ahead of time exactly what Identifier is used then we can access the MIME field directly | |
| // by just using the right index. This saves that inner loop with the Label() method. | |
| // E.g. looking at the implementation for the PRONOM identifier https://github.com/richardlehane/siegfried/blob/master/pkg/pronom/identifier.go#L390 | |
| // we can discern the MIME is at index 4. | |
| for _, id := range ids { | |
| fmt.Println(id.Values()[4]) | |
| } | |
| // This is of course a little unsafe! Nicer to do a type assertion to the pronom.Identification type. This concrete type actually has an exported Mime value! | |
| // http://godoc.org/github.com/richardlehane/siegfried/pkg/pronom#Identification | |
| // (I probably should have named that MIME though - more proper :()) | |
| for _, id := range ids { | |
| pid, ok := id.(pronom.Identification) | |
| if ok { | |
| fmt.Println(pid.Mime) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment