Skip to content

Commit

Permalink
reduce changes
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Nov 11, 2024
1 parent 11b49f1 commit d73987c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/rhttp/datatx/utils/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
// TODO check preconditions like If-Range, If-Match ...

var md *provider.ResourceInfo
var reader io.ReadCloser
var sendContent io.ReadCloser
var err error
var notModified bool

// do a stat to set Content-Length and etag headers

md, reader, err = fs.Download(ctx, ref, func(md *provider.ResourceInfo) bool {
md, sendContent, err = fs.Download(ctx, ref, func(md *provider.ResourceInfo) bool {
// range requests always need to open the reader to check if it is seekable
if r.Header.Get("Range") != "" {
return true
Expand Down Expand Up @@ -126,8 +126,8 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
handleError(w, &sublog, err, "download")
return
}
if reader != nil {
defer reader.Close()
if sendContent != nil {
defer sendContent.Close()
}
if notModified {
w.Header().Set(net.HeaderETag, md.Etag)
Expand Down Expand Up @@ -165,7 +165,7 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
code := http.StatusOK
sendSize := int64(md.Size)
var s io.Seeker
if s, ok = reader.(io.Seeker); ok {
if s, ok = sendContent.(io.Seeker); ok {
// tell clients they can send range requests
w.Header().Set("Accept-Ranges", "bytes")
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
reader = pr
sendContent = pr
defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
go func() {
for _, ra := range ranges {
Expand All @@ -222,7 +222,7 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
_ = pw.CloseWithError(err) // CloseWithError always returns nil
return
}
if _, err := io.CopyN(part, reader, ra.Length); err != nil {
if _, err := io.CopyN(part, sendContent, ra.Length); err != nil {
_ = pw.CloseWithError(err) // CloseWithError always returns nil
return
}
Expand All @@ -237,7 +237,7 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI
w.Header().Set(net.HeaderContentLength, strconv.FormatInt(sendSize, 10))
}

w.Header().Set(net.HeaderContentDisposistion, net.ContentDispositionAttachment(md.Name))
w.Header().Set(net.HeaderContentDisposistion, net.ContentDispositionAttachment(path.Base(md.Path)))
w.Header().Set(net.HeaderETag, md.Etag)
w.Header().Set(net.HeaderOCFileID, storagespace.FormatResourceID(md.Id))
w.Header().Set(net.HeaderOCETag, md.Etag)
Expand All @@ -251,7 +251,7 @@ func GetOrHeadFile(w http.ResponseWriter, r *http.Request, fs storage.FS, spaceI

if r.Method != "HEAD" {
var c int64
c, err = io.CopyN(w, reader, sendSize)
c, err = io.CopyN(w, sendContent, sendSize)
if err != nil {
sublog.Error().Err(err).Interface("resourceid", md.Id).Msg("error copying data to response")
return
Expand Down

0 comments on commit d73987c

Please sign in to comment.