-
Notifications
You must be signed in to change notification settings - Fork 35
/
mime.go
218 lines (194 loc) · 4.4 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package gold
import (
"errors"
"fmt"
"mime"
"path/filepath"
"github.com/gabriel-vasile/mimetype"
crdf "github.com/presbrey/goraptor"
"regexp"
"sync"
)
var mimeParser = map[string]string{
"application/ld+json": "jsonld",
"application/json": "internal",
"application/sparql-update": "internal",
}
var mimeSerializer = map[string]string{
"application/ld+json": "internal",
"text/html": "internal",
}
var mimeRdfExt = map[string]string{
".ttl": "text/turtle",
".n3": "text/n3",
".rdf": "application/rdf+xml",
".jsonld": "application/ld+json",
}
var rdfExtensions = []string{
".ttl",
".n3",
".rdf",
".jsonld",
}
var (
serializerMimes = []string{}
validMimeType = regexp.MustCompile(`^\w+/\w+$`)
mutex = &sync.Mutex{}
)
func init() {
// add missing extensions
for k, v := range mimeRdfExt {
mime.AddExtensionType(k, v)
}
for _, syntax := range crdf.ParserSyntax {
switch syntax.MimeType {
case "", "text/html":
continue
}
mimeParser[syntax.MimeType] = syntax.Name
}
mimeParser["text/n3"] = mimeParser["text/turtle"]
for name, syntax := range crdf.SerializerSyntax {
switch name {
case "json-triples":
// only activate: json
continue
case "rdfxml-xmp", "rdfxml":
// only activate: rdfxml-abbrev
continue
}
mimeSerializer[syntax.MimeType] = syntax.Name
}
for mime := range mimeSerializer {
switch mime {
case "application/xhtml+xml":
continue
}
serializerMimes = append(serializerMimes, mime)
}
}
func GuessMimeType(path string) (string, error) {
mimeType := "text/plain"
guessedType, _, err := mimetype.DetectFile(path)
if err != nil {
fmt.Printf("Unknown mimeType from file: %s ==> %s", path, err)
return mimeType, err
}
if guessedType != "" && validMimeType.MatchString(guessedType) {
mimeType = guessedType
}
return mimeType, nil
}
func LookupExt(ctype string) string {
for k, v := range mimeRdfExt {
if v == ctype {
return k
}
}
return ""
}
func LookUpCtype(ext string) string {
return mimeRdfExt[ext]
}
func AddRDFExtension(ext string) {
rdfExtensions = append(rdfExtensions, ext)
}
func IsRdfExtension(ext string) bool {
for _, v := range rdfExtensions {
if v == ext {
return true
}
}
return false
}
func MimeLookup(path string) (string, string, bool) {
var mimeType string
maybeRDF := false
ext := filepath.Ext(path)
if len(ext) > 0 {
if IsRdfExtension(ext) {
maybeRDF = true
mimeType = LookUpCtype(ext)
} else {
mimeType = mime.TypeByExtension(ext)
if len(mimeType) > 0 {
if len(LookupExt(ext)) > 0 {
maybeRDF = true
}
}
}
}
return mimeType, ext, maybeRDF
}
// MapPathToExtension returns the path with the proper extension that matches the given content type,
// even if the resource (path) contains a different extension
// Only works with Go 1.5+
//@@TODO should switch to a more comprehensive list of mime-to-ext (instead of using go's internal list)
func MapPathToExtension(path string, ctype string) (string, error) {
if len(path) == 0 {
return "", errors.New("MapPathToExt -- missing path or ctype value")
}
if path[len(path)-1:] == "/" {
return path, nil
}
fileCType, ext, _ := MimeLookup(path)
if len(fileCType) > 0 {
fileCType, _, _ = mime.ParseMediaType(fileCType)
if len(ctype) > 0 {
if fileCType != ctype {
// append the extension corresponding to Content-Type header
newExt, err := mime.ExtensionsByType(ctype)
if err != nil {
return "", err
}
if len(newExt) > 0 {
ext = newExt[0]
}
path += "$" + ext
}
}
} else {
if len(ext) > 0 {
if len(ctype) > 0 {
newExt, err := mime.ExtensionsByType(ctype)
if err != nil {
return "", err
}
if len(newExt) > 0 {
match := false
for _, e := range newExt {
if e == ext {
match = true
break
}
}
if !match {
// could not find matching extension
if !IsRdfExtension(newExt[0]) {
path += "$" + newExt[0]
}
}
}
}
} else {
// !fileCtype, !ext, ctype
if len(ctype) > 0 {
// maybe it's an RDF resource
if ext = LookupExt(ctype); len(ext) > 0 {
path += ext
} else {
newExt, err := mime.ExtensionsByType(ctype)
if err != nil {
return "", err
}
if len(newExt) > 0 {
path += newExt[0]
}
}
} else {
return "", errors.New("Cannot infer mime type from from empty file")
}
}
}
return path, nil
}