Encrypted messaging app
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.

59 lines
1.2 KiB

  1. package Auth
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
  8. )
  9. // AddProfileImage adds a profile image
  10. func AddProfileImage(w http.ResponseWriter, r *http.Request) {
  11. var (
  12. user Database.User
  13. attachment Database.Attachment
  14. decodedFile []byte
  15. fileName string
  16. err error
  17. )
  18. // Ignore error here, as middleware should handle auth
  19. user, _ = CheckCookieCurrentUser(w, r)
  20. err = json.NewDecoder(r.Body).Decode(&attachment)
  21. if err != nil {
  22. http.Error(w, "Error", http.StatusInternalServerError)
  23. return
  24. }
  25. if attachment.Data == "" {
  26. http.Error(w, "Error", http.StatusInternalServerError)
  27. return
  28. }
  29. decodedFile, err = base64.StdEncoding.DecodeString(attachment.Data)
  30. if err != nil {
  31. http.Error(w, "Error", http.StatusInternalServerError)
  32. return
  33. }
  34. fileName, err = Util.WriteFile(decodedFile)
  35. if err != nil {
  36. http.Error(w, "Error", http.StatusInternalServerError)
  37. return
  38. }
  39. attachment.FilePath = fileName
  40. user.Attachment = attachment
  41. err = (&user).UpdateUser()
  42. if err != nil {
  43. http.Error(w, "Error", http.StatusInternalServerError)
  44. return
  45. }
  46. w.WriteHeader(http.StatusNoContent)
  47. }