s3manager-web/page-handlers.go

101 lines
2 KiB
Go
Raw Normal View History

2016-12-18 22:54:21 +01:00
package main
import (
"fmt"
"html/template"
"net/http"
"path"
2016-12-21 00:32:40 +01:00
"github.com/gorilla/mux"
2016-12-18 22:54:21 +01:00
minio "github.com/minio/minio-go"
)
2016-12-21 00:32:40 +01:00
// 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
}
2016-12-20 19:47:36 +01:00
// indexPageHandler forwards to "/buckets"
func indexPageHandler(w http.ResponseWriter, r *http.Request) {
2016-12-20 19:35:27 +01:00
http.Redirect(w, r, "/buckets", http.StatusPermanentRedirect)
}
// bucketsHandler handles the main page
2016-12-20 19:47:36 +01:00
func bucketsPageHandler(w http.ResponseWriter, r *http.Request) {
2016-12-18 22:54:21 +01:00
lp := path.Join("templates", "layout.html")
ip := path.Join("templates", "index.html")
t, err := template.ParseFiles(lp, ip)
if err != nil {
panic(err)
}
buckets, err := minioClient.ListBuckets()
if err != nil {
panic(err)
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
panic(err)
}
}
2016-12-21 00:32:40 +01:00
// bucketHandler shows the details page of a bucket
2016-12-20 19:47:36 +01:00
func bucketPageHandler(w http.ResponseWriter, r *http.Request) {
2016-12-21 00:32:40 +01:00
bucketName := mux.Vars(r)["bucketName"]
var objects []ObjectWithIcon
2016-12-18 22:54:21 +01:00
lp := path.Join("templates", "layout.html")
bp := path.Join("templates", "bucket.html")
t, err := template.ParseFiles(lp, bp)
if err != nil {
panic(err)
}
doneCh := make(chan struct{})
2016-12-21 00:32:40 +01:00
objectCh := minioClient.ListObjectsV2(bucketName, "", false, doneCh)
2016-12-18 22:54:21 +01:00
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
return
}
2016-12-21 00:32:40 +01:00
objectWithIcon := ObjectWithIcon{object, getIcon(object.Key)}
objects = append(objects, objectWithIcon)
}
bucketPage := BucketPage{
BucketName: bucketName,
Objects: objects,
2016-12-18 22:54:21 +01:00
}
2016-12-21 00:32:40 +01:00
err = t.ExecuteTemplate(w, "layout", bucketPage)
2016-12-18 22:54:21 +01:00
if err != nil {
panic(err)
}
}
2016-12-21 00:32:40 +01:00
func getIcon(fileName string) string {
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"
}