Skip to content

Commit

Permalink
Set a boundary on the download response
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Jul 10, 2024
1 parent 823d716 commit f218fb1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

*Nothing yet.*

## [1.3.6] - July 10, 2024

### Fixed

* Ensure a `boundary` is set on federation downloads, allowing the download to work.

## [1.3.5] - July 10, 2024

### Added
Expand Down
12 changes: 10 additions & 2 deletions api/v1/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"bytes"
"github.com/t2bot/matrix-media-repo/util/ids"
"net/http"

"github.com/t2bot/matrix-media-repo/api/_apimeta"
Expand All @@ -26,23 +27,30 @@ func FederationDownloadMedia(r *http.Request, rctx rcontext.RequestContext, serv
r = _routers.ForceSetParam("server", r.Host, r)

res := r0.DownloadMedia(r, rctx, _apimeta.UserInfo{})
boundary, err := ids.NewUniqueId()
if err != nil {
rctx.Log.Error("Error generating boundary on response: ", err)
return _responses.InternalServerError("unable to generate boundary")
}
if dl, ok := res.(*_responses.DownloadResponse); ok {
return &_responses.DownloadResponse{
ContentType: "multipart/mixed",
ContentType: "multipart/mixed; boundary=" + boundary,
Filename: "",
SizeBytes: 0,
Data: readers.NewMultipartReader(
boundary,
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))},
&readers.MultipartPart{ContentType: dl.ContentType, FileName: dl.Filename, Reader: dl.Data},
),
TargetDisposition: "attachment",
}
} else if rd, ok := res.(*_responses.RedirectResponse); ok {
return &_responses.DownloadResponse{
ContentType: "multipart/mixed",
ContentType: "multipart/mixed; boundary=" + boundary,
Filename: "",
SizeBytes: 0,
Data: readers.NewMultipartReader(
boundary,
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))},
&readers.MultipartPart{Location: rd.ToUrl},
),
Expand Down
12 changes: 10 additions & 2 deletions api/v1/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"bytes"
"github.com/t2bot/matrix-media-repo/util/ids"
"net/http"

"github.com/t2bot/matrix-media-repo/api/_apimeta"
Expand All @@ -26,23 +27,30 @@ func FederationThumbnailMedia(r *http.Request, rctx rcontext.RequestContext, ser
r = _routers.ForceSetParam("server", r.Host, r)

res := r0.ThumbnailMedia(r, rctx, _apimeta.UserInfo{})
boundary, err := ids.NewUniqueId()
if err != nil {
rctx.Log.Error("Error generating boundary on response: ", err)
return _responses.InternalServerError("unable to generate boundary")
}
if dl, ok := res.(*_responses.DownloadResponse); ok {
return &_responses.DownloadResponse{
ContentType: "multipart/mixed",
ContentType: "multipart/mixed; boundary=" + boundary,
Filename: "",
SizeBytes: 0,
Data: readers.NewMultipartReader(
boundary,
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))},
&readers.MultipartPart{ContentType: dl.ContentType, FileName: dl.Filename, Reader: dl.Data},
),
TargetDisposition: "attachment",
}
} else if rd, ok := res.(*_responses.RedirectResponse); ok {
return &_responses.DownloadResponse{
ContentType: "multipart/mixed",
ContentType: "multipart/mixed; boundary=" + boundary,
Filename: "",
SizeBytes: 0,
Data: readers.NewMultipartReader(
boundary,
&readers.MultipartPart{ContentType: "application/json", Reader: readers.MakeCloser(bytes.NewReader([]byte("{}")))},
&readers.MultipartPart{Location: rd.ToUrl},
),
Expand Down
7 changes: 6 additions & 1 deletion util/readers/multipart_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ type MultipartPart struct {
Reader io.ReadCloser
}

func NewMultipartReader(parts ...*MultipartPart) io.ReadCloser {
func NewMultipartReader(boundary string, parts ...*MultipartPart) io.ReadCloser {
r, w := io.Pipe()
go func() {
mpw := multipart.NewWriter(w)
err := mpw.SetBoundary(boundary)
if err != nil {
// We don't have a good error route, and don't expect this to fail anyways.
panic(err)
}

for _, part := range parts {
headers := textproto.MIMEHeader{}
Expand Down

0 comments on commit f218fb1

Please sign in to comment.