Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Jan 4, 2020
1 parent 7f785a0 commit 1fe33ab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
23 changes: 13 additions & 10 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
### Detect
Get the MIME type from a slice of bytes, from a reader and from a file.
```go
// Detect the MIME type of a file stored as a byte slice.
file := "testdata/pdf.pdf"
reader, _ := os.Open(file) // ignoring error for brevity's sake
data, _ := ioutil.ReadFile(file) // ignoring error for brevity's sake

dmime := mimetype.Detect(data)
rmime, rerr := mimetype.DetectReader(reader)
fmime, ferr := mimetype.DetectFile(file)
// Detect the MIME type of a file.
mime, ferr := mimetype.DetectFile(file)
fmt.Println(mime, ferr)
// Output: application/pdf nil

fmt.Println(dmime, rmime, fmime)
fmt.Println(rerr, ferr)
// Detect the MIME type of a reader.
reader, _ := os.Open(file) // ignoring error for brevity's sake
mime, rerr := mimetype.DetectReader(reader)
fmt.Println(mime, rerr)
// Output: application/pdf nil

// Output: application/pdf application/pdf application/pdf
// <nil> <nil>
mime := mimetype.Detect(data)
fmt.Println(mime)
// Output: application/pdf
```

### Check
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
A package for detecting MIME types and extensions based on magic numbers
</h4>
<h6 align="center">
No bindings, thread safe and completely written in go
No C bindings, zero dependencies and thread safe
</h6>

<p align="center">
Expand Down
22 changes: 14 additions & 8 deletions example_mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ import (
// mimetype.DetectFile(string) (*MIME, error)
func Example_detect() {
file := "testdata/pdf.pdf"
reader, _ := os.Open(file) // ignoring error for brevity's sake

// Detect the MIME type of a file stored as a byte slice.
data, _ := ioutil.ReadFile(file) // ignoring error for brevity's sake
mime := mimetype.Detect(data)
fmt.Println(mime)

dmime := mimetype.Detect(data)
rmime, rerr := mimetype.DetectReader(reader)
fmime, ferr := mimetype.DetectFile(file)
// Detect the MIME type of a reader.
reader, _ := os.Open(file) // ignoring error for brevity's sake
mime, rerr := mimetype.DetectReader(reader)
fmt.Println(mime, rerr)

fmt.Println(dmime, rmime, fmime)
fmt.Println(rerr, ferr)
// Detect the MIME type of a file.
mime, ferr := mimetype.DetectFile(file)
fmt.Println(mime, ferr)

// Output: application/pdf application/pdf application/pdf
// <nil> <nil>
// Output: application/pdf
// application/pdf <nil>
// application/pdf <nil>
}

// To check if some bytes/reader/file has a specific MIME type, first perform
Expand Down
4 changes: 2 additions & 2 deletions mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// Detect returns the MIME type found from the provided byte slice.
//
// Failure to identify the format results in application/octet-stream
// being returned.
// The result is always a valid MIME type, with application/octet-stream
// returned when identification failed.
func Detect(in []byte) (mime *MIME) {
if len(in) == 0 {
return newMIME("inode/x-empty", "", matchers.True)
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/gabriel-vasile/mimetype/internal/matchers"

// root is a matcher which passes for any slice of bytes.
// When a matcher passes the check, the children matchers
// are tried in order to find a more accurate mime type.
// are tried in order to find a more accurate MIME type.
var root = newMIME("application/octet-stream", "", matchers.True,
sevenZ, zip, pdf, ole, ps, psd, ogg, png, jpg, jp2, jpx, jpm, gif, webp,
exe, elf, ar, tar, xar, bz2, fits, tiff, bmp, ico, mp3, flac, midi, ape,
Expand Down

0 comments on commit 1fe33ab

Please sign in to comment.