-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.go
More file actions
61 lines (52 loc) · 1.34 KB
/
resources.go
File metadata and controls
61 lines (52 loc) · 1.34 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
package lnksworks
import (
"io"
"sync"
)
//EmbededResource struct representing embeded resource
type EmbededResource struct {
contentRS *IORW
}
var embededRes map[string]*IORW
var embededResLock *sync.RWMutex
func init() {
embededResLock = &sync.RWMutex{}
if embededRes == nil {
embededRes = make(map[string]*IORW)
}
}
//EmbededResourceByPath return EmbededResource by path
//note return resio *IORW
func EmbededResourceByPath(respath string) (resio *IORW) {
embededResLock.RLock()
defer embededResLock.RUnlock()
if emres, embok := embededRes[respath]; embok {
resio = emres
}
return resio
}
//RegisterEmbededResources RegisterEmbededResources
//Usage
// RegisterEmbededResources(string,io.Reader,string,io.Reader)
func RegisterEmbededResources(resources ...interface{}) {
if len(resources) > 0 && len(resources)%2 == 0 {
resi := 0
embededResLock.RLock()
defer embededResLock.RUnlock()
for resi < len(resources) {
if respath, respathok := resources[resi].(string); respathok {
if resstream, resstreamok := resources[resi+1].(io.Reader); resstreamok {
if _, resok := embededRes[respath]; resok {
embededRes[respath].Close()
embededRes[respath] = nil
delete(embededRes, respath)
}
iores, _ := NewIORW()
iores.Print(resstream)
embededRes[respath] = iores
}
}
resi = resi + 2
}
}
}