package Messages import ( "encoding/base64" "encoding/json" "net/http" "time" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" ) type rawMessageData struct { MessageData Database.MessageData `json:"message_data"` Messages Database.MessageList `json:"message"` } // CreateMessage sends a message func CreateMessage(w http.ResponseWriter, r *http.Request) { var ( messagesData []rawMessageData messageData rawMessageData message Database.Message t time.Time decodedFile []byte fileName string i int err error ) err = json.NewDecoder(r.Body).Decode(&messagesData) if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } for _, messageData = range messagesData { if messageData.MessageData.Data == "" { decodedFile, err = base64.StdEncoding.DecodeString(messageData.MessageData.Attachment.Data) fileName, err = Util.WriteFile(decodedFile) messageData.MessageData.Attachment.FilePath = fileName } for i, message = range messageData.Messages { t, err = time.Parse("2006-01-02T15:04:05Z", message.ExpiryRaw) if err == nil { err = messageData.Messages[i].Expiry.Scan(t) if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } } } err = (&messageData.MessageData).CreateMessageData() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } err = (&messageData.Messages).CreateMessages() if err != nil { http.Error(w, "Error", http.StatusInternalServerError) return } } w.WriteHeader(http.StatusNoContent) }