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

78 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 (
"html/template"
"net/http"
"path"
2018-03-14 21:53:35 +01:00
"path/filepath"
2017-04-03 14:08:01 +02:00
"github.com/gorilla/mux"
minio "github.com/minio/minio-go"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-04-03 14:08:01 +02:00
)
2017-05-08 23:07:07 +02:00
// objectWithIcon is a minio object with an added icon.
type objectWithIcon struct {
2017-04-03 14:08:01 +02:00
minio.ObjectInfo
Icon string
}
2017-05-08 23:07:07 +02:00
// bucketPage defines the details page of a bucket.
type bucketPage struct {
2017-04-03 14:08:01 +02:00
BucketName string
2017-05-08 23:07:07 +02:00
Objects []objectWithIcon
2017-04-03 14:08:01 +02:00
}
2017-05-08 23:07:07 +02:00
// BucketViewHandler shows the details page of a bucket.
2018-03-14 21:53:35 +01:00
func BucketViewHandler(s3 S3, tmplDir string) http.Handler {
2017-04-03 14:08:01 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
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 {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(object.Err, "error listing objects"))
2017-04-03 14:08:01 +02:00
return
}
2017-05-08 23:07:07 +02:00
obj := objectWithIcon{object, icon(object.Key)}
objs = append(objs, obj)
2017-04-03 14:08:01 +02:00
}
2017-05-08 23:07:07 +02:00
page := bucketPage{
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 {
handleHTTPError(w, errors.Wrap(err, errParsingTemplates))
return
}
2017-05-08 23:07:07 +02:00
err = t.ExecuteTemplate(w, "layout", page)
2017-04-03 14:08:01 +02:00
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, errExecutingTemplate))
2017-04-03 14:08:01 +02:00
return
}
})
}
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 {
2017-04-03 22:31:14 +02:00
case ".tgz", ".gz":
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"
}
return "insert_drive_file"
}