forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.go
More file actions
127 lines (108 loc) · 3.4 KB
/
Copy pathresource.go
File metadata and controls
127 lines (108 loc) · 3.4 KB
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
package fyne
import (
"io"
"net/http"
"os"
"path/filepath"
)
// Resource represents a single binary resource, such as an image or font.
// A resource has an identifying name and byte array content.
// The serialised path of a resource can be obtained which may result in a
// blocking filesystem write operation.
type Resource interface {
Name() string
Content() []byte
}
// ThemedResource is a version of a resource that can be updated to match a certain theme color.
// The [ThemeColorName] will be used to look up the color for the current theme and colorize the resource.
//
// Since: 2.5
type ThemedResource interface {
Resource
ThemeColorName() ThemeColorName
}
// StaticResource is a bundled resource compiled into the application.
// These resources are normally generated by the fyne_bundle command included in
// the Fyne toolkit.
type StaticResource struct {
StaticName string
StaticContent []byte
}
// Name returns the unique name of this resource, usually matching the file it
// was generated from.
func (r *StaticResource) Name() string {
return r.StaticName
}
// Content returns the bytes of the bundled resource, no compression is applied
// but any compression on the resource is retained.
func (r *StaticResource) Content() []byte {
return r.StaticContent
}
// NewStaticResource returns a new static resource object with the specified
// name and content. Creating a new static resource in memory results in
// sharable binary data that may be serialised to the system cache location.
func NewStaticResource(name string, content []byte) *StaticResource {
return &StaticResource{
StaticName: name,
StaticContent: content,
}
}
// LoadResourceFromPath creates a new [StaticResource] in memory using the contents of the specified file.
func LoadResourceFromPath(path string) (Resource, error) {
bytes, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
name := filepath.Base(path)
return NewStaticResource(name, bytes), nil
}
// LoadResourceFromURLString creates a new [StaticResource] in memory using the body of the specified URL.
func LoadResourceFromURLString(urlStr string) (Resource, error) {
res, err := http.Get(urlStr) //gosec:disable G107 -- applying security measures to the URL is the caller’s responsibility
if err != nil {
return nil, err
}
defer res.Body.Close()
bytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
name := filepath.Base(urlStr)
return NewStaticResource(name, bytes), nil
}
// CacheResourceFromURLString creates a new [StaticResource] in memory using the body of the specified URL.
// It also adds the content to our cache so that future requests for this resource will be available locally
// instead of downloaded from the URL.
//
// Since: 2.8
func CacheResourceFromURLString(urlStr string) (Resource, error) {
cache := CurrentApp().Cache()
if read, err := cache.Read(urlStr); err == nil {
name := filepath.Base(urlStr)
data, err := io.ReadAll(read)
_ = read.Close()
if err != nil {
return nil, err
}
return NewStaticResource(name, data), nil
}
res, err := LoadResourceFromURLString(urlStr)
if err != nil {
return nil, err
}
write, err := cache.Write(urlStr)
if err != nil {
return res, err
}
defer write.Close()
data := res.Content()
n, err := write.Write(data)
for n < len(data) {
if err != nil {
return res, err
}
data = data[n:]
n, err = write.Write(data)
}
return res, nil
}