Clean up access a bit

master
Caj Larsson 3 years ago
parent a26782d6cd
commit bb541a8193

@ -35,20 +35,24 @@ func New(config *Configuration) *Bog {
return return
} }
bog_file, err := b.fileDataRepo.OpenOrCreate(r.URL.Path, r.Header["User-Agent"][0]) switch r.Method {
case "GET":
bog_file, err := b.fileDataRepo.Open(r.URL.Path, r.Header["User-Agent"][0])
if err != nil { if err != nil {
panic(err)
} }
switch r.Method { http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file)
case "GET":
src := domainFileSource { bog_file.Path(),
http.ServeContent(w, r, , bog_file.Modified(), bog_file)
case "POST": case "POST":
fallthrough fallthrough
case "PUT": 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) io.Copy(bog_file, r.Body)
bog_file.Close() bog_file.Close()

@ -10,6 +10,7 @@ type UserAgentRepository interface{
} }
type FileDataRepository interface { type FileDataRepository interface {
OpenOrCreate(path string, user_agent string) (BogFileData, error) Create(path string, user_agent_label string) (BogInFile, error)
Delete(file_data *BogFileData) Open(path string, user_agent_label string) (BogOutFile, error)
Delete(path string, user_agent_label string)
} }

@ -1,30 +1,33 @@
package domain package domain
import "io" //import "io"
type BogFileService struct { type BogFileService struct {
user_agent_repo UserAgentRepository user_agent_repo UserAgentRepository
file_data_repo FileDataRepository file_data_repo FileDataRepository
} }
func (b *BogFileService) RecieveFile(src FileSource) error { func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error) {
f, err := b.file_data_repo.OpenOrCreate(src.Path(), src.UserAgent()) 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 { if err != nil {
return err return nil, err
} }
io.Copy(f, src) return f, err
return nil
} }
func (b *BogFileService) SendFile(dst FileDestination) error { func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) {
f, err := b.file_data_repo.OpenOrCreate(dst.Path(), dst.UserAgent()) 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 { if err != nil {
return err return nil, err
} }
io.Copy(dst, f) return f, nil
return nil
} }

@ -22,25 +22,25 @@ func (f *FileSizeQuota) Remove(size int64) {
f.CurrentUsage -= size f.CurrentUsage -= size
} }
type BogFileData interface { type BogOutFile interface {
Path() string Path() string
Size() int64 Size() int64
Modified() time.Time Modified() time.Time
io.Reader io.Reader
io.Seeker io.Seeker
io.Writer
io.Closer io.Closer
} }
type FileSource struct { type BogInFile interface {
Path string Path() string
UserAgent string Size() int64
Size int64 Modified() time.Time
Src io.ReadCloser io.Writer
io.Seeker
io.Closer
} }
type FileDestination struct { type FileReference struct {
Path string Path string
UserAgent string UserAgent string
Dst io.WriteCloser
} }

@ -9,6 +9,8 @@ import (
type FileSystemBogFileData struct { type FileSystemBogFileData struct {
path string path string
size int64
mod_time time.Time
file *os.File file *os.File
} }
@ -34,22 +36,24 @@ func (f FileSystemBogFileData) Path() string {
} }
func (f FileSystemBogFileData) Size() int64 { func (f FileSystemBogFileData) Size() int64 {
return 0 return f.size
} }
func (f FileSystemBogFileData) Modified() time.Time{ func (f FileSystemBogFileData) Modified() time.Time{
return time.Now() return time.Now()
} }
type FileSystemBogRepository struct { type FileSystemBogRepository struct {
Root string Root string
} }
func (f FileSystemBogRepository) OpenOrCreate(open_path string, user_agent string) (domain.BogFileData, error) { func (f FileSystemBogRepository) absPath(filename string, user_agent_label string) string {
//file_name := path.Base(open_path) return path.Join(f.Root, user_agent_label, filename)
abs_path := path.Join(f.Root, open_path) }
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) dir := path.Dir(abs_path)
os.MkdirAll(dir, 0750) os.MkdirAll(dir, 0750)
file, err := os.OpenFile(abs_path, os.O_RDWR|os.O_CREATE, 0644) 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) 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 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) {
} }

Loading…
Cancel
Save