s3manager-web/bucket-view.go

84 lines
1.8 KiB
Go
Raw Normal View History

2017-04-03 14:08:01 +02:00
package main
import (
"html/template"
"net/http"
"path"
"github.com/gorilla/mux"
minio "github.com/minio/minio-go"
)
// ObjectWithIcon is a minio object with an added icon
type ObjectWithIcon struct {
minio.ObjectInfo
Icon string
}
// BucketPage defines the details page of a bucket
type BucketPage struct {
BucketName string
Objects []ObjectWithIcon
}
// BucketViewHandler shows the details page of a bucket
func BucketViewHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
var objs []ObjectWithIcon
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
}
objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
objs = append(objs, objectWithIcon)
}
bucketPage := BucketPage{
BucketName: bucketName,
Objects: objs,
}
err = t.ExecuteTemplate(w, "layout", bucketPage)
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
}
})
}
// icon returns an icon for a file type
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"
}