rework template load/generation + add generic error on template error
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user