-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.go
123 lines (110 loc) · 3.22 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package data
import (
"context"
"encoding/base64"
"fmt"
"strings"
"github.com/gabriel-vasile/mimetype"
"github.com/instill-ai/pipeline-backend/pkg/data/format"
"github.com/instill-ai/pipeline-backend/pkg/external"
)
func encodeDataURI(b []byte, contentType string) (s string, err error) {
s = fmt.Sprintf("data:%s;base64,%s", contentType, base64.StdEncoding.EncodeToString(b))
return
}
func StandardizePath(path string) (newPath string, err error) {
splits := strings.FieldsFunc(path, func(s rune) bool {
return s == '.' || s == '['
})
for _, split := range splits {
if strings.HasSuffix(split, "]") {
// Array Index
newPath += fmt.Sprintf("[%s", split)
} else {
// Map Key
newPath += fmt.Sprintf("[\"%s\"]", split)
}
}
return newPath, err
}
func NewBinaryFromBytes(b []byte, contentType, filename string) (format.Value, error) {
if contentType == "" {
contentType = strings.Split(mimetype.Detect(b).String(), ";")[0]
}
switch {
case isImageContentType(contentType):
return NewImageFromBytes(b, contentType, filename)
case isAudioContentType(contentType):
return NewAudioFromBytes(b, contentType, filename)
case isVideoContentType(contentType):
return NewVideoFromBytes(b, contentType, filename)
case isDocumentContentType(contentType):
return NewDocumentFromBytes(b, contentType, filename)
default:
return NewFileFromBytes(b, contentType, filename)
}
}
func NewBinaryFromURL(ctx context.Context, binaryFetcher external.BinaryFetcher, url string) (format.Value, error) {
b, contentType, filename, err := binaryFetcher.FetchFromURL(ctx, url)
if err != nil {
return nil, err
}
if contentType == "" {
contentType = strings.Split(mimetype.Detect(b).String(), ";")[0]
}
switch {
case isImageContentType(contentType):
return NewImageFromBytes(b, contentType, filename)
case isAudioContentType(contentType):
return NewAudioFromBytes(b, contentType, filename)
case isVideoContentType(contentType):
return NewVideoFromBytes(b, contentType, filename)
case isDocumentContentType(contentType):
return NewDocumentFromBytes(b, contentType, filename)
default:
return NewFileFromBytes(b, contentType, filename)
}
}
func isImageContentType(contentType string) bool {
return contentType == JPEG ||
contentType == PNG ||
contentType == GIF ||
contentType == BMP ||
contentType == WEBP ||
contentType == TIFF
}
func isAudioContentType(contentType string) bool {
return contentType == AIFF ||
contentType == MP3 ||
contentType == WAV ||
contentType == AAC ||
contentType == OGG ||
contentType == FLAC ||
contentType == M4A ||
contentType == WMA
}
func isVideoContentType(contentType string) bool {
return contentType == MPEG ||
contentType == AVI ||
contentType == MOV ||
contentType == WEBM ||
contentType == MKV ||
contentType == FLV ||
contentType == WMV ||
contentType == MP4
}
func isDocumentContentType(contentType string) bool {
return contentType == DOC ||
contentType == DOCX ||
contentType == PPT ||
contentType == PPTX ||
contentType == XLS ||
contentType == XLSX ||
contentType == HTML ||
contentType == PLAIN ||
contentType == TEXT ||
contentType == MARKDOWN ||
contentType == CSV ||
contentType == PDF ||
strings.HasPrefix(contentType, "text/")
}