Getting ready for BogFiles in database

master
Caj Larsson 3 years ago
parent f0c538551c
commit 288f8c4804

@ -3,7 +3,8 @@ package application
import ( import (
"net/http" "net/http"
"fmt" "fmt"
"io" "strconv"
// "io"
"caj-larsson/bog/domain" "caj-larsson/bog/domain"
"caj-larsson/bog/integration" "caj-larsson/bog/integration"
) )
@ -41,23 +42,33 @@ func buildHttpMux(file_service domain.BogFileService) *http.ServeMux {
case "GET": case "GET":
bog_file, err := file_service.OpenOutFile(ref) bog_file, err := file_service.OpenOutFile(ref)
if err == domain.ErrNotExists {
http.NotFound(w, r)
return
}
if err != nil { if err != nil {
panic(err) panic(err)
} }
http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file) http.ServeContent(w, r, bog_file.Path(), bog_file.Modified(), bog_file)
case "POST": case "POST":
fallthrough fallthrough
case "PUT": 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 { if err != nil {
panic(err) w.WriteHeader(422)
return
} }
io.Copy(bog_file, r.Body) err = file_service.SaveFile(ref, r.Body, size)
bog_file.Close()
if err != nil {
panic(err)
}
} }
return return
}) })

@ -22,6 +22,7 @@ type BogFile struct {
var ( var (
ErrDuplicate = errors.New("record already exists") ErrDuplicate = errors.New("record already exists")
ErrExceedQuota = errors.New("file too large")
ErrNotExists = errors.New("row not exists") ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed") ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed") ErrDeleteFailed = errors.New("delete failed")

@ -1,7 +1,7 @@
package domain package domain
import ( import (
// "io" "io"
"time" "time"
"strconv" "strconv"
) )
@ -16,10 +16,13 @@ func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileD
return BogFileService {user_agent_repo, file_data_repo} 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) user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent)
if !user_agent.FileQuota.Allows(size) {
return ErrExceedQuota
}
if err == ErrNotExists { if err == ErrNotExists {
// TODO make this into a factory method? // TODO make this into a factory method?
new_ua := UserAgent { new_ua := UserAgent {
@ -35,14 +38,16 @@ func (b BogFileService) CreateOrOpenInFile(ref FileReference) (BogInFile ,error)
panic(err) panic(err)
} }
f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10)) f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil { 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) { 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) file, err := os.OpenFile(abs_path, os.O_RDONLY, 0644)
if err != nil { if err != nil {
panic(err) return nil, domain.ErrNotExists
} }
bfd := FileSystemBogFileData {filename, 0, time.Now(), file} bfd := FileSystemBogFileData {filename, 0, time.Now(), file}

Loading…
Cancel
Save