Close io.Reader if possible

This commit is contained in:
Acconut 2015-07-28 14:58:52 +02:00
parent 0c04ba220f
commit 2c78d31c27
3 changed files with 26 additions and 1 deletions

View File

@ -48,6 +48,8 @@ type DataStore interface {
// should not be enabled any call to GetReader should return // should not be enabled any call to GetReader should return
// tusd.ErrNotImplemented. The length of the resource is determined by // tusd.ErrNotImplemented. The length of the resource is determined by
// retrieving the offset using GetInfo. // retrieving the offset using GetInfo.
// If the returned reader also implements the io.Closer interface, the
// Close() method will be invoked once everything has been read.
GetReader(id string) (io.Reader, error) GetReader(id string) (io.Reader, error)
// Terminate an upload so any further requests to the resource, both reading // Terminate an upload so any further requests to the resource, both reading
// and writing, must return os.ErrNotExist or similar. // and writing, must return os.ErrNotExist or similar.

View File

@ -24,7 +24,21 @@ func (s getStore) GetInfo(id string) (FileInfo, error) {
} }
func (s getStore) GetReader(id string) (io.Reader, error) { func (s getStore) GetReader(id string) (io.Reader, error) {
return strings.NewReader("hello"), nil return reader, nil
}
type closingStringReader struct {
*strings.Reader
closed bool
}
func (reader *closingStringReader) Close() error {
reader.closed = true
return nil
}
var reader = &closingStringReader{
Reader: strings.NewReader("hello"),
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
@ -42,4 +56,8 @@ func TestGet(t *testing.T) {
"Content-Length": "5", "Content-Length": "5",
}, },
}).Run(handler, t) }).Run(handler, t)
if !reader.closed {
t.Error("expected reader to be closed")
}
} }

View File

@ -383,6 +383,11 @@ func (handler *Handler) getFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", strconv.FormatInt(info.Offset, 10)) w.Header().Set("Content-Length", strconv.FormatInt(info.Offset, 10))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
io.Copy(w, src) io.Copy(w, src)
// Try to close the reader if the io.Closer interface is implemented
if closer, ok := src.(io.Closer); ok {
closer.Close()
}
} }
// Terminate an upload permanently. // Terminate an upload permanently.