Close io.Reader if possible
This commit is contained in:
parent
0c04ba220f
commit
2c78d31c27
|
@ -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.
|
||||||
|
|
20
get_test.go
20
get_test.go
|
@ -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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue