mirror of
https://github.com/arnaucube/goImgServer.git
synced 2026-02-07 03:26:43 +01:00
recieve an image and stores it to a file
This commit is contained in:
30
handlers.go
30
handlers.go
@@ -1,9 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -21,29 +19,17 @@ func ImageShow(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewImage(w http.ResponseWriter, r *http.Request) {
|
func NewImage(w http.ResponseWriter, r *http.Request) {
|
||||||
var img ImageModel
|
file, handler, err := r.FormFile("file")
|
||||||
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
if err := r.Body.Close(); err != nil {
|
data, err := ioutil.ReadAll(file)
|
||||||
panic(err)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(body))
|
err = ioutil.WriteFile(handler.Filename, data, 0777)
|
||||||
if err := json.Unmarshal(body, &img); err != nil {
|
if err != nil {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
fmt.Println(err)
|
||||||
w.WriteHeader(422) // unprocessable entity
|
|
||||||
if err := json.NewEncoder(w).Encode(err); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
img.File = body
|
|
||||||
fmt.Println(img)
|
|
||||||
fmt.Println(img.File)
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
if err := json.NewEncoder(w).Encode(img.File); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user