mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-04 08:17:24 -05:00
add sample http icon backend
This commit is contained in:
parent
f11c4cc4c1
commit
ca3974b8af
@ -3,7 +3,6 @@ package fileicon
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
@ -72,39 +71,31 @@ 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 {
|
||||
return svg.RenderHTMLFromString(icon)
|
||||
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
31
modules/fileicon/fs.go
Normal 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
38
modules/fileicon/http.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user