2016-12-18 22:54:21 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
|
2016-12-21 00:32:40 +01:00
|
|
|
"github.com/gorilla/mux"
|
2017-03-17 23:57:58 +01:00
|
|
|
"github.com/minio/minio-go"
|
2016-12-18 22:54:21 +01:00
|
|
|
)
|
|
|
|
|
2016-12-21 00:32:40 +01:00
|
|
|
// BucketPage defines the details page of a bucket
|
|
|
|
type BucketPage struct {
|
|
|
|
BucketName string
|
|
|
|
Objects []ObjectWithIcon
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:54:30 +01:00
|
|
|
// ObjectWithIcon is a minio object with an added icon
|
|
|
|
type ObjectWithIcon struct {
|
|
|
|
minio.ObjectInfo
|
|
|
|
Icon string
|
2016-12-18 22:54:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 21:44:11 +01:00
|
|
|
// BucketPageHandler shows the details page of a bucket
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) BucketPageHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bucketName := mux.Vars(r)["bucketName"]
|
|
|
|
var objects []ObjectWithIcon
|
2016-12-18 22:54:21 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
lp := path.Join("templates", "layout.html")
|
|
|
|
bp := path.Join("templates", "bucket.html")
|
2016-12-18 22:54:21 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
t, err := template.ParseFiles(lp, bp)
|
|
|
|
if err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error parsing templates"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2016-12-18 22:54:21 +01:00
|
|
|
return
|
|
|
|
}
|
2016-12-21 00:32:40 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
doneCh := make(chan struct{})
|
2017-03-12 16:07:59 +01:00
|
|
|
objectCh := s.S3.ListObjectsV2(bucketName, "", false, doneCh)
|
2017-03-10 11:12:20 +01:00
|
|
|
for object := range objectCh {
|
|
|
|
if object.Err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error listing objects"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
|
|
|
|
objects = append(objects, objectWithIcon)
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketPage := BucketPage{
|
|
|
|
BucketName: bucketName,
|
|
|
|
Objects: objects,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.ExecuteTemplate(w, "layout", bucketPage)
|
|
|
|
if err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error executing template"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2016-12-18 22:54:21 +01:00
|
|
|
}
|
2016-12-21 00:32:40 +01:00
|
|
|
|
2017-03-10 10:54:30 +01:00
|
|
|
// BucketsPageHandler shows all buckets
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) BucketsPageHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
lp := path.Join("templates", "layout.html")
|
|
|
|
ip := path.Join("templates", "index.html")
|
2017-03-10 10:54:30 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
t, err := template.ParseFiles(lp, ip)
|
|
|
|
if err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error parsing templates"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
2017-03-10 10:54:30 +01:00
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
buckets, err := s.S3.ListBuckets()
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error listing buckets"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.ExecuteTemplate(w, "layout", buckets)
|
|
|
|
if err != nil {
|
2017-03-30 14:00:06 +02:00
|
|
|
msg := "error executing template"
|
|
|
|
handleHTTPError(w, msg, err, http.StatusInternalServerError)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2017-03-10 10:54:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IndexHandler forwards to "/buckets"
|
2017-03-10 11:12:20 +01:00
|
|
|
func IndexHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, "/buckets", http.StatusPermanentRedirect)
|
|
|
|
})
|
2017-03-10 10:54:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 21:20:40 +01:00
|
|
|
// icon returns an icon for a file type
|
|
|
|
func icon(fileName string) string {
|
2016-12-21 00:32:40 +01:00
|
|
|
e := path.Ext(fileName)
|
|
|
|
|
|
|
|
switch e {
|
|
|
|
case ".tgz":
|
|
|
|
return "archive"
|
|
|
|
case ".png", ".jpg", ".gif", ".svg":
|
|
|
|
return "photo"
|
|
|
|
case ".mp3":
|
|
|
|
return "music_note"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "insert_drive_file"
|
|
|
|
}
|