-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime.go
38 lines (31 loc) · 794 Bytes
/
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
package mediaprobe
import (
"strings"
"github.com/rakyll/magicmime"
)
func init() {
if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR); err != nil {
panic(err)
}
}
// CalculateMime calculates mime type by magic numbers
// Function uses libmagic bindings using github.com/rakyll/magicmime package.
func (info *Info) CalculateMime() (err error) {
var media string
if info.data != nil {
media, err = magicmime.TypeByBuffer(info.data)
} else {
media, err = magicmime.TypeByFile(info.filename)
}
if err != nil {
return err
}
info.MediaType = "application"
info.MediaSubtype = "octet-stream"
kind := strings.Split(media, "/")
if len(kind) == 2 {
info.MediaType = kind[0]
info.MediaSubtype = kind[1]
}
return nil
}