Allow to delete bucket

This commit is contained in:
Lena Fuhrimann 2016-12-21 22:29:12 +01:00
parent 6f7c8439db
commit b2dce0b8ce
4 changed files with 101 additions and 24 deletions

View file

@ -11,6 +11,14 @@ import (
"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"`
}
// createBucketHandler creates a new bucket
func createBucketHandler(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
@ -41,6 +49,18 @@ func createBucketHandler(w http.ResponseWriter, r *http.Request) {
}
}
// 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)
}
// getObjectHandler downloads an object to the client
func getObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -66,24 +86,57 @@ func getObjectHandler(w http.ResponseWriter, r *http.Request) {
func createObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := r.ParseMultipartForm(32 << 20)
if err != nil {
panic(err)
}
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
}
file, handler, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
defer file.Close()
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
}
_, err = minioClient.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
if err != nil {
panic(err)
}
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)
}
w.WriteHeader(http.StatusCreated)
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)
}
}
// deleteObjectHandler deletes an object

View file

@ -38,6 +38,12 @@ var routes = Routes{
"/api/buckets",
createBucketHandler,
},
Route{
"Delete Bucket",
"DELETE",
"/api/buckets/{bucketName}",
deleteBucketHandler,
},
Route{
"Download Object",
"GET",

View file

@ -2,11 +2,16 @@
<nav class="purple" role="navigation">
<div class="nav-wrapper container">
<span href="#" class="brand-logo"><i class="material-icons">folder_open</i>{{ .BucketName }}</span>
{{ if not .Objects }}
<a href="#" class="right" onclick="deleteBucket({{ .BucketName }})"><i class="material-icons">delete</i> Delete</a>
{{ end }}
</div>
</nav>
<div class="section">
<a href="/buckets" style="padding-left: 25px; vertical-align: middle;"><i class="material-icons" style="vertical-align: middle;">arrow_back</i> Buckets</a>
{{ if .Objects }}
<table class="highlight bordered">
<thead>
@ -23,7 +28,7 @@
<tbody>
{{ range $index, $object := .Objects }}
<tr>
<td><i class="material-icons">{{ $object.Icon }}</i></td>
<td style="padding-left: 25px;"><i class="material-icons">{{ $object.Icon }}</i></td>
<td>{{ $object.Key }}</td>
<td>{{ $object.Size }} bytes</td>
<td>{{ $object.Owner }}</td>
@ -37,7 +42,7 @@
<!-- Dropdown Structure -->
<ul id="actions-dropdown-{{ $index }}" class="dropdown-content">
<li><a href="/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li>
<li><a href="#" onclick="deleteObject({{ $object.Key }})">Delete</a></li>
<li><a href="#" onclick="deleteObject({{ $.BucketName }}, {{ $object.Key }})">Delete</a></li>
</ul>
</td>
</tr>
@ -45,11 +50,11 @@
</tbody>
</table>
{{ end }}
{{ if not .Objects }}
<p style="text-align: center;margin-top: 2em;">Oh noes... No objects in <strong>{{ .BucketName }}</strong> yet...</p>
<p style="text-align: center;margin-top: 2em;">No objects in <strong>{{ .BucketName }}</strong> yet</p>
{{ end }}
<p></p>
</div>
@ -86,12 +91,19 @@
</div>
<script>
function deleteObject(objectName) {
function deleteObject(bucketName, objectName) {
$.ajax({
type: 'DELETE',
url: '/api/buckets/{{ .BucketName }}/objects/' + objectName,
url: '/api/buckets/' + bucketName + '/objects/' + objectName,
success: function () { location.reload(); }
})
}
function deleteBucket(bucketName) {
$.ajax({
type: 'DELETE',
url: '/api/buckets/' + bucketName,
success: function () { window.location.replace('/buckets'); }
})
}
</script>
{{ end }}

View file

@ -9,6 +9,7 @@
<div class="section">
<div class="row">
{{ if . }}
{{ range $bucket := . }}
<div class="col m12 l6">
<a href="/buckets/{{ $bucket.Name }}" style="color: black;">
@ -23,13 +24,18 @@
{{ $bucket.Name }}
</span>
<p style="color: gray;">Created on {{ $bucket.CreationDate }}</p>
</div>
</div>
</div>
</div>
</div>
</a>
</div>
{{ end }}
{{ end }}
{{ if not . }}
<p style="text-align: center;margin-top: 2em;">No buckets yet</p>
{{ end }}
</div>
</div>
@ -47,7 +53,7 @@
<br>
<div class="row">
<div class="col s6">
<form id="create-bucket-form" onsubmit="createBucket()">
<form id="create-bucket-form">
<div class="input-field">
<input placeholder="My Bucket" id="name" type="text" name="name">
<label for="name">Name</label>
@ -57,7 +63,7 @@
</div>
</div>
<div class="modal-footer">
<button type="submit" form="create-bucket-form" class="modal-action modal-close waves-effect waves-green btn-flat">Create</button>
<button type="button" onclick="createBucket()" class="modal-action modal-close waves-effect waves-green btn-flat">Create</button>
<button class="modal-action modal-close waves-effect waves-green btn-flat">Cancel</button>
</div>
</div>