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.

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