s3manager-web/api-handlers.go

154 lines
3.6 KiB
Go
Raw Normal View History

2016-12-21 00:32:40 +01:00
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"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"`
}
2016-12-21 00:32:40 +01:00
// createBucketHandler creates a new bucket
func createBucketHandler(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err = r.Body.Close(); err != nil {
panic(err)
}
err = json.Unmarshal(body, &bucket)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
err = minioClient.MakeBucket(bucket.Name, "")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
panic(err)
}
}
2016-12-21 22:29:12 +01:00
// deleteBucketHandler deletes a bucket
func deleteBucketHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := minioClient.RemoveBucket(vars["bucketName"])
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusOK)
}
2016-12-21 00:32:40 +01:00
// getObjectHandler downloads an object to the client
func getObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
objectName := vars["objectName"]
object, err := minioClient.GetObject(vars["bucketName"], objectName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
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 {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
2016-12-21 01:45:07 +01:00
// createObjectHandler allows to upload a new object
func createObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2016-12-21 22:29:12 +01:00
if r.Header.Get("Content-Type") == "application/json" {
var copy CopyObjectInfo
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err = r.Body.Close(); err != nil {
panic(err)
}
err = json.Unmarshal(body, &copy)
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
var copyConds = minio.NewCopyConditions()
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
fmt.Println(copy)
fmt.Println(objectSource)
err = minioClient.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(copy)
if err != nil {
panic(err)
}
} else {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
panic(err)
}
file, handler, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
defer file.Close()
_, err = minioClient.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
if err != nil {
panic(err)
}
w.WriteHeader(http.StatusCreated)
2016-12-21 01:45:07 +01:00
}
}
2016-12-21 00:32:40 +01:00
// deleteObjectHandler deletes an object
func deleteObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := minioClient.RemoveObject(vars["bucketName"], vars["objectName"])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}