go-asink/asink/storage.go
Aaron Lindsay e2ae508382 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.
2013-09-11 19:37:42 -04:00

45 lines
944 B
Go

/*
Copyright (C) 2013 Aaron Lindsay <aaron@aclindsay.com>
*/
package main
import (
"code.google.com/p/goconf/conf"
"errors"
"io"
)
type Storage interface {
// Close() MUST be called on the returned io.WriteCloser
Put(hash string) (io.WriteCloser, error)
// Close() MUST be called on the returned io.ReadCloser
Get(hash string) (io.ReadCloser, error)
}
func GetStorage(config *conf.ConfigFile) (Storage, error) {
storageMethod, err := config.GetString("storage", "method")
if err != nil {
return nil, errors.New("Error: storage method not specified in config file.")
}
var storage Storage
switch storageMethod {
case "local":
storage, err = NewLocalStorage(config)
if err != nil {
return nil, err
}
case "ftp":
storage, err = NewFTPStorage(config)
if err != nil {
return nil, err
}
default:
return nil, errors.New("Error: storage method '" + storageMethod + "' not found.")
}
return storage, nil
}