bridgev2/matrix: remove custom buffer in stream upload
Some checks are pending
Go / Lint (latest) (push) Waiting to run
Go / Build (old, libolm) (push) Waiting to run
Go / Build (latest, libolm) (push) Waiting to run
Go / Build (old, goolm) (push) Waiting to run
Go / Build (latest, goolm) (push) Waiting to run

This commit is contained in:
Tulir Asokan 2024-08-20 01:20:39 +03:00
commit a614668174
2 changed files with 4 additions and 28 deletions

View file

@ -7,6 +7,7 @@
package matrix
import (
"bytes"
"context"
"errors"
"fmt"
@ -238,31 +239,6 @@ func (as *ASIntent) UploadMedia(ctx context.Context, roomID id.RoomID, data []by
return
}
type simpleBuffer struct {
data []byte
}
func (w *simpleBuffer) Write(p []byte) (n int, err error) {
if w.data == nil {
w.data = p
} else {
w.data = append(w.data, p...)
}
return len(p), nil
}
func (w *simpleBuffer) Seek(offset int64, whence int) (int64, error) {
if whence == io.SeekStart {
if offset == 0 {
w.data = nil
} else {
w.data = w.data[:offset]
}
return offset, nil
}
return 0, fmt.Errorf("unsupported whence value %d", whence)
}
func (as *ASIntent) UploadMediaStream(
ctx context.Context,
roomID id.RoomID,
@ -276,14 +252,14 @@ func (as *ASIntent) UploadMediaStream(
return "", nil, fmt.Errorf("file too large (%.2f MB > %.2f MB)", float64(size)/1000/1000, float64(as.Connector.MediaConfig.UploadSize)/1000/1000)
}
if !requireFile && 0 < size && size < as.Connector.Config.Matrix.UploadFileThreshold {
var buf simpleBuffer
var buf bytes.Buffer
replPath, err := cb(&buf)
if err != nil {
return "", nil, err
} else if replPath != "" {
panic(fmt.Errorf("logic error: replacement path must only be returned if requireFile is true"))
}
return as.UploadMedia(ctx, roomID, buf.data, fileName, mimeType)
return as.UploadMedia(ctx, roomID, buf.Bytes(), fileName, mimeType)
}
var tempFile *os.File
tempFile, err = os.CreateTemp("", "mautrix-upload-*")

View file

@ -85,7 +85,7 @@ type MatrixSendExtra struct {
//
// The first return value can specify a file path to use instead of the original temp file.
// Returning a replacement path is only valid if the parameter is a file.
type FileStreamCallback func(file io.WriteSeeker) (string, error)
type FileStreamCallback func(file io.Writer) (string, error)
type MatrixAPI interface {
GetMXID() id.UserID