@@ -7,36 +7,36 @@ import (
7
7
"fmt"
8
8
"io/fs"
9
9
"os"
10
- "path"
11
10
"path/filepath"
12
11
12
+ "code.gitea.io/gitea/modules/log"
13
13
"code.gitea.io/gitea/modules/setting"
14
14
"code.gitea.io/gitea/modules/util"
15
15
)
16
16
17
17
// Locale reads the content of a specific locale from static/bindata or custom path.
18
18
func Locale (name string ) ([]byte , error ) {
19
- return fileFromDir ( path . Join ( "locale" , util . SafePathRel ( name )) )
19
+ return fileFromOptionsDir ( "locale" , name )
20
20
}
21
21
22
22
// Readme reads the content of a specific readme from static/bindata or custom path.
23
23
func Readme (name string ) ([]byte , error ) {
24
- return fileFromDir ( path . Join ( "readme" , util . SafePathRel ( name )) )
24
+ return fileFromOptionsDir ( "readme" , name )
25
25
}
26
26
27
27
// Gitignore reads the content of a gitignore locale from static/bindata or custom path.
28
28
func Gitignore (name string ) ([]byte , error ) {
29
- return fileFromDir ( path . Join ( "gitignore" , util . SafePathRel ( name )) )
29
+ return fileFromOptionsDir ( "gitignore" , name )
30
30
}
31
31
32
32
// License reads the content of a specific license from static/bindata or custom path.
33
33
func License (name string ) ([]byte , error ) {
34
- return fileFromDir ( path . Join ( "license" , util . SafePathRel ( name )) )
34
+ return fileFromOptionsDir ( "license" , name )
35
35
}
36
36
37
37
// Labels reads the content of a specific labels from static/bindata or custom path.
38
38
func Labels (name string ) ([]byte , error ) {
39
- return fileFromDir ( path . Join ( "label" , util . SafePathRel ( name )) )
39
+ return fileFromOptionsDir ( "label" , name )
40
40
}
41
41
42
42
// WalkLocales reads the content of a specific locale
@@ -93,3 +93,21 @@ func statDirIfExist(dir string) ([]string, error) {
93
93
}
94
94
return files , nil
95
95
}
96
+
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
+ localPathElems [0 ] = dir
104
+ localPath := util .SafeFilePathAbs (localPathElems ... )
105
+ isFile , err := util .IsFile (localPath )
106
+ if err != nil {
107
+ log .Error ("Unable to check if %s is a file. Error: %v" , localPath , err )
108
+ } else if isFile {
109
+ return os .ReadFile (localPath )
110
+ }
111
+ }
112
+ return nil , fmt .Errorf ("asset file does not exist: %v" , elems )
113
+ }
0 commit comments