gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package fileicon
  4. import (
  5. "html/template"
  6. "strings"
  7. "sync"
  8. "code.gitea.io/gitea/modules/json"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/options"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/svg"
  13. "code.gitea.io/gitea/modules/util"
  14. )
  15. type materialIconRulesData struct {
  16. FileNames map[string]string `json:"fileNames"`
  17. FolderNames map[string]string `json:"folderNames"`
  18. FileExtensions map[string]string `json:"fileExtensions"`
  19. LanguageIDs map[string]string `json:"languageIds"`
  20. }
  21. type MaterialIconProvider struct {
  22. once sync.Once
  23. rules *materialIconRulesData
  24. svgs map[string]string
  25. }
  26. var materialIconProvider MaterialIconProvider
  27. func DefaultMaterialIconProvider() *MaterialIconProvider {
  28. materialIconProvider.once.Do(materialIconProvider.loadData)
  29. return &materialIconProvider
  30. }
  31. func (m *MaterialIconProvider) loadData() {
  32. buf, err := options.AssetFS().ReadFile("fileicon/material-icon-rules.json")
  33. if err != nil {
  34. log.Error("Failed to read material icon rules: %v", err)
  35. return
  36. }
  37. err = json.Unmarshal(buf, &m.rules)
  38. if err != nil {
  39. log.Error("Failed to unmarshal material icon rules: %v", err)
  40. return
  41. }
  42. buf, err = options.AssetFS().ReadFile("fileicon/material-icon-svgs.json")
  43. if err != nil {
  44. log.Error("Failed to read material icon rules: %v", err)
  45. return
  46. }
  47. err = json.Unmarshal(buf, &m.svgs)
  48. if err != nil {
  49. log.Error("Failed to unmarshal material icon rules: %v", err)
  50. return
  51. }
  52. log.Debug("Loaded material icon rules and SVG images")
  53. }
  54. func (m *MaterialIconProvider) renderFileIconSVG(p *RenderedIconPool, name, svg, extraClass string) template.HTML {
  55. // This part is a bit hacky, but it works really well. It should be safe to do so because all SVG icons are generated by us.
  56. // Will try to refactor this in the future.
  57. if !strings.HasPrefix(svg, "<svg") {
  58. panic("Invalid SVG icon")
  59. }
  60. svgID := "svg-mfi-" + name
  61. svgCommonAttrs := `class="svg git-entry-icon ` + extraClass + `" width="16" height="16" aria-hidden="true"`
  62. svgHTML := template.HTML(`<svg id="` + svgID + `" ` + svgCommonAttrs + svg[4:])
  63. if p == nil {
  64. return svgHTML
  65. }
  66. if p.IconSVGs[svgID] == "" {
  67. p.IconSVGs[svgID] = svgHTML
  68. }
  69. return template.HTML(`<svg ` + svgCommonAttrs + `><use xlink:href="#` + svgID + `"></use></svg>`)
  70. }
  71. func (m *MaterialIconProvider) EntryIconHTML(p *RenderedIconPool, entry *EntryInfo) template.HTML {
  72. if m.rules == nil {
  73. return BasicEntryIconHTML(entry)
  74. }
  75. if entry.EntryMode.IsLink() {
  76. if entry.SymlinkToMode.IsDir() {
  77. // keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
  78. return svg.RenderHTML("material-folder-symlink", 16, "octicon-file-directory-symlink")
  79. }
  80. return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them
  81. }
  82. name := m.FindIconName(entry)
  83. iconSVG := m.svgs[name]
  84. if iconSVG == "" {
  85. name = "file"
  86. if entry.EntryMode.IsDir() {
  87. name = util.Iif(entry.IsOpen, "folder-open", "folder")
  88. }
  89. iconSVG = m.svgs[name]
  90. if iconSVG == "" {
  91. setting.PanicInDevOrTesting("missing file icon for %s", name)
  92. }
  93. }
  94. // keep the old "octicon-xxx" class name to make some "theme plugin selector" could still work
  95. extraClass := "octicon-file"
  96. switch {
  97. case entry.EntryMode.IsDir():
  98. extraClass = BasicEntryIconName(entry)
  99. case entry.EntryMode.IsSubModule():
  100. extraClass = "octicon-file-submodule"
  101. }
  102. return m.renderFileIconSVG(p, name, iconSVG, extraClass)
  103. }
  104. func (m *MaterialIconProvider) findIconNameWithLangID(s string) string {
  105. if _, ok := m.svgs[s]; ok {
  106. return s
  107. }
  108. if s, ok := m.rules.LanguageIDs[s]; ok {
  109. if _, ok = m.svgs[s]; ok {
  110. return s
  111. }
  112. }
  113. return ""
  114. }
  115. func (m *MaterialIconProvider) FindIconName(entry *EntryInfo) string {
  116. if entry.EntryMode.IsSubModule() {
  117. return "folder-git"
  118. }
  119. fileNameLower := strings.ToLower(entry.BaseName)
  120. if entry.EntryMode.IsDir() {
  121. if s, ok := m.rules.FolderNames[fileNameLower]; ok {
  122. return s
  123. }
  124. return util.Iif(entry.IsOpen, "folder-open", "folder")
  125. }
  126. if s, ok := m.rules.FileNames[fileNameLower]; ok {
  127. if s = m.findIconNameWithLangID(s); s != "" {
  128. return s
  129. }
  130. }
  131. for i := len(fileNameLower) - 1; i >= 0; i-- {
  132. if fileNameLower[i] == '.' {
  133. ext := fileNameLower[i+1:]
  134. if s, ok := m.rules.FileExtensions[ext]; ok {
  135. if s = m.findIconNameWithLangID(s); s != "" {
  136. return s
  137. }
  138. }
  139. }
  140. }
  141. return "file"
  142. }