Эх сурвалжийг харах

Add option to use async media uploads

https://github.com/matrix-org/matrix-spec-proposals/pull/2246
Tulir Asokan 3 жил өмнө
parent
commit
f79ca422e8
6 өөрчлөгдсөн 26 нэмэгдсэн , 8 устгасан
  1. 1 0
      config/config.go
  2. 1 0
      config/upgrade.go
  3. 2 0
      example-config.yaml
  4. 1 1
      go.mod
  5. 2 2
      go.sum
  6. 19 5
      portal.go

+ 1 - 0
config/config.go

@@ -34,6 +34,7 @@ type Config struct {
 		Asmux                         bool   `yaml:"asmux"`
 		StatusEndpoint                string `yaml:"status_endpoint"`
 		MessageSendCheckpointEndpoint string `yaml:"message_send_checkpoint_endpoint"`
+		AsyncMedia                    bool   `yaml:"async_media"`
 	} `yaml:"homeserver"`
 
 	AppService struct {

+ 1 - 0
config/upgrade.go

@@ -33,6 +33,7 @@ func (helper *UpgradeHelper) doUpgrade() {
 	helper.Copy(Bool, "homeserver", "asmux")
 	helper.Copy(Str|Null, "homeserver", "status_endpoint")
 	helper.Copy(Str|Null, "homeserver", "message_send_checkpoint_endpoint")
+	helper.Copy(Bool, "homeserver", "async_media")
 
 	helper.Copy(Str, "appservice", "address")
 	helper.Copy(Str, "appservice", "hostname")

+ 2 - 0
example-config.yaml

@@ -13,6 +13,8 @@ homeserver:
     status_endpoint: null
     # Endpoint for reporting per-message status.
     message_send_checkpoint_endpoint: null
+    # Does the homeserver support https://github.com/matrix-org/matrix-spec-proposals/pull/2246?
+    async_media: false
 
 # Application service host/registration related details.
 # Changing these values requires regeneration of the registration.

+ 1 - 1
go.mod

@@ -17,7 +17,7 @@ require (
 	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 	maunium.net/go/mauflag v1.0.0
 	maunium.net/go/maulogger/v2 v2.3.2
-	maunium.net/go/mautrix v0.10.13-0.20220317230932-15d85fe4d3cc
+	maunium.net/go/mautrix v0.10.13-0.20220321175701-3b9911029134
 )
 
 require (

+ 2 - 2
go.sum

@@ -197,5 +197,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
 maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
 maunium.net/go/maulogger/v2 v2.3.2 h1:1XmIYmMd3PoQfp9J+PaHhpt80zpfmMqaShzUTC7FwY0=
 maunium.net/go/maulogger/v2 v2.3.2/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A=
-maunium.net/go/mautrix v0.10.13-0.20220317230932-15d85fe4d3cc h1:7NNOnyCL03RWifzCko6QhVzn+gqS8DKRRtqCVBIKj7g=
-maunium.net/go/mautrix v0.10.13-0.20220317230932-15d85fe4d3cc/go.mod h1:WqW8mruBue+1YrL/f04Ni/4R5yfLcgO8BQhUJzl7sps=
+maunium.net/go/mautrix v0.10.13-0.20220321175701-3b9911029134 h1:EYo8hg0p7XL8nXV18dcsSc0f/0NKFQF/+D/Cll1q0rU=
+maunium.net/go/mautrix v0.10.13-0.20220321175701-3b9911029134/go.mod h1:WqW8mruBue+1YrL/f04Ni/4R5yfLcgO8BQhUJzl7sps=

+ 19 - 5
portal.go

@@ -2108,16 +2108,30 @@ func (portal *Portal) convertMediaMessageContent(intent *appservice.IntentAPI, m
 func (portal *Portal) uploadMedia(intent *appservice.IntentAPI, data []byte, content *event.MessageEventContent) error {
 	data, uploadMimeType, file := portal.encryptFile(data, content.Info.MimeType)
 
-	uploaded, err := intent.UploadBytes(data, uploadMimeType)
-	if err != nil {
-		return err
+	req := mautrix.ReqUploadMedia{
+		ContentBytes: data,
+		ContentType:  uploadMimeType,
+	}
+	var mxc id.ContentURI
+	if portal.bridge.Config.Homeserver.AsyncMedia {
+		uploaded, err := intent.UnstableUploadAsync(req)
+		if err != nil {
+			return err
+		}
+		mxc = uploaded.ContentURI
+	} else {
+		uploaded, err := intent.UploadMedia(req)
+		if err != nil {
+			return err
+		}
+		mxc = uploaded.ContentURI
 	}
 
 	if file != nil {
-		file.URL = uploaded.ContentURI.CUString()
+		file.URL = mxc.CUString()
 		content.File = file
 	} else {
-		content.URL = uploaded.ContentURI.CUString()
+		content.URL = mxc.CUString()
 	}
 
 	content.Info.Size = len(data)