Compare commits
No commits in common. "041922ea6de4a079135622596fea60e4b78ee63e" and "835e3bd010596ec03cfff2f42ca8405126ae6433" have entirely different histories.
041922ea6d
...
835e3bd010
1 changed files with 27 additions and 58 deletions
75
go/main.go
75
go/main.go
|
@ -23,16 +23,7 @@ var (
|
||||||
uploadInterval = 10 * time.Second
|
uploadInterval = 10 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
uploadDir = "./uploads"
|
|
||||||
cleanupInterval = 1 * time.Hour
|
|
||||||
fileLifetime = 48 * time.Hour
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Starten Sie den Hintergrundprozess zum Löschen alter Dateien
|
|
||||||
go startCleanupProcess()
|
|
||||||
|
|
||||||
http.HandleFunc("/", homeHandler)
|
http.HandleFunc("/", homeHandler)
|
||||||
http.HandleFunc("/upload", uploadHandler)
|
http.HandleFunc("/upload", uploadHandler)
|
||||||
http.HandleFunc("/image/", imageHandler)
|
http.HandleFunc("/image/", imageHandler)
|
||||||
|
@ -45,39 +36,6 @@ func main() {
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func startCleanupProcess() {
|
|
||||||
ticker := time.NewTicker(cleanupInterval)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
cleanupOldFiles()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanupOldFiles() {
|
|
||||||
now := time.Now()
|
|
||||||
err := filepath.Walk(uploadDir, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !info.IsDir() && now.Sub(info.ModTime()) > fileLifetime {
|
|
||||||
err := os.Remove(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Fehler beim Löschen der Datei %s: %v", path, err)
|
|
||||||
} else {
|
|
||||||
log.Printf("Datei gelöscht: %s", path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Fehler beim Durchsuchen des Verzeichnisses: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Setzen der Content Security Policy
|
// Setzen der Content Security Policy
|
||||||
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self'; object-src 'none';")
|
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self'; object-src 'none';")
|
||||||
|
@ -112,12 +70,15 @@ func generateNonce() (string, error) {
|
||||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
nonce, err := generateNonce()
|
nonce, err := generateNonce()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Fehlerbehandlung, z.B. Senden eines Serverfehlers
|
||||||
http.Error(w, "Serverfehler", http.StatusInternalServerError)
|
http.Error(w, "Serverfehler", http.StatusInternalServerError)
|
||||||
log.Printf("Fehler beim Generieren des Nonce: %v", err)
|
log.Printf("Fehler beim Generieren des Nonce: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setzen der Content Security Policy
|
||||||
w.Header().Set("Content-Security-Policy", fmt.Sprintf("script-src 'self' 'nonce-%s';", nonce))
|
w.Header().Set("Content-Security-Policy", fmt.Sprintf("script-src 'self' 'nonce-%s';", nonce))
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
@ -136,7 +97,8 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
buffer := make([]byte, 512)
|
// Überprüfen Sie den MIME-Typ der Datei
|
||||||
|
buffer := make([]byte, 512) // Genug für die Erkennung des MIME-Typs
|
||||||
_, err = file.Read(buffer)
|
_, err = file.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Fehler beim Lesen der Datei", http.StatusInternalServerError)
|
http.Error(w, "Fehler beim Lesen der Datei", http.StatusInternalServerError)
|
||||||
|
@ -144,12 +106,15 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
forceUpload := r.FormValue("force_upload")
|
||||||
|
if forceUpload != "true" {
|
||||||
mimeType := http.DetectContentType(buffer)
|
mimeType := http.DetectContentType(buffer)
|
||||||
if !strings.HasPrefix(mimeType, "image/") && !strings.HasPrefix(mimeType, "text/xml") && !strings.HasPrefix(mimeType, "image/svg+xml") {
|
if !strings.HasPrefix(mimeType, "image/") && !strings.HasPrefix(mimeType, "text/xml") && !strings.HasPrefix(mimeType, "image/svg+xml") {
|
||||||
http.Error(w, "Nur Bild-Uploads sind erlaubt", http.StatusBadRequest)
|
http.Error(w, "Nur Bild-Uploads sind erlaubt", http.StatusBadRequest)
|
||||||
log.Printf("Versuch, eine Nicht-Bild-Datei hochzuladen: %v", mimeType)
|
log.Printf("Versuch, eine Nicht-Bild-Datei hochzuladen: %v", mimeType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err = file.Seek(0, io.SeekStart)
|
_, err = file.Seek(0, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -158,26 +123,23 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
forceUpload := r.FormValue("force_upload")
|
// Ermitteln, ob der ursprüngliche Dateiname erzwungen werden soll
|
||||||
|
//forceName := r.Header.Get("Force-Name")
|
||||||
forceName := r.FormValue("force_name")
|
forceName := r.FormValue("force_name")
|
||||||
|
|
||||||
var filename string
|
var filename string
|
||||||
if forceName == "true" {
|
if forceName == "true" {
|
||||||
filename = handler.Filename
|
filename = handler.Filename
|
||||||
} else {
|
} else {
|
||||||
|
// Extrahiere nur die Dateiendung
|
||||||
fileExtension := filepath.Ext(handler.Filename)
|
fileExtension := filepath.Ext(handler.Filename)
|
||||||
|
|
||||||
|
// Zeitstempel zum Dateinamen hinzufügen
|
||||||
timestamp := time.Now().Format("20060102-150405")
|
timestamp := time.Now().Format("20060102-150405")
|
||||||
filename = fmt.Sprintf("%s%s", timestamp, fileExtension)
|
filename = fmt.Sprintf("%s%s", timestamp, fileExtension)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Datei speichern
|
||||||
uploadPath := "./uploads/" + filename
|
uploadPath := "./uploads/" + filename
|
||||||
|
|
||||||
if _, err := os.Stat(uploadPath); err == nil && forceUpload != "true" {
|
|
||||||
http.Error(w, "Datei existiert bereits. Überschreiben nicht erlaubt.", http.StatusConflict)
|
|
||||||
log.Printf("Versuch, bestehende Datei ohne force_upload zu überschreiben: %v", filename)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(uploadPath)
|
f, err := os.Create(uploadPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Fehler beim Erstellen der Datei", http.StatusInternalServerError)
|
http.Error(w, "Fehler beim Erstellen der Datei", http.StatusInternalServerError)
|
||||||
|
@ -193,13 +155,18 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lastUploadTime = time.Now()
|
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
||||||
|
|
||||||
|
// Vor dem Template-Rendering prüfen, ob eine JSON-Antwort erwartet wird
|
||||||
responseType := r.URL.Query().Get("responseType")
|
responseType := r.URL.Query().Get("responseType")
|
||||||
if responseType == "json" {
|
if responseType == "json" {
|
||||||
jsonResponse(w, nonce, filename)
|
jsonResponse(w, nonce, filename)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Template-Rendering-Logik, wenn keine JSON-Antwort erwartet wird
|
||||||
renderTemplate(w, nonce, filename)
|
renderTemplate(w, nonce, filename)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
tmpl, err := template.ParseFiles("templates/uploadForm.html")
|
tmpl, err := template.ParseFiles("templates/uploadForm.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -207,10 +174,12 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("Fehler beim Laden des Templates: %v", err)
|
log.Printf("Fehler beim Laden des Templates: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tmpl.Execute(w, nil)
|
err = tmpl.Execute(w, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Fehler beim Rendern des Templates", http.StatusInternalServerError)
|
http.Error(w, "Fehler beim Rendern des Templates", http.StatusInternalServerError)
|
||||||
log.Printf("Fehler beim Rendern des Templates: %v", err)
|
log.Printf("Fehler beim Rendern des Templates: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue