Skip to content

Commit

Permalink
Allow passing content bytes to FullRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 21, 2022
1 parent 4ee0bad commit 51dd093
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions client.go
Expand Up @@ -330,6 +330,7 @@ type FullRequest struct {
URL string
Headers http.Header
RequestJSON interface{}
RequestBytes []byte
RequestBody io.Reader
RequestLength int64
ResponseJSON interface{}
Expand Down Expand Up @@ -361,7 +362,11 @@ func (params *FullRequest) compileRequest() (*http.Request, error) {
} else {
logBody = string(jsonStr)
}
reqBody = bytes.NewBuffer(jsonStr)
reqBody = bytes.NewReader(jsonStr)
} else if params.RequestBytes != nil {
logBody = fmt.Sprintf("<%d bytes>", len(params.RequestBytes))
reqBody = bytes.NewReader(params.RequestBytes)
params.RequestLength = int64(len(params.RequestBytes))
} else if params.RequestLength > 0 && params.RequestBody != nil {
logBody = fmt.Sprintf("<%d bytes>", params.RequestLength)
}
Expand Down Expand Up @@ -1194,10 +1199,9 @@ func (cli *Client) UploadBytes(data []byte, contentType string) (*RespMediaUploa

func (cli *Client) UploadBytesWithName(data []byte, contentType, fileName string) (*RespMediaUpload, error) {
return cli.UploadMedia(ReqUploadMedia{
Content: bytes.NewReader(data),
ContentLength: int64(len(data)),
ContentType: contentType,
FileName: fileName,
ContentBytes: data,
ContentType: contentType,
FileName: fileName,
})
}

Expand All @@ -1213,14 +1217,15 @@ func (cli *Client) Upload(content io.Reader, contentType string, contentLength i
}

type ReqUploadMedia struct {
ContentBytes []byte
Content io.Reader
ContentLength int64
ContentType string
FileName string
}

// UploadMedia uploads the given data to the content repository and returns an MXC URI.
// See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-media-r0-upload
// See https://spec.matrix.org/v1.2/client-server-api/#post_matrixmediav3upload
func (cli *Client) UploadMedia(data ReqUploadMedia) (*RespMediaUpload, error) {
u, _ := url.Parse(cli.BuildBaseURL("_matrix", "media", "r0", "upload"))
if len(data.FileName) > 0 {
Expand All @@ -1239,6 +1244,7 @@ func (cli *Client) UploadMedia(data ReqUploadMedia) (*RespMediaUpload, error) {
Method: http.MethodPost,
URL: u.String(),
Headers: headers,
RequestBytes: data.ContentBytes,
RequestBody: data.Content,
RequestLength: data.ContentLength,
ResponseJSON: &m,
Expand Down

0 comments on commit 51dd093

Please sign in to comment.