0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-24 15:12:48 +01:00

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 ( import (
"context" "context"
"html/template" "html/template"
"os"
"path" "path"
"strings" "strings"
@ -72,40 +71,32 @@ func getFileIconNames(entry *git.TreeEntry) []string {
return nil return nil
} }
func loadCustomIcon(iconPath string) (string, error) { type fileIconBackend interface {
log.Debug("Loading custom icon from %s", iconPath) GetIcon(string) (string, error)
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
} }
// FileIcon returns a custom icon from a folder or the default octicon for displaying files/directories // 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 { 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 iconTheme := setting.UI.FileIconTheme
if iconTheme != "" { if iconTheme != "" {
iconNames := getFileIconNames(entry) iconNames := getFileIconNames(entry)
// Try to load the custom icon // Try to load the custom icon
for _, iconName := range iconNames { for _, iconName := range iconNames {
iconPath := path.Join(setting.AppDataPath, "icons", iconTheme, iconName+".svg") if icon, err := backend.GetIcon(iconName); err == nil {
if icon, err := loadCustomIcon(iconPath); err == nil { if icon, ok := fileIconCache.Get(iconName); ok {
return svg.RenderHTMLFromString(icon) 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
}