rework template load/generation + add generic error on template error

This commit is contained in:
rmanach
2025-01-07 16:55:36 +01:00
parent 40e08154f6
commit 708db35214
13 changed files with 501 additions and 449 deletions
+11 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"librapi/services"
"net/http"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
@@ -27,7 +28,7 @@ func getDownload(w http.ResponseWriter, r *http.Request, bs services.IStore) {
downloadFiles, ok := queryParams["file"]
if !ok {
log.Error().Msg("file query param does not exist")
http.Error(w, "file does not exists", http.StatusBadRequest)
http.Error(w, "file query param does not exist", http.StatusBadRequest)
return
}
@@ -40,6 +41,15 @@ func getDownload(w http.ResponseWriter, r *http.Request, bs services.IStore) {
filename := downloadFiles[0]
filePath := filepath.Join(bs.GetStoreDir(), filename)
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
http.Error(w, "file does not exist", http.StatusInternalServerError)
return
}
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
return
}
http.ServeFile(w, r, filePath)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))