From 10ca97310edbe797846bbe993cf016cda70bf61b Mon Sep 17 00:00:00 2001 From: Karmanyaah Malhotra Date: Wed, 10 Mar 2021 22:19:17 -0500 Subject: [PATCH] Video support --- ROADMAP.md | 4 +-- groupmeExt/message.go | 22 +++++++++++++++ portal.go | 65 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 8984649..6230b5e 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -26,8 +26,8 @@ * [ ] Formatted messages * [ ] Media/files * [x] Images - * [ ] Videos - * [x] Other Files + * [x] Videos + * [x] Random Files * [ ] Location messages * [ ] Contact messages * [ ] Replies diff --git a/groupmeExt/message.go b/groupmeExt/message.go index 16fa2b7..0d416c8 100644 --- a/groupmeExt/message.go +++ b/groupmeExt/message.go @@ -91,6 +91,28 @@ func DownloadFile(RoomJID types.GroupMeID, FileID string, token string) (content } +func DownloadVideo(previewURL, videoURL, token string) (vidContents []byte, mime string) { + //preview TODO + client := &http.Client{} + + req, _ := http.NewRequest("GET", videoURL, nil) + req.AddCookie(&http.Cookie{Name: "token", Value: token}) + resp, err := client.Do(req) + if err != nil { + fmt.Println(err) + return nil, "" + } + defer resp.Body.Close() + + bytes, _ := ioutil.ReadAll(resp.Body) + mime = resp.Header.Get("Content-Type") + if len(mime) == 0 { + mime = http.DetectContentType(bytes) + } + return bytes, mime + +} + type ImgData struct { FileData struct { FileName string `json:"file_name"` diff --git a/portal.go b/portal.go index 8c41400..a57489f 100644 --- a/portal.go +++ b/portal.go @@ -1260,7 +1260,7 @@ func (portal *Portal) sendMessageDirect(intent *appservice.IntentAPI, eventType } } -func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, ts int64, source *User) (err error, sendText bool) { +func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, source *User, message *groupme.Message) (err error, sendText bool) { sendText = true switch attachment.Type { case "image": @@ -1319,11 +1319,60 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment eventType := event.EventMessage - _, err = portal.sendMessage(intent, eventType, content, ts) + _, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix()) if err != nil { portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err) return nil, true } + case "video": + vidContents, mime := groupmeExt.DownloadVideo(attachment.VideoPreviewURL, attachment.URL, source.Token) + if mime == "" { + mime = mimetype.Detect(vidContents).String() + } + + data, uploadMimeType, file := portal.encryptFile(vidContents, mime) + uploaded, err := portal.uploadWithRetry(intent, data, uploadMimeType, MediaUploadRetries) + if err != nil { + if errors.Is(err, mautrix.MTooLarge) { + err = errors.New("homeserver rejected too large file") + } else if httpErr := err.(mautrix.HTTPError); httpErr.IsStatus(413) { + err = errors.New("proxy rejected too large file") + } else { + err = fmt.Errorf("failed to upload media: %w", err) + } + return err, true + } + + text := strings.Split(attachment.URL, "/") + content := &event.MessageEventContent{ + Body: text[len(text)-1], + File: file, + Info: &event.FileInfo{ + Size: len(data), + MimeType: mime, + //Width: width, + //Height: height, + //Duration: int(msg.length), + }, + } + if content.File != nil { + content.File.URL = uploaded.ContentURI.CUString() + } else { + content.URL = uploaded.ContentURI.CUString() + } + content.MsgType = event.MsgVideo + _, _ = intent.UserTyping(portal.MXID, false, 0) + + eventType := event.EventMessage + + _, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix()) + if err != nil { + portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err) + return nil, true + } + + message.Text = strings.Replace(message.Text, attachment.URL, "", 1) + return nil, true case "file": fileData, fname, fmime := groupmeExt.DownloadFile(portal.Key.JID, attachment.FileID, source.Token) if fmime == "" { @@ -1371,7 +1420,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment eventType := event.EventMessage - _, err = portal.sendMessage(intent, eventType, content, ts) + _, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix()) if err != nil { portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err) return nil, true @@ -1533,13 +1582,9 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) return } - content := &event.MessageEventContent{ - Body: message.Text, - MsgType: event.MsgText, - } sendText := true for _, a := range message.Attachments { - err, text := portal.handleAttachment(intent, a, message.CreatedAt.ToTime().Unix(), source) + err, text := portal.handleAttachment(intent, a, source, message) if err != nil { portal.sendMediaBridgeFailure(source, intent, *message, err) } @@ -1549,6 +1594,10 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) // portal.bridge.Formatter.ParseWhatsApp(content, message.ContextInfo.MentionedJID) // portal.SetReply(content, message.ContextInfo) //TODO: mentions + content := &event.MessageEventContent{ + Body: message.Text, + MsgType: event.MsgText, + } _, _ = intent.UserTyping(portal.MXID, false, 0) if sendText {