]> code.octet-stream.net Git - broadcaster/blobdiff - server/main.go
More UI improvements
[broadcaster] / server / main.go
index 105e8581c077474241d2a8e7cbdd65bac7e6dc86..d1d47d329afe6029632e073b35d5f1af0fcc800d 100644 (file)
@@ -77,7 +77,7 @@ func main() {
        // Public routes
 
        http.HandleFunc("/login", logInPage)
        // 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
 
 
        // Authenticated routes
 
@@ -106,6 +106,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 {
 type authenticatedHandler func(http.ResponseWriter, *http.Request, User)
 
 type AuthMiddleware struct {
@@ -139,6 +157,7 @@ func requireAdmin(handler authenticatedHandler) AuthMiddleware {
 type HeaderData struct {
        SelectedMenu string
        User         User
 type HeaderData struct {
        SelectedMenu string
        User         User
+       Version      string
 }
 
 func renderHeader(w http.ResponseWriter, selectedMenu string, user User) {
 }
 
 func renderHeader(w http.ResponseWriter, selectedMenu string, user User) {
@@ -146,6 +165,7 @@ func renderHeader(w http.ResponseWriter, selectedMenu string, user User) {
        data := HeaderData{
                SelectedMenu: selectedMenu,
                User:         user,
        data := HeaderData{
                SelectedMenu: selectedMenu,
                User:         user,
+               Version:      version,
        }
        err := tmpl.Execute(w, data)
        if err != nil {
        }
        err := tmpl.Execute(w, data)
        if err != nil {
@@ -413,7 +433,7 @@ func changePasswordPage(w http.ResponseWriter, r *http.Request, user User) {
                        data.ShowForm = false
                        cookie, err := r.Cookie("broadcast_session")
                        if err == nil {
                        data.ShowForm = false
                        cookie, err := r.Cookie("broadcast_session")
                        if err == nil {
-                               log.Println("clearing other sessions for username", user.Username, "token", cookie.Value)
+                               log.Println("Clearing other sessions for username", user.Username, "token", cookie.Value)
                                db.ClearOtherSessions(user.Username, cookie.Value)
                        }
                }
                                db.ClearOtherSessions(user.Username, cookie.Value)
                        }
                }
@@ -456,6 +476,9 @@ func playlistsPage(w http.ResponseWriter, _ *http.Request, user User) {
        data := PlaylistsPageData{
                Playlists: db.GetPlaylists(),
        }
        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 {
        tmpl := template.Must(template.ParseFS(content, "templates/playlists.html"))
        err := tmpl.Execute(w, data)
        if err != nil {
@@ -506,7 +529,7 @@ func editPlaylistPage(w http.ResponseWriter, r *http.Request, id int, user User)
                data.Playlist = playlist
                data.Entries = db.GetEntriesForPlaylist(id)
        }
                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)
        tmpl := template.Must(template.ParseFS(content, "templates/playlist.html"))
        tmpl.Execute(w, data)
        renderFooter(w)
@@ -642,7 +665,6 @@ func filesPage(w http.ResponseWriter, _ *http.Request, user User) {
        data := FilesPageData{
                Files: files.Files(),
        }
        data := FilesPageData{
                Files: files.Files(),
        }
-       log.Println("file page data", data)
        tmpl := template.Must(template.ParseFS(content, "templates/files.html"))
        err := tmpl.Execute(w, data)
        if err != nil {
        tmpl := template.Must(template.ParseFS(content, "templates/files.html"))
        err := tmpl.Execute(w, data)
        if err != nil {
@@ -671,13 +693,17 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
                f, _ := os.Create(path)
                defer f.Close()
                io.Copy(f, file)
                f, _ := os.Create(path)
                defer f.Close()
                io.Copy(f, file)
-               log.Println("uploaded file to", path)
+               log.Println("Uploaded file to", path)
                files.Refresh()
        }
        http.Redirect(w, r, "/files/", http.StatusFound)
 }
 
 func logOutPage(w http.ResponseWriter, r *http.Request, user User) {
                files.Refresh()
        }
        http.Redirect(w, r, "/files/", http.StatusFound)
 }
 
 func logOutPage(w http.ResponseWriter, r *http.Request, user User) {
+       cookie, err := r.Cookie("broadcast_session")
+       if err == nil {
+               db.ClearSession(user.Username, cookie.Value)
+       }
        clearSessionCookie(w)
        renderHeader(w, "", user)
        tmpl := template.Must(template.ParseFS(content, "templates/logout.html"))
        clearSessionCookie(w)
        renderHeader(w, "", user)
        tmpl := template.Must(template.ParseFS(content, "templates/logout.html"))