X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/8a42be0580469b8fb15fc3369724332f063dca4a..364c7ca21eae4dd7262c945f1feb10f136c3d064:/server/main.go diff --git a/server/main.go b/server/main.go index 9f6927e..363b708 100644 --- a/server/main.go +++ b/server/main.go @@ -5,8 +5,6 @@ import ( "embed" "flag" "fmt" - "golang.org/x/crypto/bcrypt" - "golang.org/x/net/websocket" "html/template" "io" "log" @@ -16,10 +14,13 @@ import ( "strconv" "strings" "time" + + "code.octet-stream.net/broadcaster/internal/protocol" + "golang.org/x/crypto/bcrypt" + "golang.org/x/net/websocket" ) -const version = "v1.0.0" -const formatString = "2006-01-02T15:04" +const version = "v1.1.0" //go:embed templates/* var content embed.FS @@ -77,7 +78,7 @@ func main() { // Public routes http.HandleFunc("/login", logInPage) - http.Handle("/file-downloads/", http.StripPrefix("/file-downloads/", http.FileServer(http.Dir(config.AudioFilesPath)))) + http.Handle("/file-downloads/", applyDisposition(http.StripPrefix("/file-downloads/", http.FileServer(http.Dir(config.AudioFilesPath))))) // Authenticated routes @@ -106,6 +107,24 @@ func main() { } } +type DispositionMiddleware struct { + handler http.Handler +} + +func (m DispositionMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Println("path", r.URL.Path) + if r.URL.Path != "/file-downloads/" { + w.Header().Add("Content-Disposition", "attachment") + } + m.handler.ServeHTTP(w, r) +} + +func applyDisposition(handler http.Handler) DispositionMiddleware { + return DispositionMiddleware{ + handler: handler, + } +} + type authenticatedHandler func(http.ResponseWriter, *http.Request, User) type AuthMiddleware struct { @@ -139,6 +158,7 @@ func requireAdmin(handler authenticatedHandler) AuthMiddleware { type HeaderData struct { SelectedMenu string User User + Version string } func renderHeader(w http.ResponseWriter, selectedMenu string, user User) { @@ -146,6 +166,7 @@ func renderHeader(w http.ResponseWriter, selectedMenu string, user User) { data := HeaderData{ SelectedMenu: selectedMenu, User: user, + Version: version, } err := tmpl.Execute(w, data) if err != nil { @@ -456,6 +477,9 @@ func playlistsPage(w http.ResponseWriter, _ *http.Request, user User) { data := PlaylistsPageData{ Playlists: db.GetPlaylists(), } + for i := range data.Playlists { + data.Playlists[i].StartTime = strings.Replace(data.Playlists[i].StartTime, "T", " ", -1) + } tmpl := template.Must(template.ParseFS(content, "templates/playlists.html")) err := tmpl.Execute(w, data) if err != nil { @@ -495,7 +519,7 @@ func editPlaylistPage(w http.ResponseWriter, r *http.Request, id int, user User) if id == 0 { data.Playlist.Enabled = true data.Playlist.Name = "New Playlist" - data.Playlist.StartTime = time.Now().Format(formatString) + data.Playlist.StartTime = time.Now().Format(protocol.StartTimeFormatSecs) data.Entries = append(data.Entries, PlaylistEntry{}) } else { playlist, err := db.GetPlaylist(id) @@ -506,7 +530,7 @@ func editPlaylistPage(w http.ResponseWriter, r *http.Request, id int, user User) data.Playlist = playlist data.Entries = db.GetEntriesForPlaylist(id) } - renderHeader(w, "radios", user) + renderHeader(w, "playlists", user) tmpl := template.Must(template.ParseFS(content, "templates/playlist.html")) tmpl.Execute(w, data) renderFooter(w) @@ -520,7 +544,10 @@ func submitPlaylist(w http.ResponseWriter, r *http.Request) { if err != nil { return } - _, err = time.Parse(formatString, r.Form.Get("playlistStartTime")) + _, err = time.Parse(protocol.StartTimeFormatSecs, r.Form.Get("playlistStartTime")) + if err != nil { + _, err = time.Parse(protocol.StartTimeFormat, r.Form.Get("playlistStartTime")) + } if err != nil { return }