Skip to content

Commit

Permalink
fix: download artifacts in cli (#544)
Browse files Browse the repository at this point in the history
* fix: download artifacts in cli

* fix: proxy download file

* fix: proxy download file

* fix: api download file

* removed unnecessary files

* removed unnecessary files

* added stat for checing errors

* added commentr
  • Loading branch information
exu authored Nov 19, 2021
1 parent a926e7c commit 1cb4490
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
9 changes: 9 additions & 0 deletions internal/app/api/v1/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,20 @@ func (s testkubeAPI) GetArtifact() fiber.Handler {
return func(c *fiber.Ctx) error {
executionID := c.Params("executionID")
fileName := c.Params("filename")

// TODO fix this someday :) we don't know 15 mins before release why it's working this way
unescaped, err := url.QueryUnescape(fileName)
if err == nil {
fileName = unescaped
}

unescaped, err = url.QueryUnescape(fileName)
if err == nil {
fileName = unescaped
}

//// quickfix end

file, err := s.Storage.DownloadFile(executionID, fileName)
if err != nil {
return s.Error(c, http.StatusInternalServerError, err)
Expand Down
16 changes: 9 additions & 7 deletions pkg/api/v1/client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/problem"
Expand Down Expand Up @@ -437,8 +437,10 @@ func (c ProxyScriptsAPI) GetExecutionArtifacts(executionID string) (artifacts te
return c.getArtifactsFromResponse(resp)

}

func (c ProxyScriptsAPI) DownloadFile(executionID, fileName, destination string) (artifact string, err error) {
uri := c.getURI("/executions/%s/artifacts/%s", executionID, fileName)
// TODO consider use Query param for filename
uri := c.getURI("/executions/%s/artifacts/%s", executionID, url.QueryEscape(fileName))
req, err := c.GetProxy("GET").
Suffix(uri).
SetHeader("Accept", "text/event-stream").
Expand All @@ -449,17 +451,17 @@ func (c ProxyScriptsAPI) DownloadFile(executionID, fileName, destination string)

defer req.Close()

path := filepath.Join(destination, fileName)
split := strings.Split(fileName, "/")

f, err := os.Create(split[len(split)-1])
f, err := os.Create(filepath.Join(destination, filepath.Base(fileName)))
if err != nil {
return "", err
}

if _, err := f.ReadFrom(req); err != nil {
return "", err
}

defer f.Close()
return path, err
return f.Name(), err
}

func (c ProxyScriptsAPI) getArtifactsFromResponse(resp rest.Result) (artifacts []testkube.Artifact, err error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func (c *Client) DownloadFile(bucket, file string) (*minio.Object, error) {
return nil, err
}

_, err = reader.Stat()
if err != nil {
return reader, err
}

return reader, nil
}

Expand Down

0 comments on commit 1cb4490

Please sign in to comment.