readerfile.go 640 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package files

import "io"

// ReaderFile is a implementation of File created from an `io.Reader`.
// ReaderFiles are never directories, and can be read from and closed.
type ReaderFile struct {
	Filename string
	Reader   io.Reader
}

func (f *ReaderFile) IsDirectory() bool {
	return false
}

func (f *ReaderFile) NextFile() (File, error) {
	return nil, ErrNotDirectory
}

func (f *ReaderFile) FileName() string {
	return f.Filename
}

func (f *ReaderFile) Read(p []byte) (int, error) {
	return f.Reader.Read(p)
}

func (f *ReaderFile) Close() error {
	closer, ok := f.Reader.(io.Closer)
	if !ok {
		return nil
	}
	return closer.Close()
}