Refactor storage interface to return io.Reader and Writer instances

Doing so will enable encrypting/decrypting files in a 'pipeline' without
having to write the intermediate results to a file or store them in
their entirety in memory.

This commit also updates the existing storage classes (local and FTP) to
meet the new interface definition.
This commit is contained in:
2013-09-09 22:59:18 -04:00
parent 1254a7fb45
commit e2ae508382
5 changed files with 104 additions and 69 deletions

View File

@ -46,6 +46,21 @@ func RecursiveRemoveEmptyDirs(dir string) {
}
}
func CopyReaderToTmp(src io.Reader, tmpdir string) (string, error) {
outfile, err := ioutil.TempFile(tmpdir, "asink")
if err != nil {
return "", err
}
defer outfile.Close()
_, err = io.Copy(outfile, src)
if err != nil {
return "", err
}
return outfile.Name(), nil
}
func CopyToTmp(src string, tmpdir string) (string, error) {
infile, err := os.Open(src)
if err != nil {
@ -53,18 +68,7 @@ func CopyToTmp(src string, tmpdir string) (string, error) {
}
defer infile.Close()
outfile, err := ioutil.TempFile(tmpdir, "asink")
if err != nil {
return "", err
}
defer outfile.Close()
_, err = io.Copy(outfile, infile)
if err != nil {
return "", err
}
return outfile.Name(), nil
return CopyReaderToTmp(infile, tmpdir)
}
func ErrorFileNotFound(err error) bool {