Skip to content

Commit 213823a

Browse files
committed
[mod-misty] feat(operations): support returning raw response from CopyURL
1 parent 38b388a commit 213823a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

fs/operations/operations.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,18 @@ func (d *noStopDownloader) Close() error {
18961896
// copyURLFunc is called from CopyURLFn
18971897
type copyURLFunc func(ctx context.Context, dstFileName string, in io.ReadCloser, size int64, modTime time.Time) (err error)
18981898

1899+
// copyURLFuncEx is called from CopyURLFn
1900+
type copyURLFuncEx func(ctx context.Context, dstFileName string, in io.ReadCloser, size int64, modTime time.Time, resp *http.Response) (err error)
1901+
18991902
// copyURLFn copies the data from the url to the function supplied
19001903
func copyURLFn(ctx context.Context, dstFileName string, url string, autoFilename, dstFileNameFromHeader bool, fn copyURLFunc) (err error) {
1904+
return copyURLFnEx(ctx, dstFileName, url, autoFilename, dstFileNameFromHeader, func(ctx context.Context, dstFileName string, in io.ReadCloser, size int64, modTime time.Time, resp *http.Response) (err error) {
1905+
return fn(ctx, dstFileName, in, size, modTime)
1906+
})
1907+
}
1908+
1909+
// copyURLFn copies the data from the url to the function supplied
1910+
func copyURLFnEx(ctx context.Context, dstFileName string, url string, autoFilename, dstFileNameFromHeader bool, fn copyURLFuncEx) (err error) {
19011911
client := fshttp.NewClient(ctx)
19021912
resp, err := client.Get(url)
19031913
if err != nil {
@@ -1921,7 +1931,7 @@ func copyURLFn(ctx context.Context, dstFileName string, url string, autoFilename
19211931
return fmt.Errorf("CopyURL failed: filename not found in the Content-Disposition header")
19221932
}
19231933
fs.Debugf(headerFilename, "filename found in Content-Disposition header.")
1924-
return fn(ctx, headerFilename, resp.Body, resp.ContentLength, modTime)
1934+
return fn(ctx, headerFilename, resp.Body, resp.ContentLength, modTime, resp)
19251935
}
19261936

19271937
dstFileName = path.Base(resp.Request.URL.Path)
@@ -1930,7 +1940,7 @@ func copyURLFn(ctx context.Context, dstFileName string, url string, autoFilename
19301940
}
19311941
fs.Debugf(dstFileName, "File name found in url")
19321942
}
1933-
return fn(ctx, dstFileName, d, resp.ContentLength, modTime)
1943+
return fn(ctx, dstFileName, d, resp.ContentLength, modTime, resp)
19341944
}
19351945

19361946
// CopyURL copies the data from the url to (fdst, dstFileName)
@@ -1949,6 +1959,22 @@ func CopyURL(ctx context.Context, fdst fs.Fs, dstFileName string, url string, au
19491959
return dst, err
19501960
}
19511961

1962+
// CopyURL copies the data from the url to (fdst, dstFileName)
1963+
func CopyURLEx(ctx context.Context, fdst fs.Fs, dstFileName string, url string, autoFilename, dstFileNameFromHeader bool, noClobber bool) (dst fs.Object, rawResp *http.Response, err error) {
1964+
err = copyURLFnEx(ctx, dstFileName, url, autoFilename, dstFileNameFromHeader, func(ctx context.Context, dstFileName string, in io.ReadCloser, size int64, modTime time.Time, resp *http.Response) (err error) {
1965+
if noClobber {
1966+
_, err = fdst.NewObject(ctx, dstFileName)
1967+
if err == nil {
1968+
return errors.New("CopyURL failed: file already exist")
1969+
}
1970+
}
1971+
dst, err = RcatSize(ctx, fdst, dstFileName, in, size, modTime, nil)
1972+
rawResp = resp
1973+
return err
1974+
})
1975+
return dst, rawResp, err
1976+
}
1977+
19521978
// CopyURLToWriter copies the data from the url to the io.Writer supplied
19531979
func CopyURLToWriter(ctx context.Context, url string, out io.Writer) (err error) {
19541980
return copyURLFn(ctx, "", url, false, false, func(ctx context.Context, dstFileName string, in io.ReadCloser, size int64, modTime time.Time) (err error) {

0 commit comments

Comments
 (0)