sync: Return the reader from importer instead of writing to a given Writer
This commit is contained in:
parent
f4c3f1e15e
commit
541e32e10b
4 changed files with 34 additions and 26 deletions
|
|
@ -34,8 +34,8 @@ type Importer interface {
|
|||
// Then calls back the next function, with the downloaded location and the original URI.
|
||||
// Callback return is forwarded.
|
||||
importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error)
|
||||
// getFile write to the given buffer, the file at the given location.
|
||||
getFile(filename string, writer *bufio.Writer) error
|
||||
// getFileReader returns a reader to the requested file.
|
||||
getFile(filename string) (io.Reader, error)
|
||||
// listDir returns a list of the files and subdirectories contained inside the directory at the given location.
|
||||
listDir(filename string) ([]string, error)
|
||||
// stat returns many information about the given file: such as last modification date, size, ...
|
||||
|
|
@ -105,7 +105,18 @@ func getFileSize(i Importer, URI string) (size int64, err error) {
|
|||
func GetFile(i Importer, URI string, writer *bufio.Writer) error {
|
||||
// Import file if it exists
|
||||
if i.exists(URI) {
|
||||
return i.getFile(URI, writer)
|
||||
fd, err := i.getFile(URI)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fdc, ok := fd.(io.ReadCloser); ok {
|
||||
defer fdc.Close()
|
||||
}
|
||||
writer.ReadFrom(fd)
|
||||
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Try to find file parts
|
||||
|
|
@ -120,13 +131,21 @@ func GetFile(i Importer, URI string, writer *bufio.Writer) error {
|
|||
for _, file := range files {
|
||||
if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched {
|
||||
found = true
|
||||
if err := i.getFile(path.Join(dirname, file), writer); err != nil {
|
||||
|
||||
fd, err := i.getFile(path.Join(dirname, file))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if fdc, ok := fd.(io.ReadCloser); ok {
|
||||
defer fdc.Close()
|
||||
}
|
||||
writer.ReadFrom(fd)
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
writer.Flush()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
|
|
@ -35,8 +34,8 @@ func (i GitImporter) importFile(URI string, next func(string, string) (interface
|
|||
return i.li.importFile(URI, next)
|
||||
}
|
||||
|
||||
func (i GitImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
return i.li.getFile(filename, writer)
|
||||
func (i GitImporter) getFile(filename string) (io.Reader, error) {
|
||||
return i.li.getFile(filename)
|
||||
}
|
||||
|
||||
func (i GitImporter) writeFile(filename string, reader io.Reader) error {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
|
@ -81,15 +80,11 @@ func (i LocalImporter) importFile(URI string, next func(string, string) (interfa
|
|||
}
|
||||
}
|
||||
|
||||
func (i LocalImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
func (i LocalImporter) getFile(filename string) (io.Reader, error) {
|
||||
if fd, err := os.Open(path.Join(i.Base, filename)); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
reader := bufio.NewReader(fd)
|
||||
reader.WriteTo(writer)
|
||||
writer.Flush()
|
||||
return nil
|
||||
return fd, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue