2017-04-03 14:08:01 +02:00
|
|
|
package main
|
2017-03-30 22:48:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-04-07 10:07:23 +02:00
|
|
|
"strings"
|
2017-03-30 22:48:27 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
minio "github.com/minio/minio-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-04-03 14:08:01 +02:00
|
|
|
// CreateObjectHandler allows to upload a new object
|
|
|
|
func CreateObjectHandler(s3 S3Client) http.Handler {
|
2017-03-30 22:48:27 +02:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2017-04-07 10:07:23 +02:00
|
|
|
if strings.Contains(r.Header.Get(headerContentType), contentTypeJSON) {
|
2017-03-30 22:48:27 +02:00
|
|
|
var copy CopyObjectInfo
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(©)
|
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusInternalServerError, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-02 17:10:36 +02:00
|
|
|
copyConds := minio.NewCopyConditions()
|
2017-03-30 22:48:27 +02:00
|
|
|
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
|
|
|
|
err = s3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
|
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusInternalServerError, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-07 10:07:23 +02:00
|
|
|
w.Header().Set(headerContentType, contentTypeJSON)
|
2017-03-30 22:48:27 +02:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(copy)
|
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusInternalServerError, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusUnprocessableEntity, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusInternalServerError, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2017-04-07 10:07:23 +02:00
|
|
|
_, err = s3.PutObject(vars["bucketName"], handler.Filename, file, contentTypeOctetStream)
|
2017-03-30 22:48:27 +02:00
|
|
|
if err != nil {
|
2017-04-09 16:28:57 +02:00
|
|
|
handleHTTPError(w, http.StatusInternalServerError, err)
|
2017-03-30 22:48:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|