@@ -1193,6 +1193,36 @@ func (cli *Client) DownloadBytes(mxcURL id.ContentURI) ([]byte, error) {
1193
1193
return ioutil .ReadAll (resp )
1194
1194
}
1195
1195
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
+
1196
1226
func (cli * Client ) UploadBytes (data []byte , contentType string ) (* RespMediaUpload , error ) {
1197
1227
return cli .UploadBytesWithName (data , contentType , "" )
1198
1228
}
@@ -1222,12 +1252,19 @@ type ReqUploadMedia struct {
1222
1252
ContentLength int64
1223
1253
ContentType string
1224
1254
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
1225
1259
}
1226
1260
1227
1261
// UploadMedia uploads the given data to the content repository and returns an MXC URI.
1228
1262
// See https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload
1229
1263
func (cli * Client ) UploadMedia (data ReqUploadMedia ) (* RespMediaUpload , error ) {
1230
1264
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
+ }
1231
1268
if len (data .FileName ) > 0 {
1232
1269
q := u .Query ()
1233
1270
q .Set ("filename" , data .FileName )
0 commit comments