-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
45 lines (40 loc) · 1.2 KB
/
id.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
package mediatype
import (
"path/filepath"
"strings"
"github.com/h2non/filetype"
)
// ID identifies media into a known mediatype format. If useSignature is true,
// then the existing file extension is ignored and we use the file magic
// signature instead
// REF: https://en.wikipedia.org/wiki/File_format#Magic_number
func ID(path string, useSignature bool) (Format, error) {
ext := strings.TrimPrefix(filepath.Ext(strings.ToLower(path)), ".")
if useSignature {
t, err := filetype.MatchFile(path)
if err != nil {
return Format{}, err
}
ext = t.Extension
}
switch {
case contains(JPEG{}.Aliases(), ext):
return NewJPEGFormat(JPEG{Path: path}), nil
case contains(PNG{}.Aliases(), ext):
return NewPNGFormat(PNG{Path: path}), nil
case contains(HEIF{}.Aliases(), ext):
return NewHEIFFormat(HEIF{Path: path}), nil
case contains(TIFF{}.Aliases(), ext):
return NewTIFFFormat(TIFF{Path: path}), nil
case contains(QTFF{}.Aliases(), ext):
return NewQTFFFormat(QTFF{Path: path}), nil
case contains(MP4{}.Aliases(), ext):
return NewMP4Format(MP4{Path: path}), nil
default:
return Format{}, nil
}
}
func contains(toMatch map[string]struct{}, s string) bool {
_, ok := toMatch[s]
return ok
}