Skip to content

Commit

Permalink
Refactored downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Apr 4, 2023
1 parent c8b5228 commit a4f0127
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
10 changes: 8 additions & 2 deletions internal/http/services/archiver/manager/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ func (a *Archiver) CreateTar(ctx context.Context, dst io.Writer) error {
}

if !isDir {
err = a.downloader.Download(ctx, path, "", w)
r, err := a.downloader.Download(ctx, path, "")
if err != nil {
return err
}
if _, err := io.Copy(w, r); err != nil {
return err
}
}
return nil
})
Expand Down Expand Up @@ -239,10 +242,13 @@ func (a *Archiver) CreateZip(ctx context.Context, dst io.Writer) error {
}

if !isDir {
err = a.downloader.Download(ctx, path, "", dst)
r, err := a.downloader.Download(ctx, path, "")
if err != nil {
return err
}
if _, err := io.Copy(dst, r); err != nil {
return err
}
}
return nil
})
Expand Down
11 changes: 10 additions & 1 deletion internal/http/services/owncloud/ocdav/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ocdav
import (
"context"
"fmt"
"io"
"net/http"
"path"
"path/filepath"
Expand Down Expand Up @@ -253,7 +254,15 @@ func (h *VersionsHandler) doDownload(w http.ResponseWriter, r *http.Request, s *
w.Header().Set("Content-Transfer-Encoding", "binary")

down := downloader.NewDownloader(client)
if err := down.Download(ctx, resStat.Info.Path, key, w); err != nil {
d, err := down.Download(ctx, resStat.Info.Path, key)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer d.Close()

_, err = io.Copy(w, d)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down
23 changes: 11 additions & 12 deletions pkg/storage/utils/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
// Downloader is the interface implemented by the objects that are able to
// download a path into a destination Writer.
type Downloader interface {
Download(ctx context.Context, path string, versionKey string, w io.Writer) error
Download(ctx context.Context, path, versionKey string) (io.ReadCloser, error)
}

type revaDownloader struct {
Expand All @@ -62,7 +62,7 @@ func getDownloadProtocol(protocols []*gateway.FileDownloadProtocol, prot string)
}

// Download downloads a resource given the path to the dst Writer.
func (r *revaDownloader) Download(ctx context.Context, path, versionKey string, dst io.Writer) error {
func (r *revaDownloader) Download(ctx context.Context, path, versionKey string) (io.ReadCloser, error) {
req := &provider.InitiateFileDownloadRequest{
Ref: &provider.Reference{
Path: path,
Expand All @@ -82,37 +82,36 @@ func (r *revaDownloader) Download(ctx context.Context, path, versionKey string,

switch {
case err != nil:
return err
return nil, err
case downResp.Status.Code != rpc.Code_CODE_OK:
return errtypes.InternalError(downResp.Status.Message)
return nil, errtypes.InternalError(downResp.Status.Message)
}

p, err := getDownloadProtocol(downResp.Protocols, "simple")
if err != nil {
return err
return nil, err
}

httpReq, err := rhttp.NewRequest(ctx, http.MethodGet, p.DownloadEndpoint, nil)
if err != nil {
return err
return nil, err
}
httpReq.Header.Set(datagateway.TokenTransportHeader, p.Token)

httpRes, err := r.httpClient.Do(httpReq)
if err != nil {
return err
return nil, err
}
defer httpRes.Body.Close()

if httpRes.StatusCode != http.StatusOK {
defer httpRes.Body.Close()
switch httpRes.StatusCode {
case http.StatusNotFound:
return errtypes.NotFound(path)
return nil, errtypes.NotFound(path)
default:
return errtypes.InternalError(httpRes.Status)
return nil, errtypes.InternalError(httpRes.Status)
}
}

_, err = io.Copy(dst, httpRes.Body)
return err
return httpRes.Body, nil
}
10 changes: 3 additions & 7 deletions pkg/storage/utils/downloader/mock/downloader_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package mock

import (
"bufio"
"context"
"io"
"os"
Expand All @@ -36,13 +35,10 @@ func NewDownloader() downloader.Downloader {
}

// Download copies the content of a local file into the dst Writer.
func (m *mockDownloader) Download(ctx context.Context, path, _ string, dst io.Writer) error {
func (m *mockDownloader) Download(ctx context.Context, path, _ string) (io.ReadCloser, error) {
f, err := os.Open(path)
if err != nil {
return err
return nil, err
}
defer f.Close()
fr := bufio.NewReader(f)
_, err = io.Copy(dst, fr)
return err
return f, nil
}

0 comments on commit a4f0127

Please sign in to comment.