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.

64 lines
1.5 KiB

  1. package Messages
  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/Models"
  8. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util"
  9. "github.com/gorilla/mux"
  10. )
  11. // AddConversationImage adds an image for a conversation icon
  12. func AddConversationImage(w http.ResponseWriter, r *http.Request) {
  13. var (
  14. attachment Models.Attachment
  15. conversationDetail Models.ConversationDetail
  16. urlVars map[string]string
  17. detailID string
  18. decodedFile []byte
  19. fileName string
  20. ok bool
  21. err error
  22. )
  23. urlVars = mux.Vars(r)
  24. detailID, ok = urlVars["detailID"]
  25. if !ok {
  26. http.Error(w, "Not Found", http.StatusNotFound)
  27. return
  28. }
  29. conversationDetail, err = Database.GetConversationDetailByID(detailID)
  30. if err != nil {
  31. http.Error(w, "Not Found", http.StatusNotFound)
  32. return
  33. }
  34. err = json.NewDecoder(r.Body).Decode(&attachment)
  35. if err != nil {
  36. http.Error(w, "Error", http.StatusInternalServerError)
  37. return
  38. }
  39. if attachment.Data == "" {
  40. http.Error(w, "Error", http.StatusInternalServerError)
  41. return
  42. }
  43. decodedFile, err = base64.StdEncoding.DecodeString(attachment.Data)
  44. fileName, err = Util.WriteFile(decodedFile)
  45. attachment.FilePath = fileName
  46. conversationDetail.Attachment = attachment
  47. err = Database.UpdateConversationDetail(&conversationDetail)
  48. if err != nil {
  49. http.Error(w, "Error", http.StatusInternalServerError)
  50. return
  51. }
  52. w.WriteHeader(http.StatusNoContent)
  53. }