-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.go
96 lines (78 loc) · 1.73 KB
/
uri.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
package repository
import (
"bufio"
"mime"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/danielbaenabird/fyne/v2"
)
// Declare conformance with fyne.URI interface.
var _ fyne.URI = &uri{}
type uri struct {
scheme string
authority string
// haveAuthority lets us distinguish between a present-but-empty
// authority, and having no authority. This is needed because net/url
// incorrectly handles scheme:/absolute/path URIs.
haveAuthority bool
path string
query string
fragment string
}
func (u *uri) Extension() string {
return filepath.Ext(u.path)
}
func (u *uri) Name() string {
return filepath.Base(u.path)
}
func (u *uri) MimeType() string {
mimeTypeFull := mime.TypeByExtension(u.Extension())
if mimeTypeFull == "" {
mimeTypeFull = "text/plain"
repo, err := ForURI(u)
if err != nil {
return "application/octet-stream"
}
readCloser, err := repo.Reader(u)
if err == nil {
defer readCloser.Close()
scanner := bufio.NewScanner(readCloser)
if scanner.Scan() && !utf8.Valid(scanner.Bytes()) {
mimeTypeFull = "application/octet-stream"
}
}
}
return strings.Split(mimeTypeFull, ";")[0]
}
func (u *uri) Scheme() string {
return u.scheme
}
func (u *uri) String() string {
// NOTE: this string reconstruction is mandated by IETF RFC3986,
// section 5.3, pp. 35.
s := u.scheme + ":"
if u.haveAuthority {
s += "//" + u.authority
}
s += u.path
if len(u.query) > 0 {
s += "?" + u.query
}
if len(u.fragment) > 0 {
s += "#" + u.fragment
}
return s
}
func (u *uri) Authority() string {
return u.authority
}
func (u *uri) Path() string {
return u.path
}
func (u *uri) Query() string {
return u.query
}
func (u *uri) Fragment() string {
return u.fragment
}