121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
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,
|
||
})
|
||
}
|