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. // Then calls back the next function, with the downloaded location and the original URI.
// Callback return is forwarded. // Callback return is forwarded.
importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error)
// getFile write to the given buffer, the file at the given location. // getFileReader returns a reader to the requested file.
getFile(filename string, writer *bufio.Writer) error getFile(filename string) (io.Reader, error)
// listDir returns a list of the files and subdirectories contained inside the directory at the given location. // listDir returns a list of the files and subdirectories contained inside the directory at the given location.
listDir(filename string) ([]string, error) listDir(filename string) ([]string, error)
// stat returns many information about the given file: such as last modification date, size, ... // 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 { func GetFile(i Importer, URI string, writer *bufio.Writer) error {
// Import file if it exists // Import file if it exists
if i.exists(URI) { 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 // Try to find file parts
@ -120,13 +131,21 @@ func GetFile(i Importer, URI string, writer *bufio.Writer) error {
for _, file := range files { for _, file := range files {
if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched { if matched, _ := path.Match(fname+"[0-9][0-9]", file); matched {
found = true 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 return err
} }
if fdc, ok := fd.(io.ReadCloser); ok {
defer fdc.Close()
}
writer.ReadFrom(fd)
} }
} }
if found { if found {
writer.Flush()
return nil return nil
} }
} }

View file

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

View file

@ -1,7 +1,6 @@
package sync package sync
import ( import (
"bufio"
"io" "io"
"net/url" "net/url"
"os" "os"
@ -35,8 +34,8 @@ func (i GitImporter) importFile(URI string, next func(string, string) (interface
return i.li.importFile(URI, next) return i.li.importFile(URI, next)
} }
func (i GitImporter) getFile(filename string, writer *bufio.Writer) error { func (i GitImporter) getFile(filename string) (io.Reader, error) {
return i.li.getFile(filename, writer) return i.li.getFile(filename)
} }
func (i GitImporter) writeFile(filename string, reader io.Reader) error { func (i GitImporter) writeFile(filename string, reader io.Reader) error {

View file

@ -1,7 +1,6 @@
package sync package sync
import ( import (
"bufio"
"fmt" "fmt"
"io" "io"
"io/ioutil" "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 { if fd, err := os.Open(path.Join(i.Base, filename)); err != nil {
return err return nil, err
} else { } else {
defer fd.Close() return fd, nil
reader := bufio.NewReader(fd)
reader.WriteTo(writer)
writer.Flush()
return nil
} }
} }