Use mautrix-go methods for downloading media

This commit is contained in:
Tulir Asokan 2020-04-22 12:31:54 +03:00
parent 2d1b3bd7f0
commit 19fe067439
3 changed files with 20 additions and 23 deletions

2
go.mod
View File

@ -21,7 +21,7 @@ require (
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.8 gopkg.in/yaml.v2 v2.2.8
maunium.net/go/mautrix v0.2.0-beta.3 maunium.net/go/mautrix v0.2.0-beta.4
maunium.net/go/mauview v0.1.0 maunium.net/go/mauview v0.1.0
maunium.net/go/tcell v0.1.0 maunium.net/go/tcell v0.1.0
) )

2
go.sum
View File

@ -75,6 +75,8 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
maunium.net/go/mautrix v0.2.0-beta.3 h1:pm5MQszvzYaLyCEGjyUau4DvzrOw5dT45UAvDLz5zBo= maunium.net/go/mautrix v0.2.0-beta.3 h1:pm5MQszvzYaLyCEGjyUau4DvzrOw5dT45UAvDLz5zBo=
maunium.net/go/mautrix v0.2.0-beta.3/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8= maunium.net/go/mautrix v0.2.0-beta.3/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
maunium.net/go/mautrix v0.2.0-beta.4 h1:L7Jpc+8GGc+Qo0DdamACEeU1Ci9G1mergJpsTTgDOUA=
maunium.net/go/mautrix v0.2.0-beta.4/go.mod h1:WeTUYKrM3/4LZK2bXQ9NRIXnRWKsa+6+OA1gw0nf5G8=
maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg= maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg=
maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U= maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U=
maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM= maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM=

View File

@ -26,7 +26,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -37,18 +36,18 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/matrix/muksevt"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format" "maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/pushrules"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/matrix/muksevt"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix/pushrules"
) )
// Container is a wrapper for a mautrix Client and some other stuff. // Container is a wrapper for a mautrix Client and some other stuff.
@ -301,11 +300,12 @@ func init() {
} }
type StubSyncingModal struct{} type StubSyncingModal struct{}
func (s StubSyncingModal) SetIndeterminate() {}
func (s StubSyncingModal) SetIndeterminate() {}
func (s StubSyncingModal) SetMessage(s2 string) {} func (s StubSyncingModal) SetMessage(s2 string) {}
func (s StubSyncingModal) SetSteps(i int) {} func (s StubSyncingModal) SetSteps(i int) {}
func (s StubSyncingModal) Step() {} func (s StubSyncingModal) Step() {}
func (s StubSyncingModal) Close() {} func (s StubSyncingModal) Close() {}
// OnLogin initializes the syncer and updates the room list. // OnLogin initializes the syncer and updates the room list.
func (c *Container) OnLogin() { func (c *Container) OnLogin() {
@ -1009,14 +1009,14 @@ func (c *Container) DownloadToDisk(uri id.ContentURI, target string) (fullPath s
} }
defer file.Close() defer file.Close()
var resp *http.Response var body io.ReadCloser
resp, err = c.client.Client.Get(c.GetDownloadURL(uri)) body, err = c.client.Download(uri)
if err != nil { if err != nil {
return return
} }
defer resp.Body.Close() defer body.Close()
_, err = io.Copy(file, resp.Body) _, err = io.Copy(file, body)
if err != nil { if err != nil {
return return
} }
@ -1051,24 +1051,19 @@ func (c *Container) Download(uri id.ContentURI) (data []byte, err error) {
} }
func (c *Container) GetDownloadURL(uri id.ContentURI) string { func (c *Container) GetDownloadURL(uri id.ContentURI) string {
dlURL, _ := url.Parse(c.client.HomeserverURL.String()) return c.client.GetDownloadURL(uri)
if dlURL.Scheme == "" {
dlURL.Scheme = "https"
}
dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/r0/download", uri.Homeserver, uri.FileID)
return dlURL.String()
} }
func (c *Container) download(uri id.ContentURI, cacheFile string) (data []byte, err error) { func (c *Container) download(uri id.ContentURI, cacheFile string) (data []byte, err error) {
var resp *http.Response var body io.ReadCloser
resp, err = c.client.Client.Get(c.GetDownloadURL(uri)) body, err = c.client.Download(uri)
if err != nil { if err != nil {
return return
} }
defer resp.Body.Close() defer body.Close()
var buf bytes.Buffer var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body) _, err = io.Copy(&buf, body)
if err != nil { if err != nil {
return return
} }