diff --git a/handlers.go b/handlers.go index 79a875a..993e47c 100644 --- a/handlers.go +++ b/handlers.go @@ -2,8 +2,13 @@ package main import ( "fmt" + "image" + "image/jpeg" + "image/png" "io/ioutil" "net/http" + "os" + "strings" "github.com/gorilla/mux" ) @@ -15,7 +20,36 @@ func Index(w http.ResponseWriter, r *http.Request) { func ImageShow(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) imageName := vars["imageName"] - fmt.Fprintln(w, "Image show:", imageName) + //fmt.Fprintln(w, "Image show:", imageName) + var imageExtension = strings.Split(imageName, ".")[1] + + file, err := os.Open(imageName) + if err != nil { + //log.Fatal(err) + //fmt.Fprintln(w, "la imatge no existeix al server") + fmt.Fprintln(w, err) + } + + var img image.Image + switch imageExtension { + case "png": + img, err = png.Decode(file) + case "jpg": + img, err = jpeg.Decode(file) + case "jpeg": + img, err = jpeg.Decode(file) + default: + img = nil + } + + if err != nil { + //log.Fatal(err) + fmt.Fprintln(w, "la imatge no existeix al server") + } else { + file.Close() + + jpeg.Encode(w, img, nil) // Write to the ResponseWriter + } } func NewImage(w http.ResponseWriter, r *http.Request) { @@ -32,4 +66,5 @@ func NewImage(w http.ResponseWriter, r *http.Request) { fmt.Println(err) } + fmt.Fprintln(w, "url:", handler.Filename) }