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

116 lines
2.6 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"
2023-06-13 17:37:57 +02:00
"regexp"
"strings"
"time"
2017-04-03 14:08:01 +02:00
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 {
2023-06-13 17:37:57 +02:00
Key string
Size int64
LastModified time.Time
Owner string
Icon string
IsFolder bool
DisplayName string
2018-05-22 22:56:01 +02:00
}
type pageData struct {
BucketName string
Objects []objectWithIcon
AllowDelete bool
2023-06-13 17:37:57 +02:00
Paths []string
2023-06-20 07:18:56 +02:00
CurrentPath string
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) {
2023-06-13 17:37:57 +02:00
regex := regexp.MustCompile(`\/buckets\/([^\/]*)\/?(.*)`)
matches := regex.FindStringSubmatch(r.RequestURI)
bucketName := matches[1]
path := matches[2]
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,
2023-06-13 17:37:57 +02:00
Prefix: path,
}
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
}
2023-06-13 17:37:57 +02:00
obj := objectWithIcon{
Key: object.Key,
Size: object.Size,
LastModified: object.LastModified,
Owner: object.Owner.DisplayName,
Icon: icon(object.Key),
IsFolder: strings.HasSuffix(object.Key, "/"),
DisplayName: strings.TrimSuffix(strings.TrimPrefix(object.Key, path), "/"),
}
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,
2023-06-13 17:37:57 +02:00
Paths: removeEmptyStrings(strings.Split(path, "/")),
2023-06-20 07:18:56 +02:00
CurrentPath: path,
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 {
2023-06-13 17:37:57 +02:00
if strings.HasSuffix(fileName, "/") {
return "folder"
}
2017-04-03 14:08:01 +02:00
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"
}
2023-06-13 17:37:57 +02:00
func removeEmptyStrings(input []string) []string {
var result []string
for _, str := range input {
if str != "" {
result = append(result, str)
}
}
return result
}