diff options
author | Janoš Guljaš <janos@users.noreply.github.com> | 2018-08-10 22:12:55 +0800 |
---|---|---|
committer | Balint Gabor <balint.g@gmail.com> | 2018-08-10 22:12:55 +0800 |
commit | 6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36 (patch) | |
tree | 3bcd5ab444bd7486a9011656c85baeee00216286 /swarm/api/client | |
parent | 3ec5dda4d2dd0dec6d5bd465752f30e8f6ce208c (diff) | |
download | dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar.gz dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar.bz2 dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar.lz dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar.xz dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.tar.zst dexon-6d1e292eefa70b5cb76cd03ff61fc6c4550d7c36.zip |
Manifest cli fix and upload defaultpath only once (#17375)
* cmd/swarm: fix manifest subcommands and add tests
* cmd/swarm: manifest update: update default entry for non-encrypted uploads
* swarm/api: upload defaultpath file only once
* swarm/api/client: improve UploadDirectory default path handling
* cmd/swarm: support absolute and relative default path values
* cmd/swarm: fix a typo in test
* cmd/swarm: check encrypted uploads in manifest update tests
Diffstat (limited to 'swarm/api/client')
-rw-r--r-- | swarm/api/client/client.go | 31 | ||||
-rw-r--r-- | swarm/api/client/client_test.go | 2 |
2 files changed, 18 insertions, 15 deletions
diff --git a/swarm/api/client/client.go b/swarm/api/client/client.go index b3a5e929d..8a9efe360 100644 --- a/swarm/api/client/client.go +++ b/swarm/api/client/client.go @@ -138,7 +138,7 @@ func (c *Client) Upload(file *File, manifest string, toEncrypt bool) (string, er if file.Size <= 0 { return "", errors.New("file size must be greater than zero") } - return c.TarUpload(manifest, &FileUploader{file}, toEncrypt) + return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt) } // Download downloads a file with the given path from the swarm manifest with @@ -175,7 +175,15 @@ func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bo } else if !stat.IsDir() { return "", fmt.Errorf("not a directory: %s", dir) } - return c.TarUpload(manifest, &DirectoryUploader{dir, defaultPath}, toEncrypt) + if defaultPath != "" { + if _, err := os.Stat(filepath.Join(dir, defaultPath)); err != nil { + if os.IsNotExist(err) { + return "", fmt.Errorf("the default path %q was not found in the upload directory %q", defaultPath, dir) + } + return "", fmt.Errorf("default path: %v", err) + } + } + return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt) } // DownloadDirectory downloads the files contained in a swarm manifest under @@ -389,21 +397,11 @@ func (u UploaderFunc) Upload(upload UploadFn) error { // DirectoryUploader uploads all files in a directory, optionally uploading // a file to the default path type DirectoryUploader struct { - Dir string - DefaultPath string + Dir string } // Upload performs the upload of the directory and default path func (d *DirectoryUploader) Upload(upload UploadFn) error { - if d.DefaultPath != "" { - file, err := Open(d.DefaultPath) - if err != nil { - return err - } - if err := upload(file); err != nil { - return err - } - } return filepath.Walk(d.Dir, func(path string, f os.FileInfo, err error) error { if err != nil { return err @@ -441,7 +439,7 @@ type UploadFn func(file *File) error // TarUpload uses the given Uploader to upload files to swarm as a tar stream, // returning the resulting manifest hash -func (c *Client) TarUpload(hash string, uploader Uploader, toEncrypt bool) (string, error) { +func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool) (string, error) { reqR, reqW := io.Pipe() defer reqR.Close() addr := hash @@ -458,6 +456,11 @@ func (c *Client) TarUpload(hash string, uploader Uploader, toEncrypt bool) (stri return "", err } req.Header.Set("Content-Type", "application/x-tar") + if defaultPath != "" { + q := req.URL.Query() + q.Set("defaultpath", defaultPath) + req.URL.RawQuery = q.Encode() + } // use 'Expect: 100-continue' so we don't send the request body if // the server refuses the request diff --git a/swarm/api/client/client_test.go b/swarm/api/client/client_test.go index dc608e3f1..ae82a91d7 100644 --- a/swarm/api/client/client_test.go +++ b/swarm/api/client/client_test.go @@ -194,7 +194,7 @@ func TestClientUploadDownloadDirectory(t *testing.T) { // upload the directory client := NewClient(srv.URL) - defaultPath := filepath.Join(dir, testDirFiles[0]) + defaultPath := testDirFiles[0] hash, err := client.UploadDirectory(dir, defaultPath, "", false) if err != nil { t.Fatalf("error uploading directory: %s", err) |