Browse Source

serve image on get works

master
arnaucode 7 years ago
parent
commit
de82886187
1 changed files with 36 additions and 1 deletions
  1. +36
    -1
      handlers.go

+ 36
- 1
handlers.go

@ -2,8 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"image"
"image/jpeg"
"image/png"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -15,7 +20,36 @@ func Index(w http.ResponseWriter, r *http.Request) {
func ImageShow(w http.ResponseWriter, r *http.Request) { func ImageShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
imageName := vars["imageName"] 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) { func NewImage(w http.ResponseWriter, r *http.Request) {
@ -32,4 +66,5 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
fmt.Println(err) fmt.Println(err)
} }
fmt.Fprintln(w, "url:", handler.Filename)
} }

Loading…
Cancel
Save