This commit is contained in:
Christoph K.
2026-04-07 09:49:17 +02:00
parent 063aa67615
commit 4db170b467
37 changed files with 269 additions and 48 deletions

View File

@@ -1,8 +1,8 @@
package handler
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
@@ -15,6 +15,7 @@ const (
uploadDir = "uploads"
)
// handleListImages behandelt GET /api/v1/exercises/{id}/images.
func (h *Handler) handleListImages(w http.ResponseWriter, r *http.Request) {
exerciseID, err := pathID(r, "id")
if err != nil {
@@ -30,6 +31,9 @@ func (h *Handler) handleListImages(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, images)
}
// handleUploadImage behandelt POST /api/v1/exercises/{id}/images.
// Erwartet multipart/form-data mit Feld "image" (nur JPEG, max 5 MB).
// Die Datei wird unter uploads/<uuid>.jpg gespeichert.
func (h *Handler) handleUploadImage(w http.ResponseWriter, r *http.Request) {
exerciseID, err := pathID(r, "id")
if err != nil {
@@ -99,6 +103,8 @@ func (h *Handler) handleUploadImage(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusCreated, img)
}
// handleDeleteImage behandelt DELETE /api/v1/exercises/{id}/images/{imageId}.
// Löscht den Datenbankeintrag und die zugehörige Datei vom Dateisystem.
func (h *Handler) handleDeleteImage(w http.ResponseWriter, r *http.Request) {
imageID, err := pathID(r, "imageId")
if err != nil {
@@ -128,8 +134,8 @@ func (h *Handler) RegisterImageRoutes(mux *http.ServeMux) {
mux.Handle("GET /uploads/", http.StripPrefix("/uploads/",
http.FileServer(http.Dir(uploadDir))))
// uploads-Verzeichnis sicherstellen
os.MkdirAll(uploadDir, 0755)
fmt.Println("Bild-Upload konfiguriert:", uploadDir)
// uploads-Verzeichnis beim Start sicherstellen
if err := os.MkdirAll(uploadDir, 0755); err != nil {
log.Printf("Warnung: Upload-Verzeichnis konnte nicht erstellt werden: %v", err)
}
}