/
resource.go
60 lines (46 loc) · 1.07 KB
/
resource.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
package common
import (
"fmt"
"sync"
)
var (
resourcesDirectory string
resourcesMimeTypes = make(map[string]string)
resourcesLock sync.Mutex
)
func ResourcesDirectory() string {
if resourcesDirectory == "" {
entries, _ := app.Resources.ReadDir(".")
for _, entry := range entries {
if entry.IsDir() {
resourcesDirectory = entry.Name()
return resourcesDirectory
}
}
}
return resourcesDirectory
}
func ReadResource(filename string) ([]byte, string, error) {
if app == nil || app.Resources == nil {
return nil, "", fmt.Errorf("resources are not initialized")
}
ba, err := app.Resources.ReadFile(filename)
if err != nil {
ba, err = app.Resources.ReadFile(fmt.Sprintf("%s/%s", ResourcesDirectory(), filename))
if err != nil {
return nil, "", err
}
}
resourcesLock.Lock()
defer resourcesLock.Unlock()
mimeType, ok := resourcesMimeTypes[filename]
if !ok {
mt, err := DetectMimeType(filename, ba)
if err != nil {
return nil, "", err
}
mimeType = mt.MimeType
resourcesMimeTypes[filename] = mimeType
}
return ba, mimeType, nil
}