add json support
This commit is contained in:
parent
c7fdd04e88
commit
bd0ca05155
2 changed files with 51 additions and 20 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
uploads/
|
uploads/
|
||||||
|
README-SECRET.md
|
||||||
|
|
70
go/main.go
70
go/main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
@ -156,29 +157,15 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("templates/uploadSuccess.html")
|
// Vor dem Template-Rendering prüfen, ob eine JSON-Antwort erwartet wird
|
||||||
if err != nil {
|
responseType := r.URL.Query().Get("responseType")
|
||||||
http.Error(w, "Fehler beim Laden des Templates", http.StatusInternalServerError)
|
if responseType == "json" {
|
||||||
log.Printf("Fehler beim Laden des Templates: %v", err)
|
jsonResponse(w, nonce, filename)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
// Template-Rendering-Logik, wenn keine JSON-Antwort erwartet wird
|
||||||
Message string
|
renderTemplate(w, nonce, filename)
|
||||||
Filename string
|
|
||||||
Nonce string
|
|
||||||
}{
|
|
||||||
Message: "Bild erfolgreich hochgeladen.",
|
|
||||||
Filename: filename, // Geändert, um den möglicherweise modifizierten Dateinamen anzuzeigen
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tmpl.Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Fehler beim Rendern des Templates", http.StatusInternalServerError)
|
|
||||||
log.Printf("Fehler beim Rendern des Templates: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
tmpl, err := template.ParseFiles("templates/uploadForm.html")
|
tmpl, err := template.ParseFiles("templates/uploadForm.html")
|
||||||
|
@ -197,6 +184,49 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsonResponse(w http.ResponseWriter, nonce string, filename string) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
response := struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Filename string `json:"filename"`
|
||||||
|
Nonce string `json:"nonce"`
|
||||||
|
}{
|
||||||
|
Message: "Bild erfolgreich hochgeladen.",
|
||||||
|
Filename: filename,
|
||||||
|
Nonce: nonce,
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, nonce string, filename string) {
|
||||||
|
// Implementierung des Template-Renderings
|
||||||
|
tmpl, err := template.ParseFiles("templates/uploadSuccess.html")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Fehler beim Laden des Templates", http.StatusInternalServerError)
|
||||||
|
log.Printf("Fehler beim Laden des Templates: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := struct {
|
||||||
|
Message string
|
||||||
|
Filename string
|
||||||
|
Nonce string
|
||||||
|
}{
|
||||||
|
Message: "Bild erfolgreich hochgeladen.",
|
||||||
|
Filename: filename, // Geändert, um den möglicherweise modifizierten Dateinamen anzuzeigen
|
||||||
|
Nonce: nonce,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tmpl.Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Fehler beim Rendern des Templates", http.StatusInternalServerError)
|
||||||
|
log.Printf("Fehler beim Rendern des Templates: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Funktion zur Ermittlung des MIME-Types basierend auf der Dateiendung
|
// Funktion zur Ermittlung des MIME-Types basierend auf der Dateiendung
|
||||||
func getMimeType(filePath string) string {
|
func getMimeType(filePath string) string {
|
||||||
switch filepath.Ext(filePath) {
|
switch filepath.Ext(filePath) {
|
||||||
|
|
Loading…
Reference in a new issue