Commit a094f561 authored by Benjamin Rokseth's avatar Benjamin Rokseth
Browse files

Implement POST and DELETE image

parent 8806185d
......@@ -84,7 +84,29 @@ type Image struct {
func (s *server) getImage(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
w.Write([]byte(id))
info, err := s.minio.StatObject("images", id, minio.StatObjectOptions{})
if err != nil {
if info.Size == 0 {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
obj, err := s.minio.GetObject("images", id, minio.GetObjectOptions{})
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
log.Printf("%+v", obj)
res, err := ioutil.ReadAll(obj)
if err != nil {
fmt.Println(err)
return
}
w.Write(res)
}
func (s *server) uploadImage(w http.ResponseWriter, r *http.Request) {
......@@ -105,7 +127,6 @@ func (s *server) uploadImage(w http.ResponseWriter, r *http.Request) {
return
}
filetype := http.DetectContentType(fileBytes)
fmt.Println(filetype)
if filetype != "image/jpeg" && filetype != "image/jpg" &&
filetype != "image/gif" && filetype != "image/png" &&
filetype != "application/pdf" && filetype != "image/svg" {
......@@ -133,15 +154,17 @@ func (s *server) uploadImage(w http.ResponseWriter, r *http.Request) {
// upload to minio
n, err := s.minio.PutObject("images", img.Name, img.File, img.Size, minio.PutObjectOptions{
ContentType: filetype,
UserMetadata: map[string]string{"Foo": "Bar"}, // TODO: Add metadata from params
UserMetadata: map[string]string{"X-FOO-TYPE": "Bar"}, // TODO: Add metadata from params
})
if err != nil {
log.Println(err)
log.Printf("Failed upload: %s", err)
http.Error(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
}
log.Printf("SUCCESS: Wrote %d bytes to storage\n", n)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", img.URL)
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(img); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
......@@ -149,14 +172,30 @@ func (s *server) uploadImage(w http.ResponseWriter, r *http.Request) {
// TODO UPDATE
func (s *server) updateImage(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
w.Write([]byte(id))
//id := chi.URLParam(r, "id")
w.Write([]byte("Not implemented"))
}
// TODO DELETE
func (s *server) deleteImage(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
w.Write([]byte(id))
// perhaps GetObject instead?
info, err := s.minio.StatObject("images", id, minio.StatObjectOptions{})
if err != nil {
if info.Size == 0 {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
} else {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
fmt.Println(info)
if err := s.minio.RemoveObject("images", id); err != nil {
log.Printf("Could not remove object: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Write([]byte("Deleted OK."))
}
func randID(l int) string {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment