forked from wabarc/ipfs-pinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.go
52 lines (46 loc) · 1.07 KB
/
mime.go
1
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
package file
import (
"bufio"
"io"
"net/http"
"os"
// "github.com/gabriel-vasile/mimetype"
)
// MediaType returns the file's mime type. If the mime type cannot be
// determined, it returns "application/octet-stream".
//
// The i should be a *os.File, io.Reader, or byte slice.
func MediaType(i interface{}) string {
defaultType := "application/octet-stream"
// var err error
// var mtype *mimetype.MIME
// switch v := i.(type) {
// case *os.File:
// mtype, err = mimetype.DetectFile(v.Name())
// case io.Reader:
// mtype, err = mimetype.DetectReader(v)
// case []byte:
// mtype = mimetype.Detect(v)
// default:
// return defaultType
// }
// if err != nil {
// return defaultType
// }
// return mtype.String()
switch v := i.(type) {
case *os.File:
scanner := bufio.NewScanner(v)
return http.DetectContentType(scanner.Bytes())
case io.Reader:
rd := bufio.NewReader(v)
buf, err := rd.Peek(8192)
if err != nil {
return defaultType
}
return http.DetectContentType(buf)
case []byte:
return http.DetectContentType(v)
}
return defaultType
}