-
-
Notifications
You must be signed in to change notification settings - Fork 402
Adds support for HTTP URLs for documents, audio, images, and sticky notes… #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,18 +177,18 @@ func (s *server) authalice(next http.Handler) http.Handler { | |
| log.Debug().Str("userId", txtid).Bool("historyValid", history.Valid).Int64("historyValue", history.Int64).Str("historyStr", historyStr).Msg("User authentication - history debug") | ||
|
|
||
| v := Values{map[string]string{ | ||
| "Id": txtid, | ||
| "Name": name, | ||
| "Jid": jid, | ||
| "Webhook": webhook, | ||
| "Token": token, | ||
| "Proxy": proxy_url, | ||
| "Events": events, | ||
| "Qrcode": qrcode, | ||
| "History": historyStr, | ||
| "HasHmac": strconv.FormatBool(hasHmac), | ||
| "S3Enabled": s3Enabled, | ||
| "MediaDelivery": mediaDelivery, | ||
| "Id": txtid, | ||
| "Name": name, | ||
| "Jid": jid, | ||
| "Webhook": webhook, | ||
| "Token": token, | ||
| "Proxy": proxy_url, | ||
| "Events": events, | ||
| "Qrcode": qrcode, | ||
| "History": historyStr, | ||
| "HasHmac": strconv.FormatBool(hasHmac), | ||
| "S3Enabled": s3Enabled, | ||
| "MediaDelivery": mediaDelivery, | ||
| }} | ||
|
|
||
| userinfocache.Set(token, v, cache.NoExpiration) | ||
|
|
@@ -813,13 +813,13 @@ func (s *server) GetStatus() http.HandlerFunc { | |
| func (s *server) SendDocument() http.HandlerFunc { | ||
|
|
||
| type documentStruct struct { | ||
| Caption string | ||
| Phone string | ||
| Document string | ||
| FileName string | ||
| Id string | ||
| MimeType string | ||
| ContextInfo waE2E.ContextInfo | ||
| Caption string | ||
| Phone string | ||
| Document string | ||
| FileName string | ||
| Id string | ||
| MimeType string | ||
| ContextInfo waE2E.ContextInfo | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| } | ||
|
|
||
|
|
@@ -874,21 +874,36 @@ func (s *server) SendDocument() http.HandlerFunc { | |
| var uploaded whatsmeow.UploadResponse | ||
| var filedata []byte | ||
|
|
||
| if t.Document[0:29] == "data:application/octet-stream" { | ||
| if strings.HasPrefix(t.Document, "data:") { | ||
| var dataURL, err = dataurl.DecodeString(t.Document) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("could not decode base64 encoded data from payload")) | ||
| return | ||
| } else { | ||
| filedata = dataURL.Data | ||
| uploaded, err = clientManager.GetWhatsmeowClient(txtid).Upload(context.Background(), filedata, whatsmeow.MediaDocument) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("failed to upload file: %v", err))) | ||
| return | ||
| } | ||
| } | ||
| filedata = dataURL.Data | ||
| } else if isHTTPURL(t.Document) { | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Document, fetchDocumentMaxBytes) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to fetch document from url: %v", err))) | ||
| return | ||
| } | ||
| if t.MimeType == "" { | ||
| t.MimeType = ct | ||
| } | ||
| filedata = data | ||
| } else { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("document data should start with \"data:application/octet-stream;base64,\"")) | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("document data should start with \"data:\" or be a valid HTTP URL")) | ||
| return | ||
| } | ||
|
|
||
| uploaded, err = mediaCache.GetOrUploadDocument(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, func() string { | ||
| if t.MimeType != "" { | ||
| return t.MimeType | ||
| } | ||
| return http.DetectContentType(filedata) | ||
| }()) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("failed to upload file: %v", err))) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -973,15 +988,15 @@ func (s *server) SendDocument() http.HandlerFunc { | |
| func (s *server) SendAudio() http.HandlerFunc { | ||
|
|
||
| type audioStruct struct { | ||
| Phone string | ||
| Audio string | ||
| Caption string | ||
| Id string | ||
| PTT *bool `json:"ptt,omitempty"` | ||
| MimeType string `json:"mimetype,omitempty"` | ||
| Seconds uint32 | ||
| Waveform []byte | ||
| ContextInfo waE2E.ContextInfo | ||
| Phone string | ||
| Audio string | ||
| Caption string | ||
| Id string | ||
| PTT *bool `json:"ptt,omitempty"` | ||
| MimeType string `json:"mimetype,omitempty"` | ||
| Seconds uint32 | ||
| Waveform []byte | ||
| ContextInfo waE2E.ContextInfo | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| } | ||
|
|
||
|
|
@@ -1035,16 +1050,29 @@ func (s *server) SendAudio() http.HandlerFunc { | |
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("could not decode base64 encoded data from payload")) | ||
| return | ||
| } else { | ||
| filedata = dataURL.Data | ||
| uploaded, err = clientManager.GetWhatsmeowClient(txtid).Upload(context.Background(), filedata, whatsmeow.MediaAudio) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("failed to upload file: %v", err))) | ||
| return | ||
| } | ||
| filedata = dataURL.Data | ||
| } else if isHTTPURL(t.Audio) { | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Audio, fetchAudioMaxBytes) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to fetch audio from url: %v", err))) | ||
| return | ||
| } | ||
| if t.MimeType == "" { | ||
| if strings.HasPrefix(strings.ToLower(ct), "audio/") { | ||
| mime = ct | ||
| } | ||
| // else mantém o default já definido (ogg ou mpeg) | ||
| } | ||
| filedata = data | ||
| } else { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("audio data should start with \"data:audio/\"")) | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New("audio data should start with \"data:audio/\" or be a valid HTTP URL")) | ||
| return | ||
| } | ||
|
|
||
| uploaded, err = mediaCache.GetOrUploadAudio(context.Background(), clientManager.GetWhatsmeowClient(txtid), filedata, mime) | ||
|
Comment on lines
+1063
to
+1073
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block contains a critical issue: the variable Furthermore, the mime type resolution logic (including the defaults based on the PTT setting) should be executed before the |
||
| if err != nil { | ||
| s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("failed to upload file: %v", err))) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -1208,7 +1236,7 @@ func (s *server) SendImage() http.HandlerFunc { | |
| filedata = dataURL.Data | ||
| } | ||
| } else if isHTTPURL(t.Image) { | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Image, openGraphImageMaxBytes) | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Image, fetchImageMaxBytes) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to fetch image from url: %v", err))) | ||
| return | ||
|
|
@@ -1402,6 +1430,20 @@ func (s *server) SendSticker() http.HandlerFunc { | |
| msgid = t.Id | ||
| } | ||
|
|
||
| if isHTTPURL(t.Sticker) { | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Sticker, fetchImageMaxBytes) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to fetch sticker from url: %v", err))) | ||
| return | ||
| } | ||
| mimeType := ct | ||
| if !strings.HasPrefix(strings.ToLower(mimeType), "image/") { | ||
| mimeType = "image/webp" | ||
| } | ||
| imgDataURL := dataurl.New(data, mimeType) | ||
| t.Sticker = imgDataURL.String() | ||
| } | ||
|
|
||
| processedData, detectedMimeType, err := processStickerData( | ||
| t.Sticker, | ||
| t.MimeType, | ||
|
|
@@ -1566,7 +1608,7 @@ func (s *server) SendVideo() http.HandlerFunc { | |
|
|
||
| } | ||
| } else if isHTTPURL(t.Video) { | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Video, openGraphImageMaxBytes) | ||
| data, ct, err := fetchURLBytes(r.Context(), t.Video, fetchVideoMaxBytes) | ||
| if err != nil { | ||
| s.Respond(w, r, http.StatusBadRequest, errors.New(fmt.Sprintf("failed to fetch image from url: %v", err))) | ||
| return | ||
|
|
@@ -1675,11 +1717,11 @@ func (s *server) SendVideo() http.HandlerFunc { | |
| func (s *server) SendContact() http.HandlerFunc { | ||
|
|
||
| type contactStruct struct { | ||
| Phone string | ||
| Id string | ||
| Name string | ||
| Vcard string | ||
| ContextInfo waE2E.ContextInfo | ||
| Phone string | ||
| Id string | ||
| Name string | ||
| Vcard string | ||
| ContextInfo waE2E.ContextInfo | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| } | ||
|
|
||
|
|
@@ -1797,12 +1839,12 @@ func (s *server) SendContact() http.HandlerFunc { | |
| func (s *server) SendLocation() http.HandlerFunc { | ||
|
|
||
| type locationStruct struct { | ||
| Phone string | ||
| Id string | ||
| Name string | ||
| Latitude float64 | ||
| Longitude float64 | ||
| ContextInfo waE2E.ContextInfo | ||
| Phone string | ||
| Id string | ||
| Name string | ||
| Latitude float64 | ||
| Longitude float64 | ||
| ContextInfo waE2E.ContextInfo | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| } | ||
|
|
||
|
|
@@ -2244,8 +2286,8 @@ func (s *server) SendMessage() http.HandlerFunc { | |
| LinkPreview bool | ||
| Id string | ||
| ContextInfo waE2E.ContextInfo | ||
| QuotedText string `json:"QuotedText,omitempty"` | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| QuotedText string `json:"QuotedText,omitempty"` | ||
| QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"` | ||
| } | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| txtid := r.Context().Value("userinfo").(Values).Get("Id") | ||
|
|
@@ -6701,14 +6743,14 @@ func (s *server) publishSentMessageEvent(token, userID, txtid string, recipient | |
| var recipientLID types.JID | ||
| if client.Store != nil && client.Store.LIDs != nil { | ||
| ctx := context.Background() | ||
|
|
||
| // Get sender LID | ||
| if !senderJID.IsEmpty() { | ||
| if lid, err := client.Store.LIDs.GetLIDForPN(ctx, senderJID); err == nil && !lid.IsEmpty() { | ||
| senderLID = lid | ||
| } | ||
| } | ||
|
|
||
| // Get recipient LID (only for non-group chats) | ||
| if !isGroup && !recipient.IsEmpty() { | ||
| if lid, err := client.Store.LIDs.GetLIDForPN(ctx, recipient); err == nil && !lid.IsEmpty() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
r.Context()instead ofcontext.Background()when callingmediaCache.GetOrUploadDocument. This allows the upload process to be cancelled if the HTTP request is closed by the client, which is a best practice for handling long-running operations in web handlers.