"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
copyurl: factor code into operations and write test
Nick Craig-Wood46c2f55545c8
diff --git a/cmd/copyurl/copyurl.go b/cmd/copyurl/copyurl.goindex 0d5764925..d09324dd6 100644--- a/cmd/copyurl/copyurl.go+++ b/cmd/copyurl/copyurl.go@@ -1,9 +1,6 @@package copyurlimport (- "net/http"- "time"-"github.com/ncw/rclone/cmd""github.com/ncw/rclone/fs/operations""github.com/spf13/cobra"@@ -25,14 +22,7 @@ without saving it in tmp storage.fsdst, dstFileName := cmd.NewFsDstFile(args[1:])cmd.Run(true, true, command, func() error {- resp, err := http.Get(args[0])- if err != nil {-- return err- }-- _, err = operations.RcatSize(fsdst, dstFileName, resp.Body, resp.ContentLength, time.Now())-+ _, err := operations.CopyURL(fsdst, dstFileName, args[0])return err})},diff --git a/fs/operations/operations.go b/fs/operations/operations.goindex a22aa4d69..209a39d70 100644--- a/fs/operations/operations.go+++ b/fs/operations/operations.go@@ -8,6 +8,7 @@ import ("fmt""io""io/ioutil"+ "net/http""path""sort""strconv"@@ -1359,6 +1360,16 @@ func RcatSize(fdst fs.Fs, dstFileName string, in io.ReadCloser, size int64, modTreturn obj, nil}+// CopyURL copies the data from the url to (fdst, dstFileName)+func CopyURL(fdst fs.Fs, dstFileName string, url string) (dst fs.Object, err error) {+ resp, err := http.Get(url)+ if err != nil {+ return nil, err+ }+ defer fs.CheckClose(resp.Body, &err)+ return RcatSize(fdst, dstFileName, resp.Body, resp.ContentLength, time.Now())+}+// moveOrCopyFile moves or copies a single file possibly to a new namefunc moveOrCopyFile(fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string, cp bool) (err error) {dstFilePath := path.Join(fdst.Root(), dstFileName)diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.goindex b95ed1baf..620175c91 100644--- a/fs/operations/operations_test.go+++ b/fs/operations/operations_test.go@@ -25,6 +25,8 @@ import ("fmt""io""io/ioutil"+ "net/http"+ "net/http/httptest""regexp""strings""testing"@@ -615,6 +617,28 @@ func TestRcatSize(t *testing.T) {fstest.CheckItems(t, r.Fremote, file1, file2)}+func TestCopyURL(t *testing.T) {+ r := fstest.NewRun(t)+ defer r.Finalise()++ contents := "file1 contents\n"+ file1 := r.WriteFile("file1", contents, t1)+ r.Mkdir(r.Fremote)+ fstest.CheckItems(t, r.Fremote)++ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _, err := w.Write([]byte(contents))+ assert.NoError(t, err)+ }))+ defer ts.Close()++ o, err := operations.CopyURL(r.Fremote, "file1", ts.URL)+ require.NoError(t, err)+ assert.Equal(t, int64(len(contents)), o.Size())++ fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, nil, fs.ModTimeNotSupported)+}+func TestMoveFile(t *testing.T) {r := fstest.NewRun(t)defer r.Finalise()