2016-12-21 00:32:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-03-17 23:51:17 +01:00
|
|
|
"log"
|
2016-12-21 00:32:40 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/minio-go"
|
|
|
|
)
|
|
|
|
|
2016-12-21 22:29:12 +01:00
|
|
|
// CopyObjectInfo is the information about an object to copy
|
|
|
|
type CopyObjectInfo struct {
|
|
|
|
BucketName string `json:"bucketName"`
|
|
|
|
ObjectName string `json:"objectName"`
|
|
|
|
SourceBucketName string `json:"sourceBucketName"`
|
|
|
|
SourceObjectName string `json:"sourceObjectName"`
|
|
|
|
}
|
|
|
|
|
2017-03-09 21:20:40 +01:00
|
|
|
// CreateBucketHandler creates a new bucket
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) CreateBucketHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var bucket minio.BucketInfo
|
2016-12-21 01:45:07 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&bucket)
|
2016-12-21 22:29:12 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
log.Println("error decoding json:", err)
|
2016-12-21 22:29:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
err = s.S3.MakeBucket(bucket.Name, "")
|
2016-12-21 22:29:12 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error making bucket:", err)
|
2016-12-21 22:29:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
err = json.NewEncoder(w).Encode(bucket)
|
2016-12-21 22:29:12 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error encoding json:", err)
|
2016-12-21 22:29:12 +01:00
|
|
|
return
|
|
|
|
}
|
2017-03-10 11:12:20 +01:00
|
|
|
})
|
|
|
|
}
|
2016-12-21 22:29:12 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
// CreateObjectHandler allows to upload a new object
|
|
|
|
func (s *Server) CreateObjectHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/json" {
|
|
|
|
var copy CopyObjectInfo
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(©)
|
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
log.Println("error decoding json:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var copyConds = minio.NewCopyConditions()
|
|
|
|
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
|
|
|
|
fmt.Println(copy)
|
|
|
|
fmt.Println(objectSource)
|
2017-03-12 16:07:59 +01:00
|
|
|
err = s.S3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error copying object:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
err = json.NewEncoder(w).Encode(copy)
|
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error encoding json:", err)
|
|
|
|
return
|
2017-03-10 11:12:20 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
log.Println("error parsing form:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
log.Println("error getting form file:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
_, err = s.S3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error putting object:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2016-12-21 22:29:12 +01:00
|
|
|
}
|
2017-03-10 11:12:20 +01:00
|
|
|
})
|
2016-12-21 01:45:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:54:30 +01:00
|
|
|
// DeleteBucketHandler deletes a bucket
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) DeleteBucketHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2017-03-10 10:54:30 +01:00
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
err := s.S3.RemoveBucket(vars["bucketName"])
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error removing bucket:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
2017-03-10 10:54:30 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
})
|
2017-03-10 10:54:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 21:44:11 +01:00
|
|
|
// DeleteObjectHandler deletes an object
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) DeleteObjectHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2016-12-21 00:32:40 +01:00
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
err := s.S3.RemoveObject(vars["bucketName"], vars["objectName"])
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error removing object:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
2016-12-21 00:32:40 +01:00
|
|
|
|
2017-03-10 11:12:20 +01:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
})
|
2016-12-21 00:32:40 +01:00
|
|
|
}
|
2017-03-10 10:54:30 +01:00
|
|
|
|
|
|
|
// GetObjectHandler downloads an object to the client
|
2017-03-10 11:12:20 +01:00
|
|
|
func (s *Server) GetObjectHandler() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
objectName := vars["objectName"]
|
|
|
|
|
2017-03-12 16:07:59 +01:00
|
|
|
object, err := s.S3.GetObject(vars["bucketName"], objectName)
|
2017-03-10 11:12:20 +01:00
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error getting object:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", objectName))
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
|
|
|
|
_, err = io.Copy(w, object)
|
|
|
|
if err != nil {
|
2017-03-17 23:51:17 +01:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("error copying object:", err)
|
2017-03-10 11:12:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2017-03-10 10:54:30 +01:00
|
|
|
}
|