2015-01-19 14:51:45 -05:00
|
|
|
package minioapi
|
|
|
|
|
|
|
|
import (
|
2015-01-20 19:08:14 -05:00
|
|
|
"bytes"
|
2015-01-20 21:39:30 -05:00
|
|
|
"encoding/json"
|
2015-01-20 19:08:14 -05:00
|
|
|
"encoding/xml"
|
2015-01-19 20:39:20 -05:00
|
|
|
"log"
|
2015-01-19 14:51:45 -05:00
|
|
|
"net/http"
|
2015-01-20 21:39:30 -05:00
|
|
|
"time"
|
2015-01-19 14:51:45 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
mstorage "github.com/minio-io/minio/pkg/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type minioApi struct {
|
|
|
|
storage *mstorage.Storage
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:39:30 -05:00
|
|
|
type encoder interface {
|
|
|
|
Encode(v interface{}) error
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:51:45 -05:00
|
|
|
func HttpHandler(storage *mstorage.Storage) http.Handler {
|
|
|
|
mux := mux.NewRouter()
|
|
|
|
api := minioApi{
|
|
|
|
storage: storage,
|
|
|
|
}
|
2015-01-20 21:39:30 -05:00
|
|
|
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
|
2015-01-20 22:10:52 -05:00
|
|
|
mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET")
|
2015-01-20 21:39:30 -05:00
|
|
|
mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET")
|
2015-01-19 14:51:45 -05:00
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET")
|
|
|
|
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT")
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
2015-01-19 20:39:20 -05:00
|
|
|
|
|
|
|
_, err := server.storage.CopyObjectToWriter(w, bucket, object)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil: // success
|
|
|
|
{
|
|
|
|
log.Println("Found: " + bucket + "#" + object)
|
|
|
|
}
|
|
|
|
case mstorage.ObjectNotFound:
|
|
|
|
{
|
|
|
|
log.Println(err)
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
log.Println(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 14:51:45 -05:00
|
|
|
}
|
|
|
|
|
2015-01-20 21:39:30 -05:00
|
|
|
func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte("/"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-01-20 19:08:14 -05:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
|
|
|
|
//delimiter, ok := vars["delimiter"]
|
|
|
|
//encodingType, ok := vars["encoding-type"]
|
|
|
|
//marker, ok := vars["marker"]
|
|
|
|
//maxKeys, ok := vars["max-keys"]
|
2015-01-20 21:39:30 -05:00
|
|
|
bucket := vars["bucket"]
|
2015-01-20 19:08:14 -05:00
|
|
|
//bucket, ok := vars["bucket"]
|
|
|
|
//if ok == false {
|
|
|
|
// w.WriteHeader(http.StatusBadRequest)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
prefix, ok := vars["prefix"]
|
|
|
|
if ok == false {
|
|
|
|
prefix = ""
|
|
|
|
}
|
|
|
|
|
2015-01-20 21:39:30 -05:00
|
|
|
contentType := "xml"
|
|
|
|
|
|
|
|
if req.Header["Accept"][0] == "application/json" {
|
|
|
|
contentType = "json"
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:08:14 -05:00
|
|
|
objects := server.storage.ListObjects(bucket, prefix, 1000)
|
|
|
|
response := generateListResult(objects)
|
|
|
|
|
|
|
|
var bytesBuffer bytes.Buffer
|
2015-01-20 21:39:30 -05:00
|
|
|
var encoder encoder
|
|
|
|
if contentType == "json" {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
encoder = json.NewEncoder(&bytesBuffer)
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
|
|
encoder = xml.NewEncoder(&bytesBuffer)
|
|
|
|
}
|
|
|
|
encoder.Encode(response)
|
2015-01-20 19:08:14 -05:00
|
|
|
|
|
|
|
w.Write(bytesBuffer.Bytes())
|
|
|
|
}
|
|
|
|
|
2015-01-19 14:51:45 -05:00
|
|
|
func (server *minioApi) putObjectHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
2015-01-20 22:13:57 -05:00
|
|
|
err := server.storage.StoreObject(bucket, object, req.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2015-01-19 14:51:45 -05:00
|
|
|
}
|
2015-01-20 19:08:14 -05:00
|
|
|
|
|
|
|
func generateListResult(objects []mstorage.ObjectMetadata) ListResponse {
|
2015-01-20 21:39:30 -05:00
|
|
|
contents := []Content{}
|
|
|
|
|
2015-01-20 19:08:14 -05:00
|
|
|
owner := Owner{
|
2015-01-20 21:39:30 -05:00
|
|
|
ID: "minio",
|
|
|
|
DisplayName: "minio",
|
2015-01-20 19:08:14 -05:00
|
|
|
}
|
2015-01-20 21:39:30 -05:00
|
|
|
|
|
|
|
for _, object := range objects {
|
|
|
|
content := Content{
|
|
|
|
Key: object.Key,
|
|
|
|
LastModified: formatDate(object.SecCreated),
|
|
|
|
ETag: object.Key,
|
|
|
|
Size: object.Size,
|
|
|
|
StorageClass: "STANDARD",
|
2015-01-20 19:08:14 -05:00
|
|
|
Owner: owner,
|
2015-01-20 21:39:30 -05:00
|
|
|
}
|
|
|
|
contents = append(contents, content)
|
2015-01-20 19:08:14 -05:00
|
|
|
}
|
2015-01-20 21:39:30 -05:00
|
|
|
data := ListResponse{
|
|
|
|
Name: "name",
|
|
|
|
Contents: contents,
|
|
|
|
MaxKeys: len(objects),
|
|
|
|
IsTruncated: false,
|
2015-01-20 19:08:14 -05:00
|
|
|
}
|
2015-01-20 21:39:30 -05:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatDate(sec int64) string {
|
|
|
|
timeStamp := time.Unix(sec, 0)
|
|
|
|
return timeStamp.Format("2006-01-02T15:04:05.000Z")
|
2015-01-20 19:08:14 -05:00
|
|
|
}
|