add sample http icon backend

This commit is contained in:
Anbraten 2024-10-24 08:37:20 +02:00
parent f11c4cc4c1
commit ca3974b8af
No known key found for this signature in database
GPG Key ID: B1222603899C6B25
3 changed files with 84 additions and 24 deletions

View File

@ -3,7 +3,6 @@ package fileicon
import (
"context"
"html/template"
"os"
"path"
"strings"
@ -72,40 +71,32 @@ func getFileIconNames(entry *git.TreeEntry) []string {
return nil
}
func loadCustomIcon(iconPath string) (string, error) {
log.Debug("Loading custom icon from %s", iconPath)
if icon, ok := fileIconCache.Get(iconPath); ok {
return icon, nil
}
// Try to load the icon from the filesystem
if _, err := os.Stat(iconPath); err != nil {
return "", err
}
iconData, err := os.ReadFile(iconPath)
if err != nil {
return "", err
}
fileIconCache.Add(iconPath, string(iconData))
return string(iconData), nil
type fileIconBackend interface {
GetIcon(string) (string, error)
}
// FileIcon returns a custom icon from a folder or the default octicon for displaying files/directories
func FileIcon(ctx context.Context, entry *git.TreeEntry) template.HTML {
backend := &fileIconHTTPBackend{
theme: setting.UI.FileIconTheme,
baseURL: "https://raw.githubusercontent.com/anbraten/gitea-icons/refs/heads/master/gitea/",
}
iconTheme := setting.UI.FileIconTheme
if iconTheme != "" {
iconNames := getFileIconNames(entry)
// Try to load the custom icon
for _, iconName := range iconNames {
iconPath := path.Join(setting.AppDataPath, "icons", iconTheme, iconName+".svg")
if icon, err := loadCustomIcon(iconPath); err == nil {
if icon, err := backend.GetIcon(iconName); err == nil {
if icon, ok := fileIconCache.Get(iconName); ok {
return svg.RenderHTMLFromString(icon)
}
fileIconCache.Add(iconName, string(icon))
return svg.RenderHTMLFromString(string(icon))
}
}
}

31
modules/fileicon/fs.go Normal file
View File

@ -0,0 +1,31 @@
package fileicon
import (
"os"
"path"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)
type fileIconFolderBackend struct {
theme string
}
func (f *fileIconFolderBackend) GetIcon(iconName string) (string, error) {
iconPath := path.Join(setting.AppDataPath, "icons", f.theme, iconName+".svg")
log.Debug("Loading custom icon from %s", iconPath)
// Try to load the icon from the filesystem
if _, err := os.Stat(iconPath); err != nil {
return "", err
}
iconData, err := os.ReadFile(iconPath)
if err != nil {
return "", err
}
return string(iconData), nil
}

38
modules/fileicon/http.go Normal file
View File

@ -0,0 +1,38 @@
package fileicon
import (
"fmt"
"io"
"net/http"
"path"
"code.gitea.io/gitea/modules/log"
)
type fileIconHTTPBackend struct {
theme string
baseURL string
}
func (f *fileIconHTTPBackend) GetIcon(iconName string) (string, error) {
iconPath := path.Join(f.baseURL, f.theme, iconName+".svg")
log.Info("Loading custom icon from %s", iconPath)
// Try to load the icon via HTTP get
res, err := http.Get(iconPath)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("Failed to load icon: %s", res.Status)
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(resBody), nil
}