diff options
author | Tulir Asokan <tulir@maunium.net> | 2020-04-22 12:31:54 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2020-04-22 12:31:54 +0300 |
commit | 19fe06743919148529d2388ee57aeed686d73db1 (patch) | |
tree | 7a46148e41c8210cb84cd86553ff978b328093c5 | |
parent | 2d1b3bd7f08ef1ec290e3aca59993e4c36e992db (diff) |
Use mautrix-go methods for downloading media
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | matrix/matrix.go | 39 |
3 files changed, 20 insertions, 23 deletions
@@ -21,7 +21,7 @@ require ( golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 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/tcell v0.1.0 ) @@ -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= 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.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/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U= maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM= diff --git a/matrix/matrix.go b/matrix/matrix.go index 722883d..d0fd2f4 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -26,7 +26,6 @@ import ( "io" "io/ioutil" "net/http" - "net/url" "os" "path" "path/filepath" @@ -37,18 +36,18 @@ import ( "github.com/pkg/errors" - "maunium.net/go/gomuks/lib/open" - "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/pushrules" "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/debug" "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/mautrix/pushrules" ) // Container is a wrapper for a mautrix Client and some other stuff. @@ -301,11 +300,12 @@ func init() { } type StubSyncingModal struct{} -func (s StubSyncingModal) SetIndeterminate() {} + +func (s StubSyncingModal) SetIndeterminate() {} func (s StubSyncingModal) SetMessage(s2 string) {} -func (s StubSyncingModal) SetSteps(i int) {} -func (s StubSyncingModal) Step() {} -func (s StubSyncingModal) Close() {} +func (s StubSyncingModal) SetSteps(i int) {} +func (s StubSyncingModal) Step() {} +func (s StubSyncingModal) Close() {} // OnLogin initializes the syncer and updates the room list. func (c *Container) OnLogin() { @@ -1009,14 +1009,14 @@ func (c *Container) DownloadToDisk(uri id.ContentURI, target string) (fullPath s } defer file.Close() - var resp *http.Response - resp, err = c.client.Client.Get(c.GetDownloadURL(uri)) + var body io.ReadCloser + body, err = c.client.Download(uri) if err != nil { return } - defer resp.Body.Close() + defer body.Close() - _, err = io.Copy(file, resp.Body) + _, err = io.Copy(file, body) if err != nil { return } @@ -1051,24 +1051,19 @@ func (c *Container) Download(uri id.ContentURI) (data []byte, err error) { } func (c *Container) GetDownloadURL(uri id.ContentURI) string { - dlURL, _ := url.Parse(c.client.HomeserverURL.String()) - if dlURL.Scheme == "" { - dlURL.Scheme = "https" - } - dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/r0/download", uri.Homeserver, uri.FileID) - return dlURL.String() + return c.client.GetDownloadURL(uri) } func (c *Container) download(uri id.ContentURI, cacheFile string) (data []byte, err error) { - var resp *http.Response - resp, err = c.client.Client.Get(c.GetDownloadURL(uri)) + var body io.ReadCloser + body, err = c.client.Download(uri) if err != nil { return } - defer resp.Body.Close() + defer body.Close() var buf bytes.Buffer - _, err = io.Copy(&buf, resp.Body) + _, err = io.Copy(&buf, body) if err != nil { return } |