|
6 | 6 | package options
|
7 | 7 |
|
8 | 8 | import (
|
9 |
| - "fmt" |
10 |
| - "io/fs" |
11 |
| - "os" |
12 |
| - "path" |
13 |
| - "path/filepath" |
14 |
| - |
15 |
| - "code.gitea.io/gitea/modules/log" |
16 | 9 | "code.gitea.io/gitea/modules/setting"
|
17 |
| - "code.gitea.io/gitea/modules/util" |
18 | 10 | )
|
19 | 11 |
|
20 |
| -var directories = make(directorySet) |
21 |
| - |
22 | 12 | // Dir returns all files from static or custom directory.
|
23 | 13 | func Dir(name string) ([]string, error) {
|
24 | 14 | if directories.Filled(name) {
|
25 | 15 | return directories.Get(name), nil
|
26 | 16 | }
|
27 | 17 |
|
28 |
| - var result []string |
29 |
| - |
30 |
| - customDir := path.Join(setting.CustomPath, "options", name) |
31 |
| - |
32 |
| - isDir, err := util.IsDir(customDir) |
33 |
| - if err != nil { |
34 |
| - return []string{}, fmt.Errorf("Unabe to check if custom directory %s is a directory. %w", customDir, err) |
35 |
| - } |
36 |
| - if isDir { |
37 |
| - files, err := util.StatDir(customDir, true) |
38 |
| - if err != nil { |
39 |
| - return []string{}, fmt.Errorf("Failed to read custom directory. %w", err) |
40 |
| - } |
41 |
| - |
42 |
| - result = append(result, files...) |
43 |
| - } |
44 |
| - |
45 |
| - staticDir := path.Join(setting.StaticRootPath, "options", name) |
46 |
| - |
47 |
| - isDir, err = util.IsDir(staticDir) |
| 18 | + result, err := listLocalDirIfExist([]string{setting.CustomPath, setting.StaticRootPath}, "options", name) |
48 | 19 | if err != nil {
|
49 |
| - return []string{}, fmt.Errorf("unable to check if static directory %s is a directory. %w", staticDir, err) |
50 |
| - } |
51 |
| - if isDir { |
52 |
| - files, err := util.StatDir(staticDir, true) |
53 |
| - if err != nil { |
54 |
| - return []string{}, fmt.Errorf("Failed to read static directory. %w", err) |
55 |
| - } |
56 |
| - |
57 |
| - result = append(result, files...) |
| 20 | + return nil, err |
58 | 21 | }
|
59 | 22 |
|
60 | 23 | return directories.AddAndGet(name, result), nil
|
61 | 24 | }
|
62 | 25 |
|
63 |
| -// Locale reads the content of a specific locale from static or custom path. |
64 |
| -func Locale(name string) ([]byte, error) { |
65 |
| - return fileFromDir(path.Join("locale", name)) |
66 |
| -} |
67 |
| - |
68 |
| -// WalkLocales reads the content of a specific locale from static or custom path. |
69 |
| -func WalkLocales(callback func(path, name string, d fs.DirEntry, err error) error) error { |
70 |
| - if err := walkAssetDir(filepath.Join(setting.StaticRootPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { |
71 |
| - return fmt.Errorf("failed to walk locales. Error: %w", err) |
72 |
| - } |
73 |
| - |
74 |
| - if err := walkAssetDir(filepath.Join(setting.CustomPath, "options", "locale"), callback); err != nil && !os.IsNotExist(err) { |
75 |
| - return fmt.Errorf("failed to walk locales. Error: %w", err) |
76 |
| - } |
77 |
| - return nil |
78 |
| -} |
79 |
| - |
80 |
| -// Readme reads the content of a specific readme from static or custom path. |
81 |
| -func Readme(name string) ([]byte, error) { |
82 |
| - return fileFromDir(path.Join("readme", name)) |
83 |
| -} |
84 |
| - |
85 |
| -// Gitignore reads the content of a specific gitignore from static or custom path. |
86 |
| -func Gitignore(name string) ([]byte, error) { |
87 |
| - return fileFromDir(path.Join("gitignore", name)) |
88 |
| -} |
89 |
| - |
90 |
| -// License reads the content of a specific license from static or custom path. |
91 |
| -func License(name string) ([]byte, error) { |
92 |
| - return fileFromDir(path.Join("license", name)) |
93 |
| -} |
94 |
| - |
95 |
| -// Labels reads the content of a specific labels from static or custom path. |
96 |
| -func Labels(name string) ([]byte, error) { |
97 |
| - return fileFromDir(path.Join("label", name)) |
98 |
| -} |
99 |
| - |
100 |
| -// fileFromDir is a helper to read files from static or custom path. |
101 |
| -func fileFromDir(name string) ([]byte, error) { |
102 |
| - customPath := path.Join(setting.CustomPath, "options", name) |
103 |
| - |
104 |
| - isFile, err := util.IsFile(customPath) |
105 |
| - if err != nil { |
106 |
| - log.Error("Unable to check if %s is a file. Error: %v", customPath, err) |
107 |
| - } |
108 |
| - if isFile { |
109 |
| - return os.ReadFile(customPath) |
110 |
| - } |
111 |
| - |
112 |
| - staticPath := path.Join(setting.StaticRootPath, "options", name) |
113 |
| - |
114 |
| - isFile, err = util.IsFile(staticPath) |
115 |
| - if err != nil { |
116 |
| - log.Error("Unable to check if %s is a file. Error: %v", staticPath, err) |
117 |
| - } |
118 |
| - if isFile { |
119 |
| - return os.ReadFile(staticPath) |
120 |
| - } |
121 |
| - |
122 |
| - return []byte{}, fmt.Errorf("Asset file does not exist: %s", name) |
| 26 | +// fileFromOptionsDir is a helper to read files from custom or static path. |
| 27 | +func fileFromOptionsDir(elems ...string) ([]byte, error) { |
| 28 | + return readLocalFile([]string{setting.CustomPath, setting.StaticRootPath}, "options", elems...) |
123 | 29 | }
|
124 | 30 |
|
125 | 31 | // IsDynamic will return false when using embedded data (-tags bindata)
|
|
0 commit comments