2017-05-08 23:07:07 +02:00
|
|
|
package s3manager
|
2017-04-03 14:08:01 +02:00
|
|
|
|
|
|
|
import (
|
2019-09-05 00:44:02 +02:00
|
|
|
"fmt"
|
2017-04-03 14:08:01 +02:00
|
|
|
"html/template"
|
2021-04-21 11:37:38 +02:00
|
|
|
"io/fs"
|
2017-04-03 14:08:01 +02:00
|
|
|
"net/http"
|
2022-03-23 10:10:30 +01:00
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2017-04-03 14:08:01 +02:00
|
|
|
)
|
|
|
|
|
2018-05-31 16:10:41 +02:00
|
|
|
// HandleBucketsView renders all buckets on an HTML page.
|
2022-03-23 10:10:30 +01:00
|
|
|
func HandleBucketsView(s3 S3, templates fs.FS, allowDelete bool) http.HandlerFunc {
|
|
|
|
type pageData struct {
|
|
|
|
Buckets []minio.BucketInfo
|
|
|
|
AllowDelete bool
|
|
|
|
}
|
|
|
|
|
2021-08-05 11:44:40 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
buckets, err := s3.ListBuckets(r.Context())
|
2017-04-03 14:08:01 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error listing buckets: %w", err))
|
2017-04-03 14:08:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:10:30 +01:00
|
|
|
data := pageData{
|
|
|
|
Buckets: buckets,
|
|
|
|
AllowDelete: allowDelete,
|
|
|
|
}
|
|
|
|
|
2021-04-21 11:37:38 +02:00
|
|
|
t, err := template.ParseFS(templates, "layout.html.tmpl", "buckets.html.tmpl")
|
2017-04-03 14:08:01 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
|
2017-04-03 14:08:01 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-23 10:10:30 +01:00
|
|
|
err = t.ExecuteTemplate(w, "layout", data)
|
2017-04-03 14:08:01 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error executing template: %w", err))
|
2017-04-03 14:08:01 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-31 16:10:41 +02:00
|
|
|
}
|
2017-04-03 14:08:01 +02:00
|
|
|
}
|