add book store

This commit is contained in:
rmanach
2025-01-03 16:18:39 +01:00
parent e0d7eeadc3
commit ae27d07170
6 changed files with 89 additions and 31 deletions
+11 -23
View File
@@ -4,10 +4,8 @@ import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
@@ -17,7 +15,10 @@ import (
"librapi/templates"
)
const MaxFileSize = 200 // in MB
const (
URL = "/upload"
MaxFileSize = 200 // in MB
)
var (
ErrInvalidName = errors.New("book name must not be empty")
@@ -99,13 +100,13 @@ func (bf *BookForm) IsSuccess() bool {
return bf.Method == http.MethodPost && bf.Error == "" && !bf.HasErrors()
}
func Handler(a services.IAuthenticate) func(http.ResponseWriter, *http.Request) {
func Handler(a services.IAuthenticate, s services.IStore) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
getUploadFile(w, r)
case http.MethodPost:
postUploadFile(w, r, a)
postUploadFile(w, r, a, s)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
@@ -163,7 +164,7 @@ func extractBookForm(r *http.Request) BookForm {
return bf
}
func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenticate) {
func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenticate, s services.IStore) {
uploadForm := templates.GetUploadForm()
if !a.IsLogged(r) {
@@ -171,6 +172,7 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenti
if err := uploadForm.Execute(buf, &BookForm{Error: services.ErrUnauthorized.Error()}); err != nil {
log.Err(err).Msg("unable to generate template")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusUnauthorized)
@@ -183,6 +185,7 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenti
if err := uploadForm.Execute(buf, &bf); err != nil {
log.Err(err).Msg("unable to generate template")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
return
}
if bf.HasErrors() {
@@ -194,9 +197,8 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenti
filename := bf.File.Value.GetFilename()
log.Info().Str("filename", filename).Msg("file is uploading...")
dst, err := os.Create(filename)
if err != nil {
if err := uploadForm.Execute(buf, &BookForm{Error: "unexpected error occurred while creating file"}); err != nil {
if err := s.Save(filename, bf.File.Value.file); err != nil {
if err := uploadForm.Execute(buf, &BookForm{Error: err.Error()}); err != nil {
log.Err(err).Msg("unable to generate template")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
}
@@ -206,20 +208,6 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenti
return
}
defer dst.Close()
if _, err := io.Copy(dst, bf.File.Value.file); err != nil {
if err := uploadForm.Execute(buf, &BookForm{Error: "unexpected error occurred while uploading file"}); err != nil {
log.Err(err).Msg("unable to generate template")
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, buf.String())
return
}
buf.Reset()
if err := uploadForm.Execute(buf, &BookForm{Method: http.MethodPost}); err != nil {
log.Err(err).Msg("unable to generate template")