@@ -19,81 +19,80 @@ import (
19
19
"code.gitea.io/gitea/modules/web/routing"
20
20
)
21
21
22
- func storageHandler (storageSetting * setting.Storage , prefix string , objStore storage.ObjectStorage ) func ( next http.Handler ) http. Handler {
22
+ func storageHandler (storageSetting * setting.Storage , prefix string , objStore storage.ObjectStorage ) http.HandlerFunc {
23
23
prefix = strings .Trim (prefix , "/" )
24
24
funcInfo := routing .GetFuncInfo (storageHandler , prefix )
25
- return func (next http.Handler ) http.Handler {
26
- if storageSetting .MinioConfig .ServeDirect {
27
- return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
28
- if req .Method != "GET" && req .Method != "HEAD" {
29
- next .ServeHTTP (w , req )
30
- return
31
- }
32
-
33
- if ! strings .HasPrefix (req .URL .Path , "/" + prefix + "/" ) {
34
- next .ServeHTTP (w , req )
35
- return
36
- }
37
- routing .UpdateFuncInfo (req .Context (), funcInfo )
38
-
39
- rPath := strings .TrimPrefix (req .URL .Path , "/" + prefix + "/" )
40
- rPath = util .PathJoinRelX (rPath )
41
-
42
- u , err := objStore .URL (rPath , path .Base (rPath ))
43
- if err != nil {
44
- if os .IsNotExist (err ) || errors .Is (err , os .ErrNotExist ) {
45
- log .Warn ("Unable to find %s %s" , prefix , rPath )
46
- http .Error (w , "file not found" , http .StatusNotFound )
47
- return
48
- }
49
- log .Error ("Error whilst getting URL for %s %s. Error: %v" , prefix , rPath , err )
50
- http .Error (w , fmt .Sprintf ("Error whilst getting URL for %s %s" , prefix , rPath ), http .StatusInternalServerError )
51
- return
52
- }
53
-
54
- http .Redirect (w , req , u .String (), http .StatusTemporaryRedirect )
55
- })
56
- }
57
25
26
+ if storageSetting .MinioConfig .ServeDirect {
58
27
return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
59
28
if req .Method != "GET" && req .Method != "HEAD" {
60
- next . ServeHTTP (w , req )
29
+ http . Error (w , http . StatusText ( http . StatusMethodNotAllowed ), http . StatusMethodNotAllowed )
61
30
return
62
31
}
63
32
64
33
if ! strings .HasPrefix (req .URL .Path , "/" + prefix + "/" ) {
65
- next . ServeHTTP (w , req )
34
+ http . Error (w , http . StatusText ( http . StatusNotFound ), http . StatusNotFound )
66
35
return
67
36
}
68
37
routing .UpdateFuncInfo (req .Context (), funcInfo )
69
38
70
39
rPath := strings .TrimPrefix (req .URL .Path , "/" + prefix + "/" )
71
40
rPath = util .PathJoinRelX (rPath )
72
- if rPath == "" || rPath == "." {
73
- http .Error (w , "file not found" , http .StatusNotFound )
74
- return
75
- }
76
41
77
- fi , err := objStore .Stat (rPath )
42
+ u , err := objStore .URL (rPath , path . Base ( rPath ) )
78
43
if err != nil {
79
44
if os .IsNotExist (err ) || errors .Is (err , os .ErrNotExist ) {
80
45
log .Warn ("Unable to find %s %s" , prefix , rPath )
81
- http .Error (w , "file not found" , http .StatusNotFound )
46
+ http .Error (w , http . StatusText ( http . StatusNotFound ) , http .StatusNotFound )
82
47
return
83
48
}
84
- log .Error ("Error whilst opening %s %s. Error: %v" , prefix , rPath , err )
85
- http .Error (w , fmt .Sprintf ("Error whilst opening %s %s" , prefix , rPath ), http .StatusInternalServerError )
49
+ log .Error ("Error whilst getting URL for %s %s. Error: %v" , prefix , rPath , err )
50
+ http .Error (w , fmt .Sprintf ("Error whilst getting URL for %s %s" , prefix , rPath ), http .StatusInternalServerError )
86
51
return
87
52
}
88
53
89
- fr , err := objStore .Open (rPath )
90
- if err != nil {
91
- log .Error ("Error whilst opening %s %s. Error: %v" , prefix , rPath , err )
92
- http .Error (w , fmt .Sprintf ("Error whilst opening %s %s" , prefix , rPath ), http .StatusInternalServerError )
93
- return
94
- }
95
- defer fr .Close ()
96
- httpcache .ServeContentWithCacheControl (w , req , path .Base (rPath ), fi .ModTime (), fr )
54
+ http .Redirect (w , req , u .String (), http .StatusTemporaryRedirect )
97
55
})
98
56
}
57
+
58
+ return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
59
+ if req .Method != "GET" && req .Method != "HEAD" {
60
+ http .Error (w , http .StatusText (http .StatusMethodNotAllowed ), http .StatusMethodNotAllowed )
61
+ return
62
+ }
63
+
64
+ if ! strings .HasPrefix (req .URL .Path , "/" + prefix + "/" ) {
65
+ http .Error (w , http .StatusText (http .StatusNotFound ), http .StatusNotFound )
66
+ return
67
+ }
68
+ routing .UpdateFuncInfo (req .Context (), funcInfo )
69
+
70
+ rPath := strings .TrimPrefix (req .URL .Path , "/" + prefix + "/" )
71
+ rPath = util .PathJoinRelX (rPath )
72
+ if rPath == "" || rPath == "." {
73
+ http .Error (w , http .StatusText (http .StatusNotFound ), http .StatusNotFound )
74
+ return
75
+ }
76
+
77
+ fi , err := objStore .Stat (rPath )
78
+ if err != nil {
79
+ if os .IsNotExist (err ) || errors .Is (err , os .ErrNotExist ) {
80
+ log .Warn ("Unable to find %s %s" , prefix , rPath )
81
+ http .Error (w , http .StatusText (http .StatusNotFound ), http .StatusNotFound )
82
+ return
83
+ }
84
+ log .Error ("Error whilst opening %s %s. Error: %v" , prefix , rPath , err )
85
+ http .Error (w , fmt .Sprintf ("Error whilst opening %s %s" , prefix , rPath ), http .StatusInternalServerError )
86
+ return
87
+ }
88
+
89
+ fr , err := objStore .Open (rPath )
90
+ if err != nil {
91
+ log .Error ("Error whilst opening %s %s. Error: %v" , prefix , rPath , err )
92
+ http .Error (w , fmt .Sprintf ("Error whilst opening %s %s" , prefix , rPath ), http .StatusInternalServerError )
93
+ return
94
+ }
95
+ defer fr .Close ()
96
+ httpcache .ServeContentWithCacheControl (w , req , path .Base (rPath ), fi .ModTime (), fr )
97
+ })
99
98
}
0 commit comments