blitzer-v2/upload/main.go
2025-08-10 20:27:21 +01:00

121 lines
3.1 KiB
Go
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package upload
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"git.ctdo.de/henne/blitzer-v2/db"
"github.com/gin-gonic/gin"
)
var uploadHost string = "https://traffic.dre.li/api/"
//var uploadHost string = "http://localhost:3000/api/"
func UploadTicket(ticket db.SpeedingTicket) error {
// Buffer für den Request-Body
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
filePath := fmt.Sprintf("images/original/ticket-%s.jpg", ticket.ID.String())
// 1⃣ JSON-Payload als Form-Feld hinzufügen
jsonData, err := json.Marshal(ticket)
if err != nil {
return fmt.Errorf("fehler beim Marshallen: %w", err)
}
err = writer.WriteField("payload", string(jsonData))
if err != nil {
return fmt.Errorf("fehler beim Hinzufügen des JSON-Feldes: %w", err)
}
// 2⃣ Datei hinzufügen
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("fehler beim Öffnen der Datei: %w", err)
}
defer file.Close()
filePart, err := writer.CreateFormFile("file", filePath)
if err != nil {
return fmt.Errorf("fehler beim Erstellen des Datei-Feldes: %w", err)
}
_, err = io.Copy(filePart, file)
if err != nil {
return fmt.Errorf("fehler beim Kopieren der Datei: %w", err)
}
// Multipart-Writer schließen (setzt den Boundary korrekt)
err = writer.Close()
if err != nil {
return fmt.Errorf("fehler beim Schließen des Writers: %w", err)
}
// 3⃣ Request erstellen & senden
req, err := http.NewRequest("POST", uploadHost+"ticket", body)
if err != nil {
return fmt.Errorf("fehler beim Erstellen des Requests: %w", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("fehler beim Senden: %w", err)
}
defer resp.Body.Close()
// Optional: Antwort prüfen
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("server returned %s: %s", resp.Status, string(respBody))
}
log.Printf("uploaded picture")
return nil
}
func HandleUpload(c *gin.Context) {
// 1⃣ JSON-Feld "payload" auslesen
payloadStr := c.PostForm("payload")
if payloadStr == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "payload fehlt"})
return
}
var payload db.SpeedingTicket
if err := json.Unmarshal([]byte(payloadStr), &payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "ungültiges JSON", "details": err.Error()})
return
}
db.DB.Create(&payload)
fmt.Println("Empfangene Daten:", payload)
// 2⃣ Datei auslesen
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Datei fehlt", "details": err.Error()})
return
}
// Datei speichern
filename := fmt.Sprintf("images/original/ticket-%s.jpg", payload.ID.String())
if err := c.SaveUploadedFile(file, filename); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Fehler beim Speichern", "details": err.Error()})
return
}
// 3⃣ Antwort
c.JSON(http.StatusOK, gin.H{
"status": "ok",
"file": filename,
"data": payload,
})
}