The new Ripple frontend.
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.

avatar.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "fmt"
  4. "image"
  5. _ "image/gif"
  6. _ "image/jpeg"
  7. "image/png"
  8. "os"
  9. "github.com/gin-gonic/gin"
  10. "github.com/nfnt/resize"
  11. )
  12. func avatarSubmit(c *gin.Context) {
  13. ctx := getContext(c)
  14. if ctx.User.ID == 0 {
  15. resp403(c)
  16. return
  17. }
  18. var m message
  19. defer func() {
  20. simpleReply(c, m)
  21. }()
  22. if config.AvatarsFolder == "" {
  23. m = errorMessage{T(c, "Changing avatar is currently not possible.")}
  24. return
  25. }
  26. file, _, err := c.Request.FormFile("avatar")
  27. if err != nil {
  28. m = errorMessage{T(c, "An error occurred.")}
  29. return
  30. }
  31. img, _, err := image.Decode(file)
  32. if err != nil {
  33. m = errorMessage{T(c, "An error occurred.")}
  34. return
  35. }
  36. img = resize.Thumbnail(256, 256, img, resize.Bilinear)
  37. f, err := os.Create(fmt.Sprintf("%s/%d.png", config.AvatarsFolder, ctx.User.ID))
  38. defer f.Close()
  39. if err != nil {
  40. m = errorMessage{T(c, "An error occurred.")}
  41. c.Error(err)
  42. return
  43. }
  44. err = png.Encode(f, img)
  45. if err != nil {
  46. m = errorMessage{T(c, "We were not able to save your avatar.")}
  47. c.Error(err)
  48. return
  49. }
  50. m = successMessage{T(c, "Your avatar was successfully changed. It may take some time to properly update. To force a cache refresh, you can use CTRL+F5.")}
  51. }