From 288f8c48043243e9df081d8046e828e07b386c0b Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Fri, 22 Apr 2022 21:18:50 +0800 Subject: [PATCH] Getting ready for BogFiles in database --- application/bog.go | 23 ++++++++++++++----- domain/entities.go | 1 + domain/services.go | 17 +++++++++----- integration/filesystem_bog_file_repository.go | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/application/bog.go b/application/bog.go index 507e94d..2e55097 100644 --- a/application/bog.go +++ b/application/bog.go @@ -3,7 +3,8 @@ package application import ( "net/http" "fmt" - "io" + "strconv" + // "io" "caj-larsson/bog/domain" "caj-larsson/bog/integration" ) @@ -41,23 +42,33 @@ func buildHttpMux(file_service domain.BogFileService) *http.ServeMux { case "GET": bog_file, err := file_service.OpenOutFile(ref) + if err == domain.ErrNotExists { + http.NotFound(w, r) + return + } + if err != nil { panic(err) } http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file) - case "POST": fallthrough case "PUT": - bog_file, err := file_service.CreateOrOpenInFile(ref) + size_str := r.Header["Content-Length"][0] + + size, err := strconv.ParseInt(size_str, 10, 64) if err != nil { - panic(err) + w.WriteHeader(422) + return } - io.Copy(bog_file, r.Body) - bog_file.Close() + err = file_service.SaveFile(ref, r.Body, size) + + if err != nil { + panic(err) + } } return }) diff --git a/domain/entities.go b/domain/entities.go index 30cdc2e..fc65034 100644 --- a/domain/entities.go +++ b/domain/entities.go @@ -22,6 +22,7 @@ type BogFile struct { var ( ErrDuplicate = errors.New("record already exists") + ErrExceedQuota = errors.New("file too large") ErrNotExists = errors.New("row not exists") ErrUpdateFailed = errors.New("update failed") ErrDeleteFailed = errors.New("delete failed") diff --git a/domain/services.go b/domain/services.go index 43d6f7d..e2df49f 100644 --- a/domain/services.go +++ b/domain/services.go @@ -1,7 +1,7 @@ package domain import ( - // "io" + "io" "time" "strconv" ) @@ -16,10 +16,13 @@ func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileD return BogFileService {user_agent_repo, file_data_repo} } -func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) { - +func (b BogFileService) SaveFile(ref FileReference, src io.Reader, size int64) error { user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) + if !user_agent.FileQuota.Allows(size) { + return ErrExceedQuota + } + if err == ErrNotExists { // TODO make this into a factory method? new_ua := UserAgent { @@ -35,14 +38,16 @@ func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) panic(err) } - f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10)) if err != nil { - return nil, err + return err } - return f, err + io.Copy(f, src) + f.Close() + + return nil } func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) { diff --git a/integration/filesystem_bog_file_repository.go b/integration/filesystem_bog_file_repository.go index 3260cb5..1a24d90 100644 --- a/integration/filesystem_bog_file_repository.go +++ b/integration/filesystem_bog_file_repository.go @@ -80,7 +80,7 @@ func (f FileSystemBogRepository) Open(filename string, user_agent_label string) file, err := os.OpenFile(abs_path, os.O_RDONLY, 0644) if err != nil { - panic(err) + return nil, domain.ErrNotExists } bfd := FileSystemBogFileData {filename, 0, time.Now(), file}