Skip to content

Commit 3b99110

Browse files
committed
Add support for asynchronous media uploads
See matrix-org/matrix-spec-proposals#2246 for more info
1 parent 51dd093 commit 3b99110

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

client.go

+37
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,36 @@ func (cli *Client) DownloadBytes(mxcURL id.ContentURI) ([]byte, error) {
11931193
return ioutil.ReadAll(resp)
11941194
}
11951195

1196+
// UnstableCreateMXC creates a blank Matrix content URI to allow uploading the content asynchronously later.
1197+
// See https://github.com/matrix-org/matrix-spec-proposals/pull/2246
1198+
func (cli *Client) UnstableCreateMXC() (*RespCreateMXC, error) {
1199+
u, _ := url.Parse(cli.BuildBaseURL("_matrix", "media", "unstable", "fi.mau.msc2246", "create"))
1200+
var m RespCreateMXC
1201+
_, err := cli.MakeFullRequest(FullRequest{
1202+
Method: http.MethodPost,
1203+
URL: u.String(),
1204+
ResponseJSON: &m,
1205+
})
1206+
return &m, err
1207+
}
1208+
1209+
// UnstableUploadAsync creates a blank content URI with UnstableCreateMXC, starts uploading the data in the background
1210+
// and returns the created MXC immediately. See https://github.com/matrix-org/matrix-spec-proposals/pull/2246 for more info.
1211+
func (cli *Client) UnstableUploadAsync(req ReqUploadMedia) (*RespCreateMXC, error) {
1212+
resp, err := cli.UnstableCreateMXC()
1213+
if err != nil {
1214+
return nil, err
1215+
}
1216+
req.UnstableMXC = resp.ContentURI
1217+
go func() {
1218+
_, err = cli.UploadMedia(req)
1219+
if err != nil {
1220+
cli.logWarning("Failed to upload %s: %v", req.UnstableMXC, err)
1221+
}
1222+
}()
1223+
return resp, nil
1224+
}
1225+
11961226
func (cli *Client) UploadBytes(data []byte, contentType string) (*RespMediaUpload, error) {
11971227
return cli.UploadBytesWithName(data, contentType, "")
11981228
}
@@ -1222,12 +1252,19 @@ type ReqUploadMedia struct {
12221252
ContentLength int64
12231253
ContentType string
12241254
FileName string
1255+
1256+
// UnstableMXC specifies an existing MXC URI which doesn't have content yet to upload into.
1257+
// See https://github.com/matrix-org/matrix-spec-proposals/pull/2246 for more info.
1258+
UnstableMXC id.ContentURI
12251259
}
12261260

12271261
// UploadMedia uploads the given data to the content repository and returns an MXC URI.
12281262
// See https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload
12291263
func (cli *Client) UploadMedia(data ReqUploadMedia) (*RespMediaUpload, error) {
12301264
u, _ := url.Parse(cli.BuildBaseURL("_matrix", "media", "r0", "upload"))
1265+
if !data.UnstableMXC.IsEmpty() {
1266+
u, _ = url.Parse(cli.BuildBaseURL("_matrix", "media", "unstable", "fi.mau.msc2246", "upload", data.UnstableMXC.Homeserver, data.UnstableMXC.FileID))
1267+
}
12311268
if len(data.FileName) > 0 {
12321269
q := u.Query()
12331270
q.Set("filename", data.FileName)

responses.go

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ type RespMediaUpload struct {
9898
ContentURI id.ContentURI `json:"content_uri"`
9999
}
100100

101+
// RespCreateMXC is the JSON response for /_matrix/media/v3/create as specified in https://github.com/matrix-org/matrix-spec-proposals/pull/2246
102+
type RespCreateMXC struct {
103+
ContentURI id.ContentURI `json:"content_uri"`
104+
UnusedExpiresAt int `json:"unused_expires_at,omitempty"`
105+
}
106+
101107
// RespPreviewURL is the JSON response for https://spec.matrix.org/v1.2/client-server-api/#get_matrixmediav3preview_url
102108
type RespPreviewURL struct {
103109
CanonicalURL string `json:"og:url,omitempty"`

0 commit comments

Comments
 (0)