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

75 lines
1.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"
"net/http"
"path"
2018-03-14 21:53:35 +01:00
"path/filepath"
2017-04-03 14:08:01 +02:00
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
2017-04-03 14:08:01 +02:00
minio "github.com/minio/minio-go"
)
2018-05-31 16:10:41 +02:00
// HandleBucketView shows the details page of a bucket.
func HandleBucketView(s3 S3, tmplDir string) 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
}
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 := way.Param(r.Context(), "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)
2017-04-03 14:08:01 +02:00
objectCh := s3.ListObjectsV2(bucketName, "", true, doneCh)
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{
2017-04-03 14:08:01 +02:00
BucketName: bucketName,
Objects: objs,
}
2018-03-14 21:53:35 +01:00
l := filepath.Join(tmplDir, "layout.html.tmpl")
p := filepath.Join(tmplDir, "bucket.html.tmpl")
t, err := template.ParseFiles(l, p)
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"
}