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