From bb541a8193beb6d2d04c53cec2da44e78c317b76 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Mon, 18 Apr 2022 21:40:29 +0800 Subject: [PATCH] Clean up access a bit --- application/bog.go | 20 ++++++---- domain/repository.go | 5 ++- domain/services.go | 25 ++++++------ domain/valueobjects.go | 20 +++++----- integration/filesystem_bog_file_repository.go | 38 +++++++++++++++---- 5 files changed, 70 insertions(+), 38 deletions(-) diff --git a/application/bog.go b/application/bog.go index 5f20e48..37e0c1e 100644 --- a/application/bog.go +++ b/application/bog.go @@ -35,20 +35,24 @@ func New(config *Configuration) *Bog { return } - bog_file, err := b.fileDataRepo.OpenOrCreate(r.URL.Path, r.Header["User-Agent"][0]) - - if err != nil { - - } - switch r.Method { case "GET": - src := domainFileSource { bog_file.Path(), - http.ServeContent(w, r, , bog_file.Modified(), bog_file) + bog_file, err := b.fileDataRepo.Open(r.URL.Path, r.Header["User-Agent"][0]) + + 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 := b.fileDataRepo.Create(r.URL.Path, r.Header["User-Agent"][0]) + + if err != nil { + panic(err) + } io.Copy(bog_file, r.Body) bog_file.Close() diff --git a/domain/repository.go b/domain/repository.go index 273ca2f..a7f2d9d 100644 --- a/domain/repository.go +++ b/domain/repository.go @@ -10,6 +10,7 @@ type UserAgentRepository interface{ } type FileDataRepository interface { - OpenOrCreate(path string, user_agent string) (BogFileData, error) - Delete(file_data *BogFileData) + Create(path string, user_agent_label string) (BogInFile, error) + Open(path string, user_agent_label string) (BogOutFile, error) + Delete(path string, user_agent_label string) } diff --git a/domain/services.go b/domain/services.go index 50811e0..88ad3b8 100644 --- a/domain/services.go +++ b/domain/services.go @@ -1,30 +1,33 @@ package domain -import "io" +//import "io" type BogFileService struct { user_agent_repo UserAgentRepository file_data_repo FileDataRepository } -func (b *BogFileService) RecieveFile(src FileSource) error { - f, err := b.file_data_repo.OpenOrCreate(src.Path(), src.UserAgent()) +func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) { + user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) + + + f, err := b.file_data_repo.Create(ref.Path, string(user_agent.ID)) if err != nil { - return err + return nil, err } - io.Copy(f, src) - return nil + return f, err } -func (b *BogFileService) SendFile(dst FileDestination) error { - f, err := b.file_data_repo.OpenOrCreate(dst.Path(), dst.UserAgent()) +func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) { + user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) + + f, err := b.file_data_repo.Open(ref.Path, string(user_agent.ID)) if err != nil { - return err + return nil, err } - io.Copy(dst, f) - return nil + return f, nil } diff --git a/domain/valueobjects.go b/domain/valueobjects.go index 3035b0c..b7a71a7 100644 --- a/domain/valueobjects.go +++ b/domain/valueobjects.go @@ -22,25 +22,25 @@ func (f *FileSizeQuota) Remove(size int64) { f.CurrentUsage -= size } -type BogFileData interface { +type BogOutFile interface { Path() string Size() int64 Modified() time.Time io.Reader io.Seeker - io.Writer io.Closer } -type FileSource struct { - Path string - UserAgent string - Size int64 - Src io.ReadCloser +type BogInFile interface { + Path() string + Size() int64 + Modified() time.Time + io.Writer + io.Seeker + io.Closer } -type FileDestination struct { - Path string +type FileReference struct { + Path string UserAgent string - Dst io.WriteCloser } diff --git a/integration/filesystem_bog_file_repository.go b/integration/filesystem_bog_file_repository.go index 76ef273..3260cb5 100644 --- a/integration/filesystem_bog_file_repository.go +++ b/integration/filesystem_bog_file_repository.go @@ -9,6 +9,8 @@ import ( type FileSystemBogFileData struct { path string + size int64 + mod_time time.Time file *os.File } @@ -34,22 +36,24 @@ func (f FileSystemBogFileData) Path() string { } func (f FileSystemBogFileData) Size() int64 { - return 0 + return f.size } func (f FileSystemBogFileData) Modified() time.Time{ return time.Now() } - type FileSystemBogRepository struct { Root string } -func (f FileSystemBogRepository) OpenOrCreate(open_path string, user_agent string) (domain.BogFileData, error) { - //file_name := path.Base(open_path) - abs_path := path.Join(f.Root, open_path) +func (f FileSystemBogRepository) absPath(filename string, user_agent_label string) string { + return path.Join(f.Root, user_agent_label, filename) +} +func (f FileSystemBogRepository) Create(filename string, user_agent_label string) (domain.BogInFile, error) { + abs_path := f.absPath(filename, user_agent_label) + //file_name := path.Base(open_path) dir := path.Dir(abs_path) os.MkdirAll(dir, 0750) file, err := os.OpenFile(abs_path, os.O_RDWR|os.O_CREATE, 0644) @@ -58,11 +62,31 @@ func (f FileSystemBogRepository) OpenOrCreate(open_path string, user_agent strin panic(err) } - bfd := FileSystemBogFileData {open_path, file} + stat_info, err := file.Stat() + if err != nil { + panic(err) + } + + bfd := FileSystemBogFileData {filename, stat_info.Size(), stat_info.ModTime(), file} return bfd, nil } -func (f FileSystemBogRepository) Delete(file *domain.BogFileData) { +func (f FileSystemBogRepository) Open(filename string, user_agent_label string) (domain.BogOutFile, error) { + abs_path := f.absPath(filename, user_agent_label) + //file_name := path.Base(open_path) + dir := path.Dir(abs_path) + os.MkdirAll(dir, 0750) + file, err := os.OpenFile(abs_path, os.O_RDONLY, 0644) + + if err != nil { + panic(err) + } + + bfd := FileSystemBogFileData {filename, 0, time.Now(), file} + + return bfd, nil +} +func (f FileSystemBogRepository) Delete(filename string, user_agent_label string) { }