2015-03-09 17:20:26 +00:00
|
|
|
package storage
|
2015-02-25 17:56:40 +00:00
|
|
|
|
|
|
|
import (
|
2015-02-27 22:36:24 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2015-03-03 19:23:04 +00:00
|
|
|
"hash/crc64"
|
2015-02-25 17:56:40 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-07-22 14:27:53 +00:00
|
|
|
"path/filepath"
|
2015-02-25 17:56:40 +00:00
|
|
|
)
|
|
|
|
|
2015-06-23 20:13:29 +00:00
|
|
|
// FileGetter is the interface for getting a stream of a file payload,
|
|
|
|
// addressed by name/filename. Presumably, the names will be scoped to relative
|
|
|
|
// file paths.
|
2015-02-25 17:56:40 +00:00
|
|
|
type FileGetter interface {
|
|
|
|
// Get returns a stream for the provided file path
|
2015-07-22 14:27:53 +00:00
|
|
|
Get(filename string) (output io.ReadCloser, err error)
|
2015-02-25 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 18:11:11 +00:00
|
|
|
// FilePutter is the interface for storing a stream of a file payload,
|
2015-07-22 14:27:53 +00:00
|
|
|
// addressed by name/filename.
|
2015-02-27 21:54:04 +00:00
|
|
|
type FilePutter interface {
|
2015-03-09 18:11:11 +00:00
|
|
|
// Put returns the size of the stream received, and the crc64 checksum for
|
|
|
|
// the provided stream
|
2015-07-22 14:27:53 +00:00
|
|
|
Put(filename string, input io.Reader) (size int64, checksum []byte, err error)
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 18:11:11 +00:00
|
|
|
// FileGetPutter is the interface that groups both Getting and Putting file
|
|
|
|
// payloads.
|
2015-02-27 21:54:04 +00:00
|
|
|
type FileGetPutter interface {
|
|
|
|
FileGetter
|
|
|
|
FilePutter
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:11:11 +00:00
|
|
|
// NewPathFileGetter returns a FileGetter that is for files relative to path
|
|
|
|
// relpath.
|
2015-02-25 17:56:40 +00:00
|
|
|
func NewPathFileGetter(relpath string) FileGetter {
|
|
|
|
return &pathFileGetter{root: relpath}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathFileGetter struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pfg pathFileGetter) Get(filename string) (io.ReadCloser, error) {
|
2015-07-22 14:27:53 +00:00
|
|
|
return os.Open(filepath.Join(pfg.root, filename))
|
2015-02-25 17:56:40 +00:00
|
|
|
}
|
2015-02-27 21:54:04 +00:00
|
|
|
|
|
|
|
type bufferFileGetPutter struct {
|
|
|
|
files map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
|
2015-02-27 22:36:24 +00:00
|
|
|
if _, ok := bfgp.files[name]; !ok {
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
b := bytes.NewBuffer(bfgp.files[name])
|
|
|
|
return &readCloserWrapper{b}, nil
|
|
|
|
}
|
2015-02-27 21:54:04 +00:00
|
|
|
|
2015-03-04 22:14:43 +00:00
|
|
|
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) (int64, []byte, error) {
|
2015-08-13 18:42:43 +00:00
|
|
|
crc := crc64.New(CRCTable)
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
cw := io.MultiWriter(crc, buf)
|
|
|
|
i, err := io.Copy(cw, r)
|
2015-03-04 22:14:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
2015-02-27 22:36:24 +00:00
|
|
|
}
|
2015-08-13 18:42:43 +00:00
|
|
|
bfgp.files[name] = buf.Bytes()
|
|
|
|
return i, crc.Sum(nil), nil
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
type readCloserWrapper struct {
|
|
|
|
io.Reader
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
func (w *readCloserWrapper) Close() error { return nil }
|
2015-02-27 21:54:04 +00:00
|
|
|
|
2015-06-23 20:13:29 +00:00
|
|
|
// NewBufferFileGetPutter is a simple in-memory FileGetPutter
|
2015-02-28 17:47:55 +00:00
|
|
|
//
|
|
|
|
// Implication is this is memory intensive...
|
|
|
|
// Probably best for testing or light weight cases.
|
2015-02-27 21:54:04 +00:00
|
|
|
func NewBufferFileGetPutter() FileGetPutter {
|
2015-02-28 17:47:55 +00:00
|
|
|
return &bufferFileGetPutter{
|
|
|
|
files: map[string][]byte{},
|
|
|
|
}
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
2015-03-02 20:25:03 +00:00
|
|
|
|
|
|
|
// NewDiscardFilePutter is a bit bucket FilePutter
|
|
|
|
func NewDiscardFilePutter() FilePutter {
|
|
|
|
return &bitBucketFilePutter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bitBucketFilePutter struct {
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:14:43 +00:00
|
|
|
func (bbfp *bitBucketFilePutter) Put(name string, r io.Reader) (int64, []byte, error) {
|
2015-03-09 17:20:26 +00:00
|
|
|
c := crc64.New(CRCTable)
|
2015-08-12 23:46:04 +00:00
|
|
|
i, err := io.Copy(c, r)
|
2015-03-04 22:14:43 +00:00
|
|
|
return i, c.Sum(nil), err
|
2015-03-02 20:25:03 +00:00
|
|
|
}
|
2015-03-03 19:23:04 +00:00
|
|
|
|
2015-03-09 17:20:26 +00:00
|
|
|
// CRCTable is the default table used for crc64 sum calculations
|
|
|
|
var CRCTable = crc64.MakeTable(crc64.ISO)
|