|
@ -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 |
|
|
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576)) |
|
|
|
|
|
|
|
|
file, handler, err := r.FormFile("file") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err) |
|
|
|
|
|
|
|
|
fmt.Println(err) |
|
|
} |
|
|
} |
|
|
if err := r.Body.Close(); err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(file) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println(err) |
|
|
} |
|
|
} |
|
|
fmt.Println(string(body)) |
|
|
|
|
|
if err := json.Unmarshal(body, &img); err != nil { |
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8") |
|
|
|
|
|
w.WriteHeader(422) // unprocessable entity
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(err); err != nil { |
|
|
|
|
|
panic(err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(handler.Filename, data, 0777) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println(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) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |