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" "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 // createBucketHandler creates a new bucket
func createBucketHandler(w http.ResponseWriter, r *http.Request) { func createBucketHandler(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo 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 // getObjectHandler downloads an object to the client
func getObjectHandler(w http.ResponseWriter, r *http.Request) { func getObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
@ -66,24 +86,57 @@ func getObjectHandler(w http.ResponseWriter, r *http.Request) {
func createObjectHandler(w http.ResponseWriter, r *http.Request) { func createObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
err := r.ParseMultipartForm(32 << 20) if r.Header.Get("Content-Type") == "application/json" {
if err != nil { var copy CopyObjectInfo
panic(err) 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") var copyConds = minio.NewCopyConditions()
if err != nil { objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
w.WriteHeader(http.StatusUnprocessableEntity) fmt.Println(copy)
return fmt.Println(objectSource)
} err = minioClient.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
defer file.Close() if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = minioClient.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err != nil { w.WriteHeader(http.StatusCreated)
panic(err) 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 // deleteObjectHandler deletes an object

View file

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

View file

@ -2,11 +2,16 @@
<nav class="purple" role="navigation"> <nav class="purple" role="navigation">
<div class="nav-wrapper container"> <div class="nav-wrapper container">
<span href="#" class="brand-logo"><i class="material-icons">folder_open</i>{{ .BucketName }}</span> <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> </div>
</nav> </nav>
<div class="section"> <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"> <table class="highlight bordered">
<thead> <thead>
@ -23,7 +28,7 @@
<tbody> <tbody>
{{ range $index, $object := .Objects }} {{ range $index, $object := .Objects }}
<tr> <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.Key }}</td>
<td>{{ $object.Size }} bytes</td> <td>{{ $object.Size }} bytes</td>
<td>{{ $object.Owner }}</td> <td>{{ $object.Owner }}</td>
@ -37,7 +42,7 @@
<!-- Dropdown Structure --> <!-- Dropdown Structure -->
<ul id="actions-dropdown-{{ $index }}" class="dropdown-content"> <ul id="actions-dropdown-{{ $index }}" class="dropdown-content">
<li><a href="/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li> <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> </ul>
</td> </td>
</tr> </tr>
@ -45,11 +50,11 @@
</tbody> </tbody>
</table> </table>
{{ end }}
{{ if not .Objects }} {{ 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 }} {{ end }}
<p></p>
</div> </div>
@ -86,12 +91,19 @@
</div> </div>
<script> <script>
function deleteObject(objectName) { function deleteObject(bucketName, objectName) {
$.ajax({ $.ajax({
type: 'DELETE', type: 'DELETE',
url: '/api/buckets/{{ .BucketName }}/objects/' + objectName, url: '/api/buckets/' + bucketName + '/objects/' + objectName,
success: function () { location.reload(); } success: function () { location.reload(); }
}) })
} }
function deleteBucket(bucketName) {
$.ajax({
type: 'DELETE',
url: '/api/buckets/' + bucketName,
success: function () { window.location.replace('/buckets'); }
})
}
</script> </script>
{{ end }} {{ end }}

View file

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