|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package fileicon |
| 5 | + |
| 6 | +import ( |
| 7 | + "html/template" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/git" |
| 13 | + "code.gitea.io/gitea/modules/json" |
| 14 | + "code.gitea.io/gitea/modules/log" |
| 15 | + "code.gitea.io/gitea/modules/options" |
| 16 | + "code.gitea.io/gitea/modules/reqctx" |
| 17 | + "code.gitea.io/gitea/modules/svg" |
| 18 | +) |
| 19 | + |
| 20 | +type materialIconRulesData struct { |
| 21 | + IconDefinitions map[string]*struct { |
| 22 | + IconPath string `json:"iconPath"` |
| 23 | + } `json:"iconDefinitions"` |
| 24 | + FileNames map[string]string `json:"fileNames"` |
| 25 | + FolderNames map[string]string `json:"folderNames"` |
| 26 | + FileExtensions map[string]string `json:"fileExtensions"` |
| 27 | + LanguageIDs map[string]string `json:"languageIds"` |
| 28 | +} |
| 29 | + |
| 30 | +type MaterialIconProvider struct { |
| 31 | + once sync.Once |
| 32 | + rules *materialIconRulesData |
| 33 | + svgs map[string]string |
| 34 | +} |
| 35 | + |
| 36 | +var materialIconProvider MaterialIconProvider |
| 37 | + |
| 38 | +func DefaultMaterialIconProvider() *MaterialIconProvider { |
| 39 | + return &materialIconProvider |
| 40 | +} |
| 41 | + |
| 42 | +func (m *MaterialIconProvider) loadData() { |
| 43 | + buf, err := options.AssetFS().ReadFile("fileicon/material-icon-rules.json") |
| 44 | + if err != nil { |
| 45 | + log.Error("Failed to read material icon rules: %v", err) |
| 46 | + return |
| 47 | + } |
| 48 | + err = json.Unmarshal(buf, &m.rules) |
| 49 | + if err != nil { |
| 50 | + log.Error("Failed to unmarshal material icon rules: %v", err) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + buf, err = options.AssetFS().ReadFile("fileicon/material-icon-svgs.json") |
| 55 | + if err != nil { |
| 56 | + log.Error("Failed to read material icon rules: %v", err) |
| 57 | + return |
| 58 | + } |
| 59 | + err = json.Unmarshal(buf, &m.svgs) |
| 60 | + if err != nil { |
| 61 | + log.Error("Failed to unmarshal material icon rules: %v", err) |
| 62 | + return |
| 63 | + } |
| 64 | + log.Debug("Loaded material icon rules and SVG images") |
| 65 | +} |
| 66 | + |
| 67 | +func (m *MaterialIconProvider) renderFileIconSVG(ctx reqctx.RequestContext, name, svg string) template.HTML { |
| 68 | + data := ctx.GetData() |
| 69 | + renderedSVGs, _ := data["_RenderedSVGs"].(map[string]bool) |
| 70 | + if renderedSVGs == nil { |
| 71 | + renderedSVGs = make(map[string]bool) |
| 72 | + data["_RenderedSVGs"] = renderedSVGs |
| 73 | + } |
| 74 | + // 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. |
| 75 | + // Will try to refactor this in the future. |
| 76 | + if !strings.HasPrefix(svg, "<svg") { |
| 77 | + panic("Invalid SVG icon") |
| 78 | + } |
| 79 | + svgID := "svg-mfi-" + name |
| 80 | + svgCommonAttrs := `class="svg fileicon" width="16" height="16" aria-hidden="true"` |
| 81 | + posOuterBefore := strings.IndexByte(svg, '>') |
| 82 | + if renderedSVGs[svgID] && posOuterBefore != -1 { |
| 83 | + return template.HTML(`<svg ` + svgCommonAttrs + `><use xlink:href="#` + svgID + `"></use></svg>`) |
| 84 | + } |
| 85 | + svg = `<svg id="` + svgID + `" ` + svgCommonAttrs + svg[4:] |
| 86 | + renderedSVGs[svgID] = true |
| 87 | + return template.HTML(svg) |
| 88 | +} |
| 89 | + |
| 90 | +func (m *MaterialIconProvider) FileIcon(ctx reqctx.RequestContext, entry *git.TreeEntry) template.HTML { |
| 91 | + m.once.Do(m.loadData) |
| 92 | + |
| 93 | + if m.rules == nil { |
| 94 | + return BasicThemeIcon(entry) |
| 95 | + } |
| 96 | + |
| 97 | + if entry.IsLink() { |
| 98 | + if te, err := entry.FollowLink(); err == nil && te.IsDir() { |
| 99 | + return svg.RenderHTML("material-folder-symlink") |
| 100 | + } |
| 101 | + return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them |
| 102 | + } |
| 103 | + |
| 104 | + name := m.findIconName(entry) |
| 105 | + if name == "folder" { |
| 106 | + // the material icon pack's "folder" icon doesn't look good, so use our built-in one |
| 107 | + return svg.RenderHTML("material-folder-generic") |
| 108 | + } |
| 109 | + if iconSVG, ok := m.svgs[name]; ok && iconSVG != "" { |
| 110 | + return m.renderFileIconSVG(ctx, name, iconSVG) |
| 111 | + } |
| 112 | + return svg.RenderHTML("octicon-file") |
| 113 | +} |
| 114 | + |
| 115 | +func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string { |
| 116 | + if entry.IsSubModule() { |
| 117 | + return "folder-git" |
| 118 | + } |
| 119 | + |
| 120 | + iconsData := m.rules |
| 121 | + fileName := path.Base(entry.Name()) |
| 122 | + |
| 123 | + if entry.IsDir() { |
| 124 | + if s, ok := iconsData.FolderNames[fileName]; ok { |
| 125 | + return s |
| 126 | + } |
| 127 | + if s, ok := iconsData.FolderNames[strings.ToLower(fileName)]; ok { |
| 128 | + return s |
| 129 | + } |
| 130 | + return "folder" |
| 131 | + } |
| 132 | + |
| 133 | + if s, ok := iconsData.FileNames[fileName]; ok { |
| 134 | + return s |
| 135 | + } |
| 136 | + if s, ok := iconsData.FileNames[strings.ToLower(fileName)]; ok { |
| 137 | + return s |
| 138 | + } |
| 139 | + |
| 140 | + for i := len(fileName) - 1; i >= 0; i-- { |
| 141 | + if fileName[i] == '.' { |
| 142 | + ext := fileName[i+1:] |
| 143 | + if s, ok := iconsData.FileExtensions[ext]; ok { |
| 144 | + return s |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return "file" |
| 150 | +} |
0 commit comments