From 7274e08b9afc1f95e4f65d2e2b58776dad37c450 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Thu, 5 May 2022 18:22:06 +0800 Subject: [PATCH] fix #11 --- dataswamp/swampfile/file_contract.go | 1 + infrastructure/fs/swampfile/repository.go | 8 +++++++- server/bog.go | 2 +- server/bog_test.go | 10 +++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dataswamp/swampfile/file_contract.go b/dataswamp/swampfile/file_contract.go index 4e80593..67f8b06 100644 --- a/dataswamp/swampfile/file_contract.go +++ b/dataswamp/swampfile/file_contract.go @@ -33,6 +33,7 @@ func basicFileOperationContract(fac func() Repository, t *testing.T) { is.NoErr(err) is.True(reopened_file != nil) + is.Equal(reopened_file.Size(), int64(len(testdata))) readback := make([]byte, 128) diff --git a/infrastructure/fs/swampfile/repository.go b/infrastructure/fs/swampfile/repository.go index 5e42941..173e5a6 100644 --- a/infrastructure/fs/swampfile/repository.go +++ b/infrastructure/fs/swampfile/repository.go @@ -115,7 +115,13 @@ func (f Repository) Open(filename string, namespace_ns string) (swampfile.SwampO return nil, swampfile.ErrNotExists } - bfd := FileSystemSwampFileData{filename, 0, time.Now(), file} + stat, err := file.Stat() + + if err != nil { + return nil, err + } + + bfd := FileSystemSwampFileData{filename, stat.Size(), time.Now(), file} return bfd, nil } diff --git a/server/bog.go b/server/bog.go index 42742ac..7bd64db 100644 --- a/server/bog.go +++ b/server/bog.go @@ -44,7 +44,6 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi") return } - ref := swampfile.FileReference{r.URL.Path, r.Header["User-Agent"][0]} switch r.Method { @@ -71,6 +70,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(422) return } + b.logger.Info("Recieving file '%s' of size %d from '%s'", ref.Path, size, ref.UserAgent) err = b.file_service.SaveFile(ref, r.Body, size) diff --git a/server/bog_test.go b/server/bog_test.go index bb73150..2045b40 100644 --- a/server/bog_test.go +++ b/server/bog_test.go @@ -13,20 +13,28 @@ import ( "time" ) +type TestLogger struct{} + +func (t TestLogger) Debug(format string, a ...interface{}) {} +func (t TestLogger) Info(format string, a ...interface{}) {} +func (t TestLogger) Warn(format string, a ...interface{}) {} + func TestApplication(t *testing.T) { is := is.New(t) + logger := TestLogger{} file_service := dataswamp.NewSwampFileService( namespace.NewRepository(), swampfile.NewRepository(), 1000, time.Hour, - ServerLogger{Debug}, + logger, ) bog := Bog{ router: new(http.ServeMux), file_service: file_service, address: "fake", + logger: logger, } bog.routes() req := httptest.NewRequest("POST", "/apath", strings.NewReader("testdata"))