diff --git a/badge.go b/badge.go new file mode 100644 index 0000000..5ba36d7 --- /dev/null +++ b/badge.go @@ -0,0 +1,170 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "path/filepath" + "strings" +) + +var ( + audioBasePath = audioDir // percorso base dei file audio + imageBasePath = coversDir // percorso base delle immagini + badgeBasePath = "/badge/" // percorso per i badge + siteURL = baseURL + defaultImage = podLogo +) + +func badgeHandler(w http.ResponseWriter, r *http.Request) { + // Estrai il nome dell'episodio dall'URL + episode := strings.TrimPrefix(r.URL.Path, badgeBasePath) + if episode == "" { + http.Error(w, "Episode name required", http.StatusBadRequest) + return + } + + // Costruisci i percorsi dei file + mp3Path := filepath.Join(audioBasePath, episode+".mp3") + imgPath := filepath.Join(imageBasePath, episode+".jpg") + + // Verifica se il file MP3 esiste + if _, err := os.Stat("." + mp3Path); os.IsNotExist(err) { + http.Error(w, "Episode not found", http.StatusNotFound) + return + } + + // Costruisci gli URL completi + audioURL := siteURL + mp3Path + imageURL := siteURL + imgPath + + // Verifica se l'immagine esiste, altrimenti usa quella di default + if _, err := os.Stat("." + imgPath); os.IsNotExist(err) { + imageURL = defaultImage + } + + // Genera l'HTML completo del player + playerHTML := fmt.Sprintf(` + + + + + + + +
+ Cover +
%s
+
+ +
+
+
+
0:00
+
+ +
+ + + +`, imageURL, episode, audioURL) + + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(playerHTML)) +}