-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
49 lines (38 loc) · 899 Bytes
/
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
package common
import (
"fmt"
"strings"
)
var (
resourcesDirectory string
resourcesMimeTypes = make(map[string]string)
)
func ResourcesDirectory() string {
return resourcesDirectory
}
func ReadResource(filename string) ([]byte, string, error) {
if app.Resources == nil {
return nil, "", fmt.Errorf("resources are not initialized")
}
if resourcesDirectory == "" {
de, _ := app.Resources.ReadDir(".")
if len(de) == 1 {
resourcesDirectory = de[0].Name()
}
}
path := filename
if !strings.HasPrefix(path, resourcesDirectory) {
path = strings.Join([]string{resourcesDirectory, filename}, "/")
}
ba, err := app.Resources.ReadFile(path)
if err != nil {
return nil, "", err
}
mimeType, ok := resourcesMimeTypes[filename]
if !ok {
mt := DetectMimeType(filename, ba)
mimeType = mt.MimeType
resourcesMimeTypes[filename] = mimeType
}
return ba, mimeType, nil
}