@@ -472,6 +472,25 @@ func (b *Bmatrix) handleReply(ev *matrix.Event, rmsg config.Message) bool {
472472 return true
473473}
474474
475+ func (b * Bmatrix ) handleAttachment (ev * matrix.Event , rmsg config.Message ) bool {
476+ if ! b .containsAttachment (ev .Content ) {
477+ return false
478+ }
479+
480+ go func () {
481+ // File download is processed in the background to avoid stalling
482+ err := b .handleDownloadFile (& rmsg , ev .Content )
483+ if err != nil {
484+ b .Log .Errorf ("%#v" , err )
485+ return
486+ }
487+
488+ b .Remote <- rmsg
489+ }()
490+
491+ return true
492+ }
493+
475494func (b * Bmatrix ) handleMemberChange (ev * matrix.Event ) {
476495 // Update the displayname on join messages, according to https://matrix.org/docs/spec/client_server/r0.6.1#events-on-change-of-profile-information
477496 if ev .Content ["membership" ] == "join" {
@@ -539,12 +558,10 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {
539558 return
540559 }
541560
542- // Do we have attachments
543- if b .containsAttachment (ev .Content ) {
544- err := b .handleDownloadFile (& rmsg , ev .Content )
545- if err != nil {
546- b .Log .Errorf ("download failed: %#v" , err )
547- }
561+ // Do we have an attachment
562+ // TODO: does matrix support multiple attachments?
563+ if b .handleAttachment (ev , rmsg ) {
564+ return
548565 }
549566
550567 b .Log .Debugf ("<= Sending message from %s on %s to gateway" , ev .Sender , b .Account )
@@ -570,7 +587,10 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in
570587 if url , ok = content ["url" ].(string ); ! ok {
571588 return fmt .Errorf ("url isn't a %T" , url )
572589 }
573- url = strings .ReplaceAll (url , "mxc://" , b .GetString ("Server" )+ "/_matrix/media/v1/download/" )
590+ // Matrix downloads now have to be authenticated with an access token
591+ // See https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/3916-authentication-for-media.md
592+ // Also see: https://github.com/matterbridge-org/matterbridge/issues/36
593+ url = strings .ReplaceAll (url , "mxc://" , b .GetString ("Server" )+ "/_matrix/client/v1/media/download/" )
574594
575595 if info , ok = content ["info" ].(map [string ]interface {}); ! ok {
576596 return fmt .Errorf ("info isn't a %T" , info )
@@ -601,18 +621,11 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in
601621 }
602622 }
603623
604- // check if the size is ok
605- err := helper . HandleDownloadSize ( b . Log , rmsg , name , int64 ( size ), b . General )
624+ // TODO: add attachment ID?
625+ err := b . AddAttachmentFromURL ( rmsg , name , "" , "" , url )
606626 if err != nil {
607627 return err
608628 }
609- // actually download the file
610- data , err := helper .DownloadFile (url )
611- if err != nil {
612- return fmt .Errorf ("download %s failed %#v" , url , err )
613- }
614- // add the downloaded data to the message
615- helper .HandleDownloadData (b .Log , rmsg , name , "" , url , data , b .General )
616629 return nil
617630}
618631
0 commit comments