You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/domain/services.go

67 lines
1.3 KiB
Go

package domain
import (
"io"
"time"
"strconv"
)
type BogFileService struct {
user_agent_repo UserAgentRepository
file_data_repo FileDataRepository
}
func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileDataRepository) BogFileService {
return BogFileService {user_agent_repo, file_data_repo}
}
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 {
0, ref.UserAgent, time.Now(), time.Duration(time.Second * 400), FileSizeQuota {10, 10 },
}
user_agent, err = b.user_agent_repo.Create(new_ua)
if err != nil {
panic(err)
}
}
if err != nil {
panic(err)
}
f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil {
return err
}
io.Copy(f, src)
f.Close()
return nil
}
func (b BogFileService) OpenOutFile(ref FileReference) (BogOutFile, error) {
user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent)
if err == ErrNotExists {
return nil, err
}
f, err := b.file_data_repo.Open(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil {
return nil, err
}
return f, nil
}