s3manager-web/bucket_view.go

84 lines
1.8 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"
"github.com/gorilla/mux"
minio "github.com/minio/minio-go"
)
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.
func BucketViewHandler(s3 S3) 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"]
2017-05-08 23:07:07 +02:00
var objs []objectWithIcon
2017-04-03 14:08:01 +02:00
2017-04-07 10:07:23 +02:00
l := path.Join(tmplDirectory, "layout.html.tmpl")
p := path.Join(tmplDirectory, "bucket.html.tmpl")
2017-04-03 14:08:01 +02:00
t, err := template.ParseFiles(l, p)
if err != nil {
2017-04-09 16:28:57 +02:00
handleHTTPError(w, http.StatusInternalServerError, err)
2017-04-03 14:08:01 +02:00
return
}
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-04-03 22:03:45 +02:00
code := http.StatusInternalServerError
2017-04-09 16:28:57 +02:00
if object.Err.Error() == ErrBucketDoesNotExist {
2017-04-03 22:03:45 +02:00
code = http.StatusNotFound
}
2017-04-09 16:28:57 +02:00
handleHTTPError(w, code, object.Err)
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,
}
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-04-09 16:28:57 +02:00
handleHTTPError(w, http.StatusInternalServerError, err)
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"
}