Skip to content

Commit

Permalink
Fix bugs of cp command
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Feb 1, 2024
1 parent fc7e6b0 commit de37ec1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
26 changes: 21 additions & 5 deletions api/ftp/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/alpacanetworks/alpacon-cli/api/server"
"github.com/alpacanetworks/alpacon-cli/client"
"github.com/alpacanetworks/alpacon-cli/utils"
"io"
"mime/multipart"
"net/http"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -89,21 +91,35 @@ func DownloadFile(ac *client.AlpaconClient, src string, dest string) error {
return err
}

var data []byte
maxAttempts := 100
utils.CliWarning("Awaits file transfer completion from Alpacon server. Transfer may timeout after 100 seconds. If the specified file is not found, it will not download even after 100 seconds.")

utils.CliWarning("Awaiting file transfer completion from Alpacon server. Transfer may timeout if it exceeds 100 seconds.")
maxAttempts := 100
var resp *http.Response

for count := 0; count < maxAttempts; count++ {
resp, err = ac.SendGetRequestForDownload(utils.RemovePrefixBeforeAPI(downloadResponse.DownloadURL))
if err != nil {
return err
}

if resp.StatusCode == http.StatusNotFound {
time.Sleep(1 * time.Second)
continue
}

break
if resp.StatusCode == http.StatusOK {
break
}
}

defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

err = utils.SaveFile(filepath.Join(dest, filepath.Base(remotePath)), data)
err = utils.SaveFile(filepath.Join(dest, filepath.Base(remotePath)), respBody)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ func (ac *AlpaconClient) SendMultipartRequest(url string, multiPartWriter *multi
return nil
}

// This function returns response for custom error handling in each function, unlike direct error throwing in sendRequest.
func (ac *AlpaconClient) SendGetRequestForDownload(url string) (*http.Response, error) {
req, err := ac.createRequest("GET", url, nil)
if err != nil {
return nil, err
}

resp, err := ac.HTTPClient.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

func (ac *AlpaconClient) IsUsingHTTPS() (bool, error) {
parsedURL, err := url.Parse(ac.BaseURL)
if err != nil {
Expand Down

0 comments on commit de37ec1

Please sign in to comment.