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.

62 lines
1.2 KiB

  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(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. file, handler, err := r.FormFile("file")
  29. if err != nil {
  30. fmt.Println(err)
  31. }
  32. imageName := strings.Split(handler.Filename, ".")[0]
  33. fileName := imageName + ".png"
  34. data, err := ioutil.ReadAll(file)
  35. if err != nil {
  36. fmt.Println(err)
  37. }
  38. img, err := dataToImage(data, handler.Filename)
  39. if err != nil {
  40. fmt.Fprintln(w, "error al processar la imatge")
  41. }
  42. img = Resize(img)
  43. data, err = imageToPNG(img)
  44. if err != nil {
  45. fmt.Fprintln(w, "error al processar la imatge")
  46. }
  47. err = ioutil.WriteFile(fileName, data, 0777)
  48. if err != nil {
  49. fmt.Println(err)
  50. }
  51. fmt.Fprintln(w, "url:", fileName)
  52. }