diff --git a/http.go b/http.go index 0098650..d3e90a1 100644 --- a/http.go +++ b/http.go @@ -7,10 +7,13 @@ import ( "log" "net/http" "path" + "sort" "strings" + "time" _ "github.com/nmasse-itix/Telegram-Photo-Album-Bot/statik" "github.com/rakyll/statik/fs" + "github.com/spf13/viper" ) func slurpFile(statikFS http.FileSystem, filename string) (string, error) { @@ -52,6 +55,9 @@ func getTemplate(statikFS http.FileSystem, filename string, name string) (*templ } return "" }, + "short": func(t time.Time) string { + return t.Format("2006-01") + }, } return tmpl.Funcs(customFunctions).Parse(content) @@ -76,6 +82,7 @@ func ShiftPath(p string) (head, tail string) { type WebInterface struct { AlbumTemplate *template.Template MediaTemplate *template.Template + IndexTemplate *template.Template } func (bot *PhotoBot) ServeWebInterface(listenAddr string) { @@ -94,6 +101,11 @@ func (bot *PhotoBot) ServeWebInterface(listenAddr string) { log.Fatal(err) } + bot.WebInterface.IndexTemplate, err = getTemplate(statikFS, "/index.html.template", "index") + if err != nil { + log.Fatal(err) + } + router := http.NewServeMux() router.Handle("/js/", http.FileServer(statikFS)) router.Handle("/css/", http.FileServer(statikFS)) @@ -121,7 +133,7 @@ func (bot *PhotoBot) HandleDisplayAlbum(w http.ResponseWriter, r *http.Request, album, err := bot.MediaStore.GetAlbum(albumName, false) if err != nil { - log.Printf("GetAlbum: %s", err) + log.Printf("MediaStore.GetAlbum: %s", err) bot.HandleError(w, r) return } @@ -134,6 +146,29 @@ func (bot *PhotoBot) HandleDisplayAlbum(w http.ResponseWriter, r *http.Request, } } +func (bot *PhotoBot) HandleDisplayIndex(w http.ResponseWriter, r *http.Request) { + albums, err := bot.MediaStore.ListAlbums() + if err != nil { + log.Printf("MediaStore.ListAlbums: %s", err) + bot.HandleError(w, r) + return + } + + sort.Sort(sort.Reverse(albums)) + err = bot.WebInterface.IndexTemplate.Execute(w, struct { + Title string + Albums []Album + }{ + viper.GetString("SiteName"), + albums, + }) + if err != nil { + log.Printf("Template.Execute: %s", err) + bot.HandleError(w, r) + return + } +} + func (bot *PhotoBot) HandleDisplayMedia(w http.ResponseWriter, r *http.Request, albumName string, mediaId string) { if albumName == "latest" { albumName = "" @@ -202,8 +237,19 @@ func (bot *PhotoBot) ServeHTTP(w http.ResponseWriter, r *http.Request) { bot.HandleDisplayMedia(w, r, albumName, media) return } + } else { + if !strings.HasSuffix(originalPath, "/") { + http.Redirect(w, r, originalPath+"/", http.StatusMovedPermanently) + return + } + bot.HandleDisplayIndex(w, r) + return } + } else if resource == "" { + http.Redirect(w, r, "/album/", http.StatusMovedPermanently) + return } + default: http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return diff --git a/main.go b/main.go index bce6d0e..c1f735f 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ func initConfig() { viper.SetDefault("RetryDelay", 60) // max duration between two telegram updates viper.SetDefault("TelegramNewUpdateTimeout", 60) + viper.SetDefault("SiteName", "My photo album") // Default messages viper.SetDefault("MsgForbidden", "Access Denied") @@ -63,7 +64,7 @@ func validateConfig() { retryDelay := viper.GetInt("RetryDelay") if retryDelay <= 0 { - log.Fatal("The TelegramNewUpdateTimeout cannot be zero or negative!") + log.Fatal("The RetryDelay cannot be zero or negative!") } timeout := viper.GetInt("TelegramNewUpdateTimeout") diff --git a/media_store.go b/media_store.go index 1badf91..b85dada 100644 --- a/media_store.go +++ b/media_store.go @@ -89,7 +89,21 @@ func appendToFile(filename string, data []byte) error { return nil } -func (store *MediaStore) ListAlbums() ([]Album, error) { +type AlbumList []Album + +func (list AlbumList) Len() int { + return len(list) +} + +func (list AlbumList) Less(i, j int) bool { + return list[i].Date.Before(list[j].Date) +} + +func (list AlbumList) Swap(i, j int) { + list[i], list[j] = list[j], list[i] +} + +func (store *MediaStore) ListAlbums() (AlbumList, error) { files, err := ioutil.ReadDir(store.StoreLocation) if err != nil { return nil, err diff --git a/web/album.html.template b/web/album.html.template index 934c2e9..bad8b6c 100644 --- a/web/album.html.template +++ b/web/album.html.template @@ -7,7 +7,7 @@ - +

{{ .Title }}