Add files via upload

This commit is contained in:
Tomáš Dvořák
2025-05-27 08:29:33 +02:00
committed by GitHub
parent 15be4969f6
commit 69551298ea
+10 -8
View File
@@ -36,13 +36,15 @@ func init() {
} }
const ( const (
bannerDataFile = "banner.json" // Changed from data/banner.json to root directory bannerDataFile = "data/banner.json"
uploadDir = "uploads" uploadDir = "uploads"
) )
// Ensure directories exist // Ensure directories exist
func ensureDirs() error { func ensureDirs() error {
// Create uploads directory if it doesn't exist if err := os.MkdirAll(filepath.Dir(bannerDataFile), 0755); err != nil {
return err
}
if err := os.MkdirAll(uploadDir, 0755); err != nil { if err := os.MkdirAll(uploadDir, 0755); err != nil {
return err return err
} }
@@ -58,7 +60,7 @@ type BannerContent struct {
type BannerStyle struct { type BannerStyle struct {
BackgroundColor string `json:"backgroundColor"` BackgroundColor string `json:"backgroundColor"`
TextColor string `json:"textColor"` Color string `json:"color"` // Use color instead of textColor
TextAlign string `json:"textAlign"` TextAlign string `json:"textAlign"`
FontSize string `json:"fontSize"` FontSize string `json:"fontSize"`
Padding string `json:"padding"` Padding string `json:"padding"`
@@ -94,7 +96,7 @@ func initDefaultBanner() {
Text: "Vítejte na našem webu!", Text: "Vítejte na našem webu!",
Style: BannerStyle{ Style: BannerStyle{
BackgroundColor: "#f8d7da", BackgroundColor: "#f8d7da",
TextColor: "#721c24", Color: "#721c24",
TextAlign: "center", TextAlign: "center",
FontSize: "18px", FontSize: "18px",
Padding: "20px", Padding: "20px",
@@ -199,9 +201,8 @@ func UpdateBannerHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Error creating file", http.StatusInternalServerError) http.Error(w, "Error creating file", http.StatusInternalServerError)
return return
} }
defer tempFile.Close()
// Copy the uploaded file to the destination file // Copy the file
if _, err := io.Copy(tempFile, file); err != nil { if _, err := io.Copy(tempFile, file); err != nil {
log.Printf("Error saving file: %v", err) log.Printf("Error saving file: %v", err)
http.Error(w, "Error saving file", http.StatusInternalServerError) http.Error(w, "Error saving file", http.StatusInternalServerError)
@@ -235,8 +236,9 @@ func UpdateBannerHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json") // Return the updated banner data
json.NewEncoder(w).Encode(banner) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(newBanner)
} }
// ServeUploads handles serving uploaded files // ServeUploads handles serving uploaded files