@@ -7,10 +7,8 @@ package options
77
88import (
99 "fmt"
10- "io/fs"
1110 "os"
1211 "path"
13- "path/filepath"
1412
1513 "code.gitea.io/gitea/modules/log"
1614 "code.gitea.io/gitea/modules/setting"
@@ -27,76 +25,20 @@ func Dir(name string) ([]string, error) {
2725
2826 var result []string
2927
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 )
48- 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 )
28+ for _ , dir := range []string {
29+ path .Join (setting .CustomPath , "options" , name ), // custom dir
30+ path .Join (setting .StaticRootPath , "options" , name ), // static dir
31+ } {
32+ files , err := statDirIfExist (dir )
5333 if err != nil {
54- return [] string {}, fmt . Errorf ( "Failed to read static directory. %w" , err )
34+ return nil , err
5535 }
56-
5736 result = append (result , files ... )
5837 }
5938
6039 return directories .AddAndGet (name , result ), nil
6140}
6241
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" , path .Clean ("/" + 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" , path .Clean ("/" + 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" , path .Clean ("/" + 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" , path .Clean ("/" + name )))
98- }
99-
10042// fileFromDir is a helper to read files from static or custom path.
10143func fileFromDir (name string ) ([]byte , error ) {
10244 customPath := path .Join (setting .CustomPath , "options" , name )
0 commit comments