U-pod/badge.go

171 lines
4.7 KiB
Go

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(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.podcast-badge {
width: 300px;
font-family: Arial, sans-serif;
background: #f5f5f5;
border-radius: 8px;
padding: 12px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.podcast-cover {
width: 100%%;
border-radius: 5px;
margin-bottom: 10px;
}
.podcast-title {
font-weight: bold;
margin: 5px 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.podcast-controls {
display: flex;
align-items: center;
gap: 10px;
}
.play-button {
background: #4CAF50;
color: white;
border: none;
border-radius: 50%%;
width: 40px;
height: 40px;
font-size: 16px;
cursor: pointer;
}
.progress-container {
flex-grow: 1;
height: 4px;
background: #ddd;
border-radius: 2px;
cursor: pointer;
}
.progress-bar {
height: 100%%;
background: #4CAF50;
width: 0%%;
border-radius: 2px;
}
.time-display {
font-size: 12px;
color: #666;
min-width: 50px;
text-align: right;
}
</style>
</head>
<body>
<div class="podcast-badge">
<img src="%s" alt="Cover" class="podcast-cover">
<div class="podcast-title">%s</div>
<div class="podcast-controls">
<button class="play-button" onclick="togglePlay()">▶</button>
<div class="progress-container" onclick="seek(event)">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="time-display" id="timeDisplay">0:00</div>
</div>
<audio id="audioPlayer" src="%s"></audio>
</div>
<script>
const player = document.getElementById('audioPlayer');
const progressBar = document.getElementById('progressBar');
const timeDisplay = document.getElementById('timeDisplay');
let isPlaying = false;
function togglePlay() {
if (isPlaying) {
player.pause();
} else {
player.play();
}
isPlaying = !isPlaying;
updateButton();
}
function updateButton() {
const button = document.querySelector('.play-button');
button.textContent = isPlaying ? '❚❚' : '▶';
}
function seek(e) {
const percent = e.offsetX / e.target.offsetWidth;
player.currentTime = percent * player.duration;
}
player.addEventListener('timeupdate', function() {
// Aggiorna la barra di progresso
const progress = (player.currentTime / player.duration) * 100;
progressBar.style.width = progress + '%%';
// Aggiorna il tempo
const minutes = Math.floor(player.currentTime / 60);
const seconds = Math.floor(player.currentTime %% 60);
timeDisplay.textContent = minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
});
player.addEventListener('ended', function() {
isPlaying = false;
updateButton();
});
</script>
</body>
</html>`, imageURL, episode, audioURL)
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(playerHTML))
}