sync: Return the reader from importer instead of writing to a given Writer

This commit is contained in:
nemunaire 2022-11-21 12:30:13 +01:00
commit 541e32e10b
4 changed files with 34 additions and 26 deletions

View file

@ -1,7 +1,6 @@
package sync
import (
"bufio"
"errors"
"io"
"net/http"
@ -78,30 +77,26 @@ func (i CloudImporter) importFile(URI string, next func(string, string) (interfa
return ImportFile(i, URI, next)
}
func (i CloudImporter) getFile(filename string, writer *bufio.Writer) error {
func (i CloudImporter) getFile(filename string) (io.Reader, error) {
fullURL := i.baseDAV
fullURL.Path = path.Join(fullURL.Path, filename)
client := http.Client{}
if req, err := http.NewRequest("GET", fullURL.String(), nil); err != nil {
return err
return nil, err
} else {
req.SetBasicAuth(i.username, i.password)
if resp, err := client.Do(req); err != nil {
return err
return nil, err
} else {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
resp.Body.Close()
return nil, errors.New(resp.Status)
} else {
reader := bufio.NewReader(resp.Body)
reader.WriteTo(writer)
writer.Flush()
return resp.Body, nil
}
}
}
return nil
}
func (i CloudImporter) writeFile(filename string, reader io.Reader) error {