Add files via upload

This commit is contained in:
Tomáš Dvořák
2025-05-21 13:12:46 +02:00
committed by GitHub
parent c31e86ed0c
commit db484a637b
2 changed files with 101 additions and 15 deletions
+63
View File
@@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
@@ -50,6 +51,8 @@ func main() {
http.ServeFile(w, r, "evidence-aut.html")
}))
http.HandleFunc("/open-folder", enableCORS(handleOpenFolder))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
@@ -156,6 +159,66 @@ func handleSubmit(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"message":"Záznam byl úspěšně uložen a email odeslán"}`))
}
// handleOpenFolder opens a Windows Explorer window to the specified folder path
func handleOpenFolder(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Only allow POST requests
if r.Method != http.MethodPost {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]string{"error": "Only POST method is allowed"})
return
}
// Parse the request body
type FolderRequest struct {
Path string `json:"path"`
}
var req FolderRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Printf("Chyba při čtení těla požadavku: %v", err)
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to parse request body"})
return
}
// Validate the folder path
if req.Path == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Path is required"})
return
}
// Sanitize the path (basic security - you might want to add more validation)
// Ensure it's a valid Windows network path
if !strings.HasPrefix(req.Path, "M:\\") {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Only network paths on M: drive are allowed"})
return
}
// Construct the command to open Windows Explorer
cmd := exec.Command("explorer.exe", req.Path)
// Run the command
err = cmd.Start()
if err != nil {
log.Printf("Chyba při spouštění explorer.exe: %v", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": fmt.Sprintf("Failed to open folder: %v", err)})
return
}
// Send a success response
json.NewEncoder(w).Encode(map[string]string{"status": "success", "message": "Folder opened successfully"})
}
func sendEmail(entry TripEntry, parsedDateStart, parsedDateEnd time.Time, czechMonths []string) error {
smtpHost := "mail.pp-kunovice.cz"
smtpPort := 465