You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB

7 years ago
7 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "image/jpeg"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "github.com/gorilla/mux"
  9. )
  10. func Index(w http.ResponseWriter, r *http.Request) {
  11. fmt.Fprintln(w, "Welcome! To send images, go to /image")
  12. }
  13. func ImageShow(w http.ResponseWriter, r *http.Request) {
  14. vars := mux.Vars(r)
  15. imageName := vars["imageName"]
  16. file, err := ioutil.ReadFile(config.Folder + "/" + imageName)
  17. if err != nil {
  18. fmt.Fprintln(w, err)
  19. }
  20. img, err := dataToImage(file, imageName)
  21. if err != nil {
  22. fmt.Fprintln(w, "la imatge no existeix al server")
  23. } else {
  24. jpeg.Encode(w, img, nil) // Write to the ResponseWriter
  25. }
  26. }
  27. func NewImage(w http.ResponseWriter, r *http.Request) {
  28. err := ipFilter(r)
  29. if err != nil {
  30. fmt.Fprintln(w, err.Error())
  31. return
  32. }
  33. file, handler, err := r.FormFile("file")
  34. if err != nil {
  35. fmt.Println(err)
  36. }
  37. imageName := strings.Split(handler.Filename, ".")[0]
  38. fileName := imageName + ".png"
  39. data, err := ioutil.ReadAll(file)
  40. if err != nil {
  41. fmt.Println(err)
  42. }
  43. img, err := dataToImage(data, handler.Filename)
  44. if err != nil {
  45. fmt.Fprintln(w, "error al processar la imatge")
  46. }
  47. img = Resize(img)
  48. data, err = imageToPNG(img)
  49. if err != nil {
  50. fmt.Fprintln(w, "error al processar la imatge")
  51. }
  52. err = ioutil.WriteFile(config.Folder+"/"+fileName, data, 0777)
  53. if err != nil {
  54. fmt.Println(err)
  55. }
  56. fmt.Fprintln(w, config.ServerIP+":"+config.ServerPort+"/images/"+fileName)
  57. }