@@ -6,25 +6,25 @@ import (
66 "io/fs"
77 "net/http"
88 "regexp"
9- "sort"
109 "strconv"
1110 "strings"
12- "time"
1311
1412 "github.com/gorilla/mux"
1513 "github.com/minio/minio-go/v7"
1614)
1715
18- // objectWithIconExtended extends objectWithIcon with additional formatting fields
19- type objectWithIconExtended struct {
20- Key string
21- Size int64
22- SizeDisplay string
23- LastModified time.Time
24- Owner string
25- Icon string
26- IsFolder bool
27- DisplayName string
16+ // withInstance extracts the instance from the request, looks it up in the manager,
17+ // and delegates to a handler that receives the resolved S3 client.
18+ func withInstance (manager * MultiS3Manager , fn func (S3 ) http.HandlerFunc ) http.HandlerFunc {
19+ return func (w http.ResponseWriter , r * http.Request ) {
20+ instanceName := mux .Vars (r )["instance" ]
21+ current , err := manager .GetInstance (instanceName )
22+ if err != nil {
23+ http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
24+ return
25+ }
26+ fn (current .Client )(w , r )
27+ }
2828}
2929
3030// HandleBucketsViewWithManager renders all buckets on an HTML page using MultiS3Manager.
@@ -120,142 +120,45 @@ func HandleBucketViewWithManager(manager *MultiS3Manager, templates fs.FS, allow
120120
121121// HandleCreateBucketWithManager creates a new bucket using MultiS3Manager.
122122func HandleCreateBucketWithManager (manager * MultiS3Manager ) http.HandlerFunc {
123- return func (w http.ResponseWriter , r * http.Request ) {
124- vars := mux .Vars (r )
125- instanceName := vars ["instance" ]
126-
127- current , err := manager .GetInstance (instanceName )
128- if err != nil {
129- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
130- return
131- }
132-
133- s3 := current .Client
134- // Delegate to the original handler with the current S3 client
135- handler := HandleCreateBucket (s3 )
136- handler (w , r )
137- }
123+ return withInstance (manager , HandleCreateBucket )
138124}
139125
140126// HandleDeleteBucketWithManager deletes a bucket using MultiS3Manager.
141127func HandleDeleteBucketWithManager (manager * MultiS3Manager ) http.HandlerFunc {
142- return func (w http.ResponseWriter , r * http.Request ) {
143- vars := mux .Vars (r )
144- instanceName := vars ["instance" ]
145-
146- current , err := manager .GetInstance (instanceName )
147- if err != nil {
148- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
149- return
150- }
151-
152- s3 := current .Client
153- // Delegate to the original handler with the current S3 client
154- handler := HandleDeleteBucket (s3 )
155- handler (w , r )
156- }
128+ return withInstance (manager , HandleDeleteBucket )
157129}
158130
159131// HandleCreateObjectWithManager uploads a new object using MultiS3Manager.
160132func HandleCreateObjectWithManager (manager * MultiS3Manager , sseInfo SSEType ) http.HandlerFunc {
161- return func (w http.ResponseWriter , r * http.Request ) {
162- vars := mux .Vars (r )
163- instanceName := vars ["instance" ]
164-
165- current , err := manager .GetInstance (instanceName )
166- if err != nil {
167- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
168- return
169- }
170-
171- s3 := current .Client
172- // Delegate to the original handler with the current S3 client
173- handler := HandleCreateObject (s3 , sseInfo )
174- handler (w , r )
175- }
133+ return withInstance (manager , func (s3 S3 ) http.HandlerFunc { return HandleCreateObject (s3 , sseInfo ) })
176134}
177135
178136// HandleGenerateURLWithManager generates a presigned URL using MultiS3Manager.
179137func HandleGenerateURLWithManager (manager * MultiS3Manager ) http.HandlerFunc {
180- return func (w http.ResponseWriter , r * http.Request ) {
181- vars := mux .Vars (r )
182- instanceName := vars ["instance" ]
183-
184- current , err := manager .GetInstance (instanceName )
185- if err != nil {
186- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
187- return
188- }
189-
190- s3 := current .Client
191- // Delegate to the original handler with the current S3 client
192- handler := HandleGenerateURL (s3 )
193- handler (w , r )
194- }
138+ return withInstance (manager , HandleGenerateURL )
195139}
196140
197141// HandleGetObjectWithManager downloads an object to the client using MultiS3Manager.
198142func HandleGetObjectWithManager (manager * MultiS3Manager , forceDownload bool ) http.HandlerFunc {
199- return func (w http.ResponseWriter , r * http.Request ) {
200- vars := mux .Vars (r )
201- instanceName := vars ["instance" ]
202-
203- current , err := manager .GetInstance (instanceName )
204- if err != nil {
205- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
206- return
207- }
208-
209- s3 := current .Client
210- // Delegate to the original handler with the current S3 client
211- handler := HandleGetObject (s3 , forceDownload )
212- handler (w , r )
213- }
143+ return withInstance (manager , func (s3 S3 ) http.HandlerFunc { return HandleGetObject (s3 , forceDownload ) })
214144}
215145
216146// HandleDeleteObjectWithManager deletes an object using MultiS3Manager.
217147func HandleDeleteObjectWithManager (manager * MultiS3Manager ) http.HandlerFunc {
218- return func (w http.ResponseWriter , r * http.Request ) {
219- vars := mux .Vars (r )
220- instanceName := vars ["instance" ]
221-
222- current , err := manager .GetInstance (instanceName )
223- if err != nil {
224- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
225- return
226- }
227-
228- s3 := current .Client
229- // Delegate to the original handler with the current S3 client
230- handler := HandleDeleteObject (s3 )
231- handler (w , r )
232- }
148+ return withInstance (manager , HandleDeleteObject )
233149}
234150
235151// HandleCheckPublicAccessWithManager checks if an object is publicly accessible using MultiS3Manager.
236152func HandleCheckPublicAccessWithManager (manager * MultiS3Manager ) http.HandlerFunc {
237- return func (w http.ResponseWriter , r * http.Request ) {
238- vars := mux .Vars (r )
239- instanceName := vars ["instance" ]
240-
241- current , err := manager .GetInstance (instanceName )
242- if err != nil {
243- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
244- return
245- }
246-
247- s3 := current .Client
248- handler := HandleCheckPublicAccess (s3 )
249- handler (w , r )
250- }
153+ return withInstance (manager , HandleCheckPublicAccess )
251154}
252155
253156// createBucketViewWithS3Data creates a bucket view handler that includes S3 instance data
254157func createBucketViewWithS3Data (s3 S3 , templates fs.FS , allowDelete bool , listRecursive bool , rootURL string , current * S3Instance , instances []* S3Instance ) http.HandlerFunc {
255158 type pageData struct {
256159 RootURL string
257160 BucketName string
258- Objects []objectWithIconExtended
161+ Objects []objectWithIcon
259162 AllowDelete bool
260163 Paths []string
261164 CurrentPath string
@@ -309,7 +212,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
309212 }
310213 }
311214
312- perPage := 25
215+ perPage := defaultPerPage
313216 showAll := false
314217 if perPageStr := r .URL .Query ().Get ("perPage" ); perPageStr != "" {
315218 if pp , err := strconv .Atoi (perPageStr ); err == nil {
@@ -324,7 +227,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
324227 // Get search parameter
325228 search := strings .TrimSpace (r .URL .Query ().Get ("search" ))
326229
327- var objs []objectWithIconExtended
230+ var objs []objectWithIcon
328231 hasError := false
329232 errorMessage := ""
330233
@@ -349,7 +252,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
349252
350253 sizeDisplay := FormatFileSize (object .Size )
351254
352- obj := objectWithIconExtended {
255+ obj := objectWithIcon {
353256 Key : object .Key ,
354257 Size : object .Size ,
355258 SizeDisplay : sizeDisplay ,
@@ -365,7 +268,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
365268 // Filter objects based on search query
366269 if search != "" && ! hasError {
367270 searchLower := strings .ToLower (search )
368- filteredObjs := make ([]objectWithIconExtended , 0 )
271+ filteredObjs := make ([]objectWithIcon , 0 )
369272 for _ , obj := range objs {
370273 // Search in DisplayName and Key (case-insensitive)
371274 if strings .Contains (strings .ToLower (obj .DisplayName ), searchLower ) ||
@@ -378,7 +281,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
378281
379282 // Sort objects based on sortBy and sortOrder
380283 if ! hasError {
381- sortObjectsWithSize (objs , sortBy , sortOrder )
284+ sortObjects (objs , sortBy , sortOrder )
382285 }
383286
384287 // Calculate pagination
@@ -415,7 +318,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
415318 if start < totalItems && ! hasError {
416319 objs = objs [start :end ]
417320 } else if ! hasError {
418- objs = []objectWithIconExtended {}
321+ objs = []objectWithIcon {}
419322 }
420323 }
421324
@@ -474,100 +377,22 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
474377 }
475378}
476379
477- // sortObjectsWithSize sorts objects with SizeDisplay field based on the specified field and order
478- func sortObjectsWithSize (objs []objectWithIconExtended , sortBy , sortOrder string ) {
479- sort .Slice (objs , func (i , j int ) bool {
480- var less bool
481- switch sortBy {
482- case "size" :
483- less = objs [i ].Size < objs [j ].Size
484- case "owner" :
485- less = strings .ToLower (objs [i ].Owner ) < strings .ToLower (objs [j ].Owner )
486- case "lastModified" :
487- less = objs [i ].LastModified .Before (objs [j ].LastModified )
488- case "key" :
489- fallthrough
490- default :
491- less = strings .ToLower (objs [i ].DisplayName ) < strings .ToLower (objs [j ].DisplayName )
492- }
493-
494- if sortOrder == "desc" {
495- return ! less
496- }
497- return less
498- })
499- }
500-
501380// HandleBulkDeleteObjectsWithManager deletes multiple objects using MultiS3Manager.
502381func HandleBulkDeleteObjectsWithManager (manager * MultiS3Manager ) http.HandlerFunc {
503- return func (w http.ResponseWriter , r * http.Request ) {
504- vars := mux .Vars (r )
505- instanceName := vars ["instance" ]
506-
507- current , err := manager .GetInstance (instanceName )
508- if err != nil {
509- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
510- return
511- }
512-
513- s3 := current .Client
514- // Delegate to the bulk delete handler with the current S3 client
515- handler := HandleBulkDeleteObjects (s3 )
516- handler (w , r )
517- }
382+ return withInstance (manager , HandleBulkDeleteObjects )
518383}
519384
520385// HandleGetBucketPolicyWithManager retrieves the policy for a bucket using MultiS3Manager.
521386func HandleGetBucketPolicyWithManager (manager * MultiS3Manager ) http.HandlerFunc {
522- return func (w http.ResponseWriter , r * http.Request ) {
523- vars := mux .Vars (r )
524- instanceName := vars ["instance" ]
525-
526- current , err := manager .GetInstance (instanceName )
527- if err != nil {
528- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
529- return
530- }
531-
532- s3 := current .Client
533- handler := HandleGetBucketPolicy (s3 )
534- handler (w , r )
535- }
387+ return withInstance (manager , HandleGetBucketPolicy )
536388}
537389
538390// HandlePutBucketPolicyWithManager sets the policy for a bucket using MultiS3Manager.
539391func HandlePutBucketPolicyWithManager (manager * MultiS3Manager ) http.HandlerFunc {
540- return func (w http.ResponseWriter , r * http.Request ) {
541- vars := mux .Vars (r )
542- instanceName := vars ["instance" ]
543-
544- current , err := manager .GetInstance (instanceName )
545- if err != nil {
546- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
547- return
548- }
549-
550- s3 := current .Client
551- handler := HandlePutBucketPolicy (s3 )
552- handler (w , r )
553- }
392+ return withInstance (manager , HandlePutBucketPolicy )
554393}
555394
556395// HandleBulkDownloadObjectsWithManager downloads multiple objects as a ZIP using MultiS3Manager.
557396func HandleBulkDownloadObjectsWithManager (manager * MultiS3Manager ) http.HandlerFunc {
558- return func (w http.ResponseWriter , r * http.Request ) {
559- vars := mux .Vars (r )
560- instanceName := vars ["instance" ]
561-
562- current , err := manager .GetInstance (instanceName )
563- if err != nil {
564- http .Error (w , fmt .Sprintf ("Instance not found: %s" , err .Error ()), http .StatusNotFound )
565- return
566- }
567-
568- s3 := current .Client
569- // Delegate to the bulk download handler with the current S3 client
570- handler := HandleBulkDownloadObjects (s3 )
571- handler (w , r )
572- }
397+ return withInstance (manager , HandleBulkDownloadObjects )
573398}
0 commit comments