-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.go
68 lines (54 loc) · 1.46 KB
/
utils.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package media
import (
"path/filepath"
"regexp"
"strings"
"github.com/disintegration/imaging"
"github.com/qor/qor/utils"
)
func GetImageFormat(url string) (*imaging.Format, error) {
formats := map[string]imaging.Format{
".jpg": imaging.JPEG,
".jpeg": imaging.JPEG,
".png": imaging.PNG,
".tif": imaging.TIFF,
".tiff": imaging.TIFF,
".bmp": imaging.BMP,
".gif": imaging.GIF,
}
ext := strings.ToLower(regexp.MustCompile(`(\?.*?$)`).ReplaceAllString(filepath.Ext(url), ""))
if f, ok := formats[ext]; ok {
return &f, nil
}
return nil, imaging.ErrUnsupportedFormat
}
// IsImageFormat check filename is image or not
func IsImageFormat(name string) bool {
_, err := GetImageFormat(name)
return err == nil
}
// IsVideoFormat check filename is video or not
func IsVideoFormat(name string) bool {
formats := []string{".mp4", ".m4p", ".m4v", ".m4v", ".mov", ".mpeg", ".webm", ".avi", ".ogg", ".ogv"}
ext := strings.ToLower(regexp.MustCompile(`(\?.*?$)`).ReplaceAllString(filepath.Ext(name), ""))
for _, format := range formats {
if format == ext {
return true
}
}
return false
}
func IsSVGFormat(name string) bool {
formats := []string{".svg", ".svgz"}
ext := strings.ToLower(regexp.MustCompile(`(\?.*?$)`).ReplaceAllString(filepath.Ext(name), ""))
for _, format := range formats {
if format == ext {
return true
}
}
return false
}
func parseTagOption(str string) *Option {
option := Option(utils.ParseTagOption(str))
return &option
}