@@ -14,6 +14,8 @@ import (
14
14
"code.gitea.io/gitea/modules/util"
15
15
)
16
16
17
+ var directories = make (directorySet )
18
+
17
19
// Locale reads the content of a specific locale from static/bindata or custom path.
18
20
func Locale (name string ) ([]byte , error ) {
19
21
return fileFromOptionsDir ("locale" , name )
@@ -79,37 +81,47 @@ func walkAssetDir(root string, callback func(path, name string, d fs.DirEntry, e
79
81
return nil
80
82
}
81
83
82
- func statDirIfExist (dir string ) ([]string , error ) {
83
- isDir , err := util .IsDir (dir )
84
+ // mustLocalPathAbs coverts a path to absolute path
85
+ // FIXME: the old behavior (StaticRootPath might not be absolute), not ideal, just keep the same as before
86
+ func mustLocalPathAbs (s string ) string {
87
+ abs , err := filepath .Abs (s )
84
88
if err != nil {
85
- return nil , fmt . Errorf ( "unable to check if static directory %s is a directory. %w " , dir , err )
89
+ log . Fatal ( "Unable to get absolute path for %q: %v " , s , err )
86
90
}
87
- if ! isDir {
88
- return nil , nil
91
+ return abs
92
+ }
93
+
94
+ func joinLocalPaths (baseDirs []string , subDir string , elems ... string ) (paths []string ) {
95
+ abs := make ([]string , len (elems )+ 2 )
96
+ abs [1 ] = subDir
97
+ copy (abs [2 :], elems )
98
+ for _ , baseDir := range baseDirs {
99
+ abs [0 ] = mustLocalPathAbs (baseDir )
100
+ paths = append (paths , util .SafeFilePathAbs (abs ... ))
89
101
}
90
- files , err := util .StatDir (dir , true )
91
- if err != nil {
92
- return nil , fmt .Errorf ("unable to read directory %q. %w" , dir , err )
102
+ return paths
103
+ }
104
+
105
+ func listLocalDirIfExist (baseDirs []string , subDir string , elems ... string ) (files []string , err error ) {
106
+ for _ , localPath := range joinLocalPaths (baseDirs , subDir , elems ... ) {
107
+ isDir , err := util .IsDir (localPath )
108
+ if err != nil {
109
+ return nil , fmt .Errorf ("unable to check if static directory %s is a directory. %w" , localPath , err )
110
+ } else if ! isDir {
111
+ continue
112
+ }
113
+
114
+ dirFiles , err := util .StatDir (localPath , true )
115
+ if err != nil {
116
+ return nil , fmt .Errorf ("unable to read directory %q. %w" , localPath , err )
117
+ }
118
+ files = append (files , dirFiles ... )
93
119
}
94
120
return files , nil
95
121
}
96
122
97
- func readFileFromLocal (base []string , sub string , elems ... string ) ([]byte , error ) {
98
- localPathElems := make ([]string , len (elems )+ 2 ) // path[0] will be used for the custom path prefix
99
- localPathElems [1 ] = sub
100
- copy (localPathElems [2 :], elems )
101
-
102
- for _ , dir := range base {
103
- if ! filepath .IsAbs (dir ) {
104
- // FIXME: the old behavior (CustomPath or StaticRootPath might not be absolute), not ideal, just keep the same as before
105
- var err error
106
- dir , err = filepath .Abs (dir )
107
- if err != nil {
108
- return nil , fmt .Errorf ("unable to get absolute path for %q. %w" , dir , err )
109
- }
110
- }
111
- localPathElems [0 ] = dir
112
- localPath := util .SafeFilePathAbs (localPathElems ... )
123
+ func readLocalFile (baseDirs []string , subDir string , elems ... string ) ([]byte , error ) {
124
+ for _ , localPath := range joinLocalPaths (baseDirs , subDir , elems ... ) {
113
125
data , err := os .ReadFile (localPath )
114
126
if err == nil {
115
127
return data , nil
0 commit comments