@@ -11,8 +11,19 @@ import (
1111 "code.gitea.io/gitea/modules/log"
1212 "code.gitea.io/gitea/modules/setting"
1313 "code.gitea.io/gitea/modules/svg"
14+ lru "github.com/hashicorp/golang-lru/v2"
1415)
1516
17+ var fileIconCache * lru.Cache [string , string ]
18+
19+ func init () {
20+ var err error
21+ fileIconCache , err = lru.New [string , string ](1000 )
22+ if err != nil {
23+ log .Fatal ("Failed to create file icon cache: %v" , err )
24+ }
25+ }
26+
1627func getBasicFileIconName (entry * git.TreeEntry ) string {
1728 switch {
1829 case entry .IsLink ():
@@ -35,49 +46,53 @@ func getBasicFileIconName(entry *git.TreeEntry) string {
3546}
3647
3748func getFileIconNames (entry * git.TreeEntry ) []string {
49+ fileName := strings .ToLower (path .Base (entry .Name ()))
50+
3851 if entry .IsDir () {
39- fileName := strings .ToLower (path .Base (entry .Name ()))
40- return []string {fileName , "directory" }
52+ return []string {"folder_" + fileName , "folder" }
4153 }
4254
4355 if entry .IsRegular () {
44- fileName := strings .ToLower (path .Base (entry .Name ()))
4556 ext := strings .ToLower (strings .TrimPrefix (path .Ext (fileName ), "." ))
46- return []string {fileName , ext , "file" }
57+ return []string {"file_" + fileName , "file_" + ext , "file" }
4758 }
4859
4960 return nil
5061}
5162
5263func loadCustomIcon (iconPath string ) (string , error ) {
64+ log .Info ("Loading custom icon from %s" , iconPath )
65+
66+ if icon , ok := fileIconCache .Get (iconPath ); ok {
67+ return icon , nil
68+ }
69+
5370 // Try to load the icon from the filesystem
5471 if _ , err := os .Stat (iconPath ); err != nil {
5572 return "" , err
5673 }
5774
58- // Read the SVG file
5975 iconData , err := os .ReadFile (iconPath )
6076 if err != nil {
6177 return "" , err
6278 }
6379
80+ fileIconCache .Add (iconPath , string (iconData ))
81+
6482 return string (iconData ), nil
6583}
6684
6785// FileIcon returns a custom icon from a folder or the default octicon for displaying files/directories
6886func FileIcon (ctx context.Context , entry * git.TreeEntry ) template.HTML {
69- iconPack , ok := ctx .Value ("icon-pack" ).(string ) // TODO: allow user to select an icon pack from a list
70- iconPack = "demo"
71- ok = true
72-
73- if ok && iconPack != "" {
87+ iconTheme := setting .UI .FileIconTheme
88+ if iconTheme != "" {
7489 iconNames := getFileIconNames (entry )
7590
7691 // Try to load the custom icon
7792 for _ , iconName := range iconNames {
78- iconPath := path .Join (setting .AppDataPath , "icons" , iconPack , iconName + ".svg" )
93+ iconPath := path .Join (setting .AppDataPath , "icons" , iconTheme , iconName + ".svg" )
7994 if icon , err := loadCustomIcon (iconPath ); err == nil {
80- return template . HTML (icon )
95+ return svg . RenderHTMLFromString (icon )
8196 }
8297 }
8398 }
0 commit comments