|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package fileicon |
| 5 | + |
| 6 | +import ( |
| 7 | + "archive/tar" |
| 8 | + "compress/gzip" |
| 9 | + "context" |
| 10 | + "html/template" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "path" |
| 14 | + "strings" |
| 15 | + "sync" |
| 16 | + "time" |
| 17 | + |
| 18 | + "code.gitea.io/gitea/modules/git" |
| 19 | + "code.gitea.io/gitea/modules/json" |
| 20 | + "code.gitea.io/gitea/modules/log" |
| 21 | + "code.gitea.io/gitea/modules/options" |
| 22 | + "code.gitea.io/gitea/modules/svg" |
| 23 | + "code.gitea.io/gitea/modules/util" |
| 24 | +) |
| 25 | + |
| 26 | +type materialIconsData struct { |
| 27 | + IconDefinitions map[string]*struct { |
| 28 | + IconPath string `json:"iconPath"` |
| 29 | + IconContent string `json:"-"` |
| 30 | + } `json:"iconDefinitions"` |
| 31 | + FileNames map[string]string `json:"fileNames"` |
| 32 | + FolderNames map[string]string `json:"folderNames"` |
| 33 | + FileExtensions map[string]string `json:"fileExtensions"` |
| 34 | + LanguageIds map[string]string `json:"languageIds"` |
| 35 | +} |
| 36 | + |
| 37 | +type MaterialIconProvider struct { |
| 38 | + mu sync.RWMutex |
| 39 | + |
| 40 | + fs http.FileSystem |
| 41 | + packFile string |
| 42 | + packFileTime time.Time |
| 43 | + lastStatTime time.Time |
| 44 | + reloadInterval time.Duration |
| 45 | + |
| 46 | + materialIcons *materialIconsData |
| 47 | +} |
| 48 | + |
| 49 | +var ( |
| 50 | + materialIconProvider *MaterialIconProvider |
| 51 | + materialIconProviderOnce sync.Once |
| 52 | +) |
| 53 | + |
| 54 | +func DefaultMaterialIconProvider() *MaterialIconProvider { |
| 55 | + materialIconProviderOnce.Do(func() { |
| 56 | + materialIconProvider = NewMaterialIconProvider(options.AssetFS(), "fileicon/material.tgz") |
| 57 | + }) |
| 58 | + return materialIconProvider |
| 59 | +} |
| 60 | + |
| 61 | +func NewMaterialIconProvider(fs http.FileSystem, packFile string) *MaterialIconProvider { |
| 62 | + return &MaterialIconProvider{fs: fs, packFile: packFile, reloadInterval: time.Second} |
| 63 | +} |
| 64 | + |
| 65 | +func (m *MaterialIconProvider) preprocessSvgContent(s string) string { |
| 66 | + if !strings.HasPrefix(s, "<svg") { |
| 67 | + return s |
| 68 | + } |
| 69 | + return `<svg class="svg svg-extpack-material" width="16" height="16" ` + s[4:] |
| 70 | +} |
| 71 | + |
| 72 | +func (m *MaterialIconProvider) loadDataFromPack(pack http.File) (*materialIconsData, error) { |
| 73 | + gzf, err := gzip.NewReader(pack) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + files := map[string][]byte{} |
| 79 | + tarReader := tar.NewReader(gzf) |
| 80 | + for { |
| 81 | + header, err := tarReader.Next() |
| 82 | + if err == io.EOF { |
| 83 | + break |
| 84 | + } else if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + files[util.PathJoinRelX(header.Name)], err = io.ReadAll(tarReader) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + iconsData := materialIconsData{} |
| 94 | + err = json.Unmarshal(files["package/dist/material-icons.json"], &iconsData) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + for name, icon := range iconsData.IconDefinitions { |
| 100 | + iconContent := string(files[path.Join("package/dist", icon.IconPath)]) |
| 101 | + iconsData.IconDefinitions[name].IconContent = m.preprocessSvgContent(iconContent) |
| 102 | + } |
| 103 | + |
| 104 | + return &iconsData, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (m *MaterialIconProvider) loadData() { |
| 108 | + m.mu.Lock() |
| 109 | + defer m.mu.Unlock() |
| 110 | + if time.Since(m.lastStatTime) > m.reloadInterval { |
| 111 | + m.lastStatTime = time.Now() |
| 112 | + |
| 113 | + f, err := m.fs.Open(m.packFile) |
| 114 | + if err != nil { |
| 115 | + log.Error("Failed to open material icon pack file: %v", err) |
| 116 | + return |
| 117 | + } |
| 118 | + defer f.Close() |
| 119 | + |
| 120 | + fileInfo, err := f.Stat() |
| 121 | + if err != nil { |
| 122 | + log.Error("Failed to stat material icon pack file: %v", err) |
| 123 | + return |
| 124 | + } |
| 125 | + if fileInfo.ModTime().Equal(m.packFileTime) { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + iconsData, err := m.loadDataFromPack(f) |
| 130 | + if err != nil { |
| 131 | + log.Error("Failed to load material icon pack file: %v", err) |
| 132 | + return |
| 133 | + } |
| 134 | + m.materialIcons = iconsData |
| 135 | + m.packFileTime = fileInfo.ModTime() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func (m *MaterialIconProvider) FileIcon(ctx context.Context, entry *git.TreeEntry) template.HTML { |
| 140 | + m.mu.RLock() |
| 141 | + if time.Since(m.lastStatTime) > m.reloadInterval { |
| 142 | + m.mu.RUnlock() |
| 143 | + m.loadData() |
| 144 | + m.mu.RLock() |
| 145 | + } |
| 146 | + defer m.mu.RUnlock() |
| 147 | + |
| 148 | + if m.materialIcons == nil { |
| 149 | + return fileIconBasic(ctx, entry) |
| 150 | + } |
| 151 | + |
| 152 | + if entry.IsLink() { |
| 153 | + if te, err := entry.FollowLink(); err == nil && te.IsDir() { |
| 154 | + return svg.RenderHTML("octicon-file-directory-symlink") // TODO: find some better icons for them |
| 155 | + } |
| 156 | + return svg.RenderHTML("octicon-file-symlink-file") // TODO: find some better icons for them |
| 157 | + } |
| 158 | + |
| 159 | + name := m.findIconName(entry) |
| 160 | + if iconDef, ok := m.materialIcons.IconDefinitions[name]; ok && iconDef.IconContent != "" { |
| 161 | + return template.HTML(iconDef.IconContent) |
| 162 | + } |
| 163 | + return svg.RenderHTML("octicon-file") |
| 164 | +} |
| 165 | + |
| 166 | +func (m *MaterialIconProvider) findIconName(entry *git.TreeEntry) string { |
| 167 | + if entry.IsSubModule() { |
| 168 | + return "folder-git" |
| 169 | + } |
| 170 | + |
| 171 | + iconsData := m.materialIcons |
| 172 | + fileName := path.Base(entry.Name()) |
| 173 | + |
| 174 | + if entry.IsDir() { |
| 175 | + if s, ok := iconsData.FolderNames[fileName]; ok { |
| 176 | + return s |
| 177 | + } |
| 178 | + if s, ok := iconsData.FolderNames[strings.ToLower(fileName)]; ok { |
| 179 | + return s |
| 180 | + } |
| 181 | + return "folder" |
| 182 | + } |
| 183 | + |
| 184 | + if s, ok := iconsData.FileNames[fileName]; ok { |
| 185 | + return s |
| 186 | + } |
| 187 | + if s, ok := iconsData.FileNames[strings.ToLower(fileName)]; ok { |
| 188 | + return s |
| 189 | + } |
| 190 | + |
| 191 | + for i := len(fileName) - 1; i >= 0; i-- { |
| 192 | + if fileName[i] == '.' { |
| 193 | + ext := fileName[i+1:] |
| 194 | + if s, ok := iconsData.FileExtensions[ext]; ok { |
| 195 | + return s |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return "file" |
| 201 | +} |
0 commit comments