forked from TrueCloudLab/rclone
copyurl: factor code into operations and write test
This commit is contained in:
parent
fc2afcbcbd
commit
46c2f55545
3 changed files with 36 additions and 11 deletions
|
@ -1,9 +1,6 @@
|
||||||
package copyurl
|
package copyurl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ncw/rclone/cmd"
|
"github.com/ncw/rclone/cmd"
|
||||||
"github.com/ncw/rclone/fs/operations"
|
"github.com/ncw/rclone/fs/operations"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -25,14 +22,7 @@ without saving it in tmp storage.
|
||||||
fsdst, dstFileName := cmd.NewFsDstFile(args[1:])
|
fsdst, dstFileName := cmd.NewFsDstFile(args[1:])
|
||||||
|
|
||||||
cmd.Run(true, true, command, func() error {
|
cmd.Run(true, true, command, func() error {
|
||||||
resp, err := http.Get(args[0])
|
_, err := operations.CopyURL(fsdst, dstFileName, args[0])
|
||||||
if err != nil {
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = operations.RcatSize(fsdst, dstFileName, resp.Body, resp.ContentLength, time.Now())
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -1359,6 +1360,16 @@ func RcatSize(fdst fs.Fs, dstFileName string, in io.ReadCloser, size int64, modT
|
||||||
return obj, nil
|
return 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 name
|
// moveOrCopyFile moves or copies a single file possibly to a new name
|
||||||
func moveOrCopyFile(fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string, cp bool) (err error) {
|
func moveOrCopyFile(fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string, cp bool) (err error) {
|
||||||
dstFilePath := path.Join(fdst.Root(), dstFileName)
|
dstFilePath := path.Join(fdst.Root(), dstFileName)
|
||||||
|
|
|
@ -25,6 +25,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -615,6 +617,28 @@ func TestRcatSize(t *testing.T) {
|
||||||
fstest.CheckItems(t, r.Fremote, file1, file2)
|
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) {
|
func TestMoveFile(t *testing.T) {
|
||||||
r := fstest.NewRun(t)
|
r := fstest.NewRun(t)
|
||||||
defer r.Finalise()
|
defer r.Finalise()
|
||||||
|
|
Loading…
Add table
Reference in a new issue