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
}
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 {
panic(err)
}
switch r.Method {
case "GET":
src := domainFileSource { bog_file.Path(),
http.ServeContent(w, r, , bog_file.Modified(), bog_file)
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()

@ -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)
}

@ -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
}

@ -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 {
type FileReference struct {
Path string
UserAgent string
Dst io.WriteCloser
}

@ -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) {
}

Loading…
Cancel
Save