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.

50 lines
1.1 KiB

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