Getting ready for BogFiles in database

master
Caj Larsson 3 years ago
parent f0c538551c
commit 288f8c4804

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

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

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

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

Loading…
Cancel
Save