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"
|
|
|
|
|
2022-03-23 10:10:30 +01:00
|
|
|
"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.
|
2022-03-23 10:10:30 +01:00
|
|
|
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 {
|
2022-03-23 10:10:30 +01:00
|
|
|
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) {
|
2022-03-23 10:10:30 +01:00
|
|
|
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)
|
2022-03-24 08:46:21 +01:00
|
|
|
opts := minio.ListObjectsOptions{
|
|
|
|
Recursive: listRecursive,
|
2022-03-23 10:10:30 +01:00
|
|
|
}
|
|
|
|
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{
|
2022-03-23 10:10:30 +01:00
|
|
|
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"
|
|
|
|
}
|