restructuration
This commit is contained in:
+60
-14
@@ -1,14 +1,17 @@
|
||||
package forms
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"librapi/services"
|
||||
"mime/multipart"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"librapi/services"
|
||||
)
|
||||
|
||||
const MaxFileSize = 200 // in MB
|
||||
@@ -18,12 +21,14 @@ var (
|
||||
ErrInvalidPassword = errors.New("password must not be empty")
|
||||
ErrInvalidCredentials = errors.New("bad credentials")
|
||||
|
||||
ErrInvalidName = errors.New("book name must not be empty")
|
||||
ErrInvalidEditor = errors.New("book editor must not be empty")
|
||||
ErrInvalidName = errors.New("resource name must not be empty")
|
||||
ErrInvalidEditor = errors.New("resource editor must not be empty")
|
||||
ErrInvalidYear = errors.New("invalid year, unable to parse")
|
||||
ErrInvalidYearRange = errors.New("invalid year, can't be greater than today")
|
||||
ErrInvalidAuthors = errors.New("must at least contains one author")
|
||||
ErrFileMaxSizeReached = errors.New("max file size reached, must be <= 200MB")
|
||||
ErrFileOpen = errors.New("unable to open file from form")
|
||||
ErrFileUnreadable = errors.New("unable to read the file")
|
||||
)
|
||||
|
||||
type StrList = []string
|
||||
@@ -39,16 +44,17 @@ type FormField[T FormFieldType] struct {
|
||||
}
|
||||
|
||||
type UploadFile struct {
|
||||
File multipart.File
|
||||
Header *multipart.FileHeader
|
||||
filename string
|
||||
content []byte
|
||||
size int64
|
||||
}
|
||||
|
||||
func (uf *UploadFile) GetFilename() string {
|
||||
return uf.Header.Filename
|
||||
return uf.filename
|
||||
}
|
||||
|
||||
func (uf *UploadFile) CheckSize() error {
|
||||
if uf.Header.Size > (MaxFileSize << 20) {
|
||||
if uf.size > (MaxFileSize << 20) {
|
||||
return ErrFileMaxSizeReached
|
||||
}
|
||||
return nil
|
||||
@@ -94,22 +100,37 @@ func UploadFormFromRequest(r *http.Request) UploadForm {
|
||||
log.Err(errParse).Msg("unable to parse date")
|
||||
uf.Year.Err = ErrInvalidYear.Error()
|
||||
}
|
||||
if year > time.Now().Year() {
|
||||
log.Error().Msg("bad date range")
|
||||
uf.Year.Err = ErrInvalidYearRange.Error()
|
||||
}
|
||||
uf.Year.Value = year
|
||||
|
||||
if kw := r.FormValue(uf.Keywords.Name); kw != "" {
|
||||
uf.Keywords.Value = strings.Split(kw, ",")
|
||||
}
|
||||
|
||||
uf.sanitize()
|
||||
|
||||
file, fileh, err := r.FormFile(uf.File.Name)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("unable to get file from form")
|
||||
uf.File.Err = ErrFileOpen.Error()
|
||||
return uf
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("unable to get read file from form")
|
||||
uf.File.Err = ErrFileOpen.Error()
|
||||
return uf
|
||||
}
|
||||
|
||||
uf.File.Value = UploadFile{
|
||||
File: file,
|
||||
Header: fileh,
|
||||
filename: fileh.Filename,
|
||||
content: content,
|
||||
size: fileh.Size,
|
||||
}
|
||||
|
||||
if err := uf.File.Value.CheckSize(); err != nil {
|
||||
@@ -159,13 +180,14 @@ func (uf *UploadForm) IsSuccess() bool {
|
||||
return uf.Method == http.MethodPost && uf.Error == "" && !uf.HasErrors()
|
||||
}
|
||||
|
||||
func (uf *UploadForm) IntoMetadata() *services.BookMetadata {
|
||||
bm := &services.BookMetadata{
|
||||
func (uf *UploadForm) IntoResource() *services.Resource {
|
||||
bm := &services.Resource{
|
||||
Name: uf.Name.Value,
|
||||
Editor: uf.Editor.Value,
|
||||
Authors: uf.Authors.Value,
|
||||
Year: uint16(uf.Year.Value),
|
||||
Keywords: nil,
|
||||
Content: bytes.NewBuffer(uf.File.Value.content),
|
||||
}
|
||||
|
||||
if desc := uf.Description.Value; desc != "" {
|
||||
@@ -179,6 +201,30 @@ func (uf *UploadForm) IntoMetadata() *services.BookMetadata {
|
||||
return bm
|
||||
}
|
||||
|
||||
func (uf *UploadForm) sanitize() {
|
||||
uf.Name.Value = strings.TrimSpace(uf.Name.Value)
|
||||
uf.Editor.Value = strings.TrimSpace(uf.Editor.Value)
|
||||
uf.Description.Value = strings.TrimSpace(uf.Description.Value)
|
||||
|
||||
authors := []string{}
|
||||
for _, a := range uf.Authors.Value {
|
||||
if a == "" {
|
||||
continue
|
||||
}
|
||||
authors = append(authors, strings.TrimSpace(a))
|
||||
}
|
||||
uf.Authors.Value = authors
|
||||
|
||||
keywords := []string{}
|
||||
for _, k := range uf.Keywords.Value {
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
keywords = append(keywords, strings.TrimSpace(k))
|
||||
}
|
||||
uf.Keywords.Value = keywords
|
||||
}
|
||||
|
||||
type LoginForm struct {
|
||||
Username FormField[string]
|
||||
Password FormField[string]
|
||||
@@ -228,12 +274,12 @@ type SearchForm struct {
|
||||
Search FormField[string]
|
||||
Error error
|
||||
Method string
|
||||
Results []services.BookMetadata
|
||||
Results []services.Resource
|
||||
}
|
||||
|
||||
func SearchFormFromRequest(r *http.Request) SearchForm {
|
||||
sf := NewSearchForm()
|
||||
sf.Search.Value = r.FormValue(sf.Search.Name)
|
||||
sf.Search.Value = strings.TrimSpace(r.FormValue(sf.Search.Name))
|
||||
return sf
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user