s3manager-web/internal/app/s3manager/bucket_view.go

79 lines
1.7 KiB
Go
Raw Normal View History

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"
"path"
"github.com/gorilla/mux"
2021-08-05 11:44:40 +02:00
"github.com/minio/minio-go/v7"
2017-04-03 14:08:01 +02:00
)
2018-05-31 16:10:41 +02:00
// HandleBucketView shows the details page of a bucket.
func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bool) http.HandlerFunc {
2018-05-22 22:56:01 +02:00
type objectWithIcon struct {
2018-06-18 20:39:58 +02:00
Info minio.ObjectInfo
2018-05-22 22:56:01 +02:00
Icon string
}
type pageData struct {
BucketName string
Objects []objectWithIcon
AllowDelete bool
2018-05-22 22:56:01 +02:00
}
2018-06-23 12:49:44 +02:00
2018-05-31 16:10:41 +02:00
return func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
2017-04-03 14:08:01 +02:00
2018-03-14 21:53:35 +01:00
var objs []objectWithIcon
2017-04-03 14:08:01 +02:00
doneCh := make(chan struct{})
2017-04-03 22:03:45 +02:00
defer close(doneCh)
opts := minio.ListObjectsOptions{}
if listRecursive {
opts.Recursive = true
}
objectCh := s3.ListObjects(r.Context(), bucketName, opts)
2017-04-03 14:08:01 +02:00
for object := range objectCh {
if object.Err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error listing objects: %w", object.Err))
2017-04-03 14:08:01 +02:00
return
}
2018-06-18 20:39:58 +02:00
obj := objectWithIcon{Info: object, Icon: icon(object.Key)}
2017-05-08 23:07:07 +02:00
objs = append(objs, obj)
2017-04-03 14:08:01 +02:00
}
2018-05-22 22:56:01 +02:00
data := pageData{
BucketName: bucketName,
Objects: objs,
AllowDelete: allowDelete,
2017-04-03 14:08:01 +02:00
}
2021-04-21 11:37:38 +02:00
t, err := template.ParseFS(templates, "layout.html.tmpl", "bucket.html.tmpl")
2018-03-14 21:53:35 +01:00
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
2018-03-14 21:53:35 +01:00
return
}
2018-05-22 22:56:01 +02: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
}
2017-05-08 23:07:07 +02:00
// icon returns an icon for a file type.
2017-04-03 14:08:01 +02:00
func icon(fileName string) string {
e := path.Ext(fileName)
switch e {
2019-08-18 14:26:29 +02:00
case ".tgz", ".gz", ".zip":
2017-04-03 14:08:01 +02:00
return "archive"
case ".png", ".jpg", ".gif", ".svg":
return "photo"
2017-04-03 22:31:14 +02:00
case ".mp3", ".wav":
2017-04-03 14:08:01 +02:00
return "music_note"
}
2019-09-05 00:44:02 +02:00
2017-04-03 14:08:01 +02:00
return "insert_drive_file"
}