Skip to content

Commit 716bfd5

Browse files
committed
fix bindata build, fix svg assets loading
1 parent bbfef58 commit 716bfd5

File tree

8 files changed

+55
-73
lines changed

8 files changed

+55
-73
lines changed

modules/public/public.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ func StaticAssets() *assetfs.Layer {
3636
return assetfs.Local("static", setting.StaticRootPath, "public")
3737
}
3838

39-
// AssetsHandlerFunc implements the static handler for serving custom or original assets.
40-
func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
41-
var assetFS http.FileSystem
39+
func AssetFS() *assetfs.LayeredFS {
4240
if setting.HasBuiltinBindata {
43-
assetFS = assetfs.Layered(CustomAssets(), StaticAssets(), BuiltinAssets()) // old behavior: always include StaticAssets
41+
return assetfs.Layered(CustomAssets(), StaticAssets(), BuiltinAssets()) // old behavior: always include StaticAssets
4442
} else {
45-
assetFS = assetfs.Layered(CustomAssets(), BuiltinAssets()) // now BuiltinAssets is StaticAssets
43+
return assetfs.Layered(CustomAssets(), BuiltinAssets()) // now BuiltinAssets is StaticAssets
4644
}
45+
}
46+
47+
// AssetsHandlerFunc implements the static handler for serving custom or original assets.
48+
func AssetsHandlerFunc(opts *Options) http.HandlerFunc {
49+
assetFS := AssetFS()
4750

4851
return func(resp http.ResponseWriter, req *http.Request) {
4952
path := req.URL.Path

modules/public/serve_static.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@
55

66
package public
77

8+
import (
9+
"time"
10+
11+
"code.gitea.io/gitea/modules/assetfs"
12+
"code.gitea.io/gitea/modules/timeutil"
13+
)
14+
815
var _ GzipBytesProvider = (*vfsgen۰CompressedFileInfo)(nil)
916

17+
// GlobalModTime provide a global mod time for embedded asset files
18+
func GlobalModTime(filename string) time.Time {
19+
return timeutil.GetExecutableModTime()
20+
}
21+
1022
func BuiltinAssets() *assetfs.Layer {
1123
return assetfs.Bindata("builtin(bindata)", Assets)
1224
}

modules/svg/discover_bindata.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

modules/svg/discover_nobindata.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

modules/svg/svg.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ package svg
66
import (
77
"fmt"
88
"html/template"
9+
"path"
910
"regexp"
1011
"strings"
1112

1213
"code.gitea.io/gitea/modules/html"
14+
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/public"
1316
)
1417

1518
var (
1619
// SVGs contains discovered SVGs
17-
SVGs map[string]string
20+
SVGs = map[string]string{}
1821

1922
widthRe = regexp.MustCompile(`width="[0-9]+?"`)
2023
heightRe = regexp.MustCompile(`height="[0-9]+?"`)
@@ -23,17 +26,29 @@ var (
2326
const defaultSize = 16
2427

2528
// Init discovers SVGs and populates the `SVGs` variable
26-
func Init() {
27-
SVGs = Discover()
29+
func Init() error {
30+
files, err := public.AssetFS().ListFiles("img/svg")
31+
if err != nil {
32+
return err
33+
}
2834

2935
// Remove `xmlns` because inline SVG does not need it
30-
r := regexp.MustCompile(`(<svg\b[^>]*?)\s+xmlns="[^"]*"`)
31-
for name, svg := range SVGs {
32-
SVGs[name] = r.ReplaceAllString(svg, "$1")
36+
reXmlns := regexp.MustCompile(`(<svg\b[^>]*?)\s+xmlns="[^"]*"`)
37+
for _, file := range files {
38+
if path.Ext(file) != ".svg" {
39+
continue
40+
}
41+
bs, err := public.AssetFS().ReadFile("img/svg", file)
42+
if err != nil {
43+
log.Error("Failed to read SVG file %s: %v", file, err)
44+
} else {
45+
SVGs[file[:len(file)-4]] = reXmlns.ReplaceAllString(string(bs), "$1")
46+
}
3347
}
48+
return nil
3449
}
3550

36-
// Render render icons - arguments icon name (string), size (int), class (string)
51+
// RenderHTML renders icons - arguments icon name (string), size (int), class (string)
3752
func RenderHTML(icon string, others ...interface{}) template.HTML {
3853
size, class := html.ParseSizeAndClass(defaultSize, "", others...)
3954

modules/templates/static.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55

66
package templates
77

8+
import (
9+
"time"
10+
11+
"code.gitea.io/gitea/modules/assetfs"
12+
"code.gitea.io/gitea/modules/timeutil"
13+
)
14+
15+
// GlobalModTime provide a global mod time for embedded asset files
16+
func GlobalModTime(filename string) time.Time {
17+
return timeutil.GetExecutableModTime()
18+
}
19+
820
func BuiltinAssets() *assetfs.Layer {
921
return assetfs.Bindata("builtin(bindata)", Assets)
1022
}

routers/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func GlobalInitInstalled(ctx context.Context) {
172172
mustInit(ssh.Init)
173173

174174
auth.Init()
175-
svg.Init()
175+
mustInit(svg.Init)
176176

177177
actions_service.Init()
178178

routers/install/setting.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ func reloadSettings(ctx context.Context) {
4747
} else {
4848
log.Fatal("ORM engine initialization failed: %v", err)
4949
}
50-
svg.Init()
5150
}
5251
}

0 commit comments

Comments
 (0)