/
asset.go
157 lines (141 loc) · 3.88 KB
/
asset.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package assets
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/cozy/cozy-stack/pkg/assets/dynamic"
"github.com/cozy/cozy-stack/pkg/assets/model"
"github.com/cozy/cozy-stack/pkg/assets/statik"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/i18n"
"github.com/cozy/cozy-stack/pkg/logger"
)
// Get looks for an asset. It tries in this order:
// 1. A dynamic asset for the given context
// 2. A dynamic asset for the default context
// 3. A static asset.
func Get(name, context string) (*model.Asset, bool) {
if context == "" {
context = config.DefaultInstanceContext
}
// Check if a dynamic asset is existing
dynAsset, err := dynamic.GetAsset(context, name)
if err == nil {
return dynAsset, true
}
if !errors.Is(err, dynamic.ErrDynAssetNotFound) {
logger.WithNamespace("asset").Errorf("Error while retreiving dynamic asset: %s", err)
}
if context != config.DefaultInstanceContext {
dynAsset, err = dynamic.GetAsset(config.DefaultInstanceContext, name)
if err == nil {
return dynAsset, true
}
}
// If asset was not found, try to retrieve it from static assets
asset := statik.GetAsset(name)
if asset == nil {
return nil, false
}
return asset, true
}
// Head does the same job as Get, but the returned model.Asset can have no body
// data. It allows to use a cache for it.
func Head(name, context string) (*model.Asset, bool) {
if context == "" {
context = config.DefaultInstanceContext
}
key := fmt.Sprintf("dyn-assets:%s/%s", context, name)
cache := config.GetConfig().CacheStorage
if data, ok := cache.Get(key); ok {
asset := &model.Asset{}
if err := json.Unmarshal(data, asset); err == nil {
return asset, true
}
}
asset, ok := Get(name, context)
if !ok {
return nil, false
}
if data, err := json.Marshal(asset); err == nil {
cache.Set(key, data, 24*time.Hour)
}
return asset, true
}
// Add adds dynamic assets
func Add(options []model.AssetOption) error {
err := dynamic.RegisterCustomExternals(options, 0)
if err == nil {
cache := config.GetConfig().CacheStorage
for _, opt := range options {
key := fmt.Sprintf("dyn-assets:%s/%s", opt.Context, opt.Name)
cache.Clear(key)
}
}
return err
}
// Remove removes an asset
// Note: Only dynamic assets can be removed
func Remove(name, context string) error {
err := dynamic.RemoveAsset(context, name)
if err == nil {
key := fmt.Sprintf("dyn-assets:%s/%s", context, name)
cache := config.GetConfig().CacheStorage
cache.Clear(key)
}
return err
}
// List returns a map containing all the existing assets (statik & dynamic)
// Each map key represents the context
func List() (map[string][]*model.Asset, error) {
assetsMap := make(map[string][]*model.Asset)
defctx := config.DefaultInstanceContext
// Get dynamic assets
dynAssets, err := dynamic.ListAssets()
if err != nil {
return nil, err
}
for ctx, assets := range dynAssets {
assetsMap[ctx] = append(assetsMap[ctx], assets...)
}
// Get statik assets
statik.Foreach(func(name string, f *model.Asset) {
for _, asset := range assetsMap[defctx] {
if asset.Name == f.Name {
return
}
}
assetsMap[defctx] = append(assetsMap[defctx], f)
})
return assetsMap, nil
}
// Open returns a bytes.Reader for an asset in the given context, or the
// default context if no context is given.
func Open(name string, context string) (*bytes.Reader, error) {
f, ok := Get(name, context)
if ok {
return f.Reader(), nil
}
return nil, os.ErrNotExist
}
// LoadContextualizedLocale loads the translations dictionary from dynamic
// assets for the given locale and context.
func LoadContextualizedLocale(context, locale string) {
name := "/locales/" + locale + ".po"
asset, ok := Head(name, context)
if !ok || asset.Context != context {
return
}
f, err := Open(name, context)
if err != nil {
return
}
buf, err := io.ReadAll(f)
if err == nil {
i18n.LoadLocale(locale, context, buf)
}
}