Skip to content

Commit

Permalink
Add option to use async media uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 21, 2022
1 parent 34954fc commit f79ca42
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions config/config.go
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions config/upgrade.go
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions example-config.yaml
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -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=
24 changes: 19 additions & 5 deletions portal.go
Expand Up @@ -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)
Expand Down

0 comments on commit f79ca42

Please sign in to comment.