Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return *os.File from ContentStore.Get, close handle after usage #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewContentStore(base string) (*ContentStore, error) {

// Get takes a Meta object and retreives the content from the store, returning
// it as an io.Reader. If fromByte > 0, the reader starts from that byte
func (s *ContentStore) Get(meta *MetaObject, fromByte int64) (io.Reader, error) {
func (s *ContentStore) Get(meta *MetaObject, fromByte int64) (*os.File, error) {
path := filepath.Join(s.basePath, transformKey(meta.Oid))

f, err := os.Open(path)
Expand Down
8 changes: 7 additions & 1 deletion content_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func TestContenStoreGet(t *testing.T) {
if string(by) != "test content" {
t.Fatalf("expected to read content, got: %s", string(by))
}

r.Close()
}

func TestContenStoreGetWithRange(t *testing.T) {
Expand All @@ -123,16 +125,20 @@ func TestContenStoreGetWithRange(t *testing.T) {
if string(by) != "content" {
t.Fatalf("expected to read content, got: %s", string(by))
}

r.Close()
}

func TestContenStoreGetNonExisting(t *testing.T) {
setup()
defer teardown()

_, err := contentStore.Get(&MetaObject{Oid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, 0)
f, err := contentStore.Get(&MetaObject{Oid: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, 0)
if err == nil {
t.Fatalf("expected to get an error, but content existed")
}

f.Close()
}

func TestContenStoreExists(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (a *App) objectsRawHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Content-Length", fmt.Sprintf("%d", meta.Size))
io.Copy(w, content)
content.Close()
}

func (a *App) usersHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (a *App) GetContentHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
io.Copy(w, content)
logRequest(r, statusCode)
content.Close()
}

// GetMetaHandler retrieves metadata about the object
Expand Down
1 change: 1 addition & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func TestPut(t *testing.T) {
if string(c) != content {
t.Fatalf("expected content, got `%s`", string(c))
}
r.Close()
}

func TestMediaTypesRequired(t *testing.T) {
Expand Down