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

@ -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
}
}