Skip to content

Commit ca3974b

Browse files
committed
add sample http icon backend
1 parent f11c4cc commit ca3974b

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

modules/fileicon/fileicon.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package fileicon
33
import (
44
"context"
55
"html/template"
6-
"os"
76
"path"
87
"strings"
98

@@ -72,39 +71,31 @@ func getFileIconNames(entry *git.TreeEntry) []string {
7271
return nil
7372
}
7473

75-
func loadCustomIcon(iconPath string) (string, error) {
76-
log.Debug("Loading custom icon from %s", iconPath)
77-
78-
if icon, ok := fileIconCache.Get(iconPath); ok {
79-
return icon, nil
80-
}
81-
82-
// Try to load the icon from the filesystem
83-
if _, err := os.Stat(iconPath); err != nil {
84-
return "", err
85-
}
86-
87-
iconData, err := os.ReadFile(iconPath)
88-
if err != nil {
89-
return "", err
90-
}
91-
92-
fileIconCache.Add(iconPath, string(iconData))
93-
94-
return string(iconData), nil
74+
type fileIconBackend interface {
75+
GetIcon(string) (string, error)
9576
}
9677

9778
// FileIcon returns a custom icon from a folder or the default octicon for displaying files/directories
9879
func FileIcon(ctx context.Context, entry *git.TreeEntry) template.HTML {
80+
backend := &fileIconHTTPBackend{
81+
theme: setting.UI.FileIconTheme,
82+
baseURL: "https://raw.githubusercontent.com/anbraten/gitea-icons/refs/heads/master/gitea/",
83+
}
84+
9985
iconTheme := setting.UI.FileIconTheme
10086
if iconTheme != "" {
10187
iconNames := getFileIconNames(entry)
10288

10389
// Try to load the custom icon
10490
for _, iconName := range iconNames {
105-
iconPath := path.Join(setting.AppDataPath, "icons", iconTheme, iconName+".svg")
106-
if icon, err := loadCustomIcon(iconPath); err == nil {
107-
return svg.RenderHTMLFromString(icon)
91+
if icon, err := backend.GetIcon(iconName); err == nil {
92+
if icon, ok := fileIconCache.Get(iconName); ok {
93+
return svg.RenderHTMLFromString(icon)
94+
}
95+
96+
fileIconCache.Add(iconName, string(icon))
97+
98+
return svg.RenderHTMLFromString(string(icon))
10899
}
109100
}
110101
}

modules/fileicon/fs.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fileicon
2+
3+
import (
4+
"os"
5+
"path"
6+
7+
"code.gitea.io/gitea/modules/log"
8+
"code.gitea.io/gitea/modules/setting"
9+
)
10+
11+
type fileIconFolderBackend struct {
12+
theme string
13+
}
14+
15+
func (f *fileIconFolderBackend) GetIcon(iconName string) (string, error) {
16+
iconPath := path.Join(setting.AppDataPath, "icons", f.theme, iconName+".svg")
17+
18+
log.Debug("Loading custom icon from %s", iconPath)
19+
20+
// Try to load the icon from the filesystem
21+
if _, err := os.Stat(iconPath); err != nil {
22+
return "", err
23+
}
24+
25+
iconData, err := os.ReadFile(iconPath)
26+
if err != nil {
27+
return "", err
28+
}
29+
30+
return string(iconData), nil
31+
}

modules/fileicon/http.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package fileicon
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"path"
8+
9+
"code.gitea.io/gitea/modules/log"
10+
)
11+
12+
type fileIconHTTPBackend struct {
13+
theme string
14+
baseURL string
15+
}
16+
17+
func (f *fileIconHTTPBackend) GetIcon(iconName string) (string, error) {
18+
iconPath := path.Join(f.baseURL, f.theme, iconName+".svg")
19+
20+
log.Info("Loading custom icon from %s", iconPath)
21+
22+
// Try to load the icon via HTTP get
23+
res, err := http.Get(iconPath)
24+
if err != nil {
25+
return "", err
26+
}
27+
28+
if res.StatusCode != http.StatusOK {
29+
return "", fmt.Errorf("Failed to load icon: %s", res.Status)
30+
}
31+
32+
resBody, err := io.ReadAll(res.Body)
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
return string(resBody), nil
38+
}

0 commit comments

Comments
 (0)