Browse Source

add main page

master
Nicolas Massé 6 years ago
parent
commit
aad72602f5
  1. 48
      http.go
  2. 3
      main.go
  3. 16
      media_store.go
  4. 2
      web/album.html.template
  5. 14
      web/css/main.css
  6. 23
      web/index.html.template

48
http.go

@ -7,10 +7,13 @@ import (
"log" "log"
"net/http" "net/http"
"path" "path"
"sort"
"strings" "strings"
"time"
_ "github.com/nmasse-itix/Telegram-Photo-Album-Bot/statik" _ "github.com/nmasse-itix/Telegram-Photo-Album-Bot/statik"
"github.com/rakyll/statik/fs" "github.com/rakyll/statik/fs"
"github.com/spf13/viper"
) )
func slurpFile(statikFS http.FileSystem, filename string) (string, error) { func slurpFile(statikFS http.FileSystem, filename string) (string, error) {
@ -52,6 +55,9 @@ func getTemplate(statikFS http.FileSystem, filename string, name string) (*templ
} }
return "" return ""
}, },
"short": func(t time.Time) string {
return t.Format("2006-01")
},
} }
return tmpl.Funcs(customFunctions).Parse(content) return tmpl.Funcs(customFunctions).Parse(content)
@ -76,6 +82,7 @@ func ShiftPath(p string) (head, tail string) {
type WebInterface struct { type WebInterface struct {
AlbumTemplate *template.Template AlbumTemplate *template.Template
MediaTemplate *template.Template MediaTemplate *template.Template
IndexTemplate *template.Template
} }
func (bot *PhotoBot) ServeWebInterface(listenAddr string) { func (bot *PhotoBot) ServeWebInterface(listenAddr string) {
@ -94,6 +101,11 @@ func (bot *PhotoBot) ServeWebInterface(listenAddr string) {
log.Fatal(err) log.Fatal(err)
} }
bot.WebInterface.IndexTemplate, err = getTemplate(statikFS, "/index.html.template", "index")
if err != nil {
log.Fatal(err)
}
router := http.NewServeMux() router := http.NewServeMux()
router.Handle("/js/", http.FileServer(statikFS)) router.Handle("/js/", http.FileServer(statikFS))
router.Handle("/css/", 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) album, err := bot.MediaStore.GetAlbum(albumName, false)
if err != nil { if err != nil {
log.Printf("GetAlbum: %s", err) log.Printf("MediaStore.GetAlbum: %s", err)
bot.HandleError(w, r) bot.HandleError(w, r)
return 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) { func (bot *PhotoBot) HandleDisplayMedia(w http.ResponseWriter, r *http.Request, albumName string, mediaId string) {
if albumName == "latest" { if albumName == "latest" {
albumName = "" albumName = ""
@ -202,8 +237,19 @@ func (bot *PhotoBot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
bot.HandleDisplayMedia(w, r, albumName, media) bot.HandleDisplayMedia(w, r, albumName, media)
return 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: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return

3
main.go

@ -17,6 +17,7 @@ func initConfig() {
viper.SetDefault("RetryDelay", 60) viper.SetDefault("RetryDelay", 60)
// max duration between two telegram updates // max duration between two telegram updates
viper.SetDefault("TelegramNewUpdateTimeout", 60) viper.SetDefault("TelegramNewUpdateTimeout", 60)
viper.SetDefault("SiteName", "My photo album")
// Default messages // Default messages
viper.SetDefault("MsgForbidden", "Access Denied") viper.SetDefault("MsgForbidden", "Access Denied")
@ -63,7 +64,7 @@ func validateConfig() {
retryDelay := viper.GetInt("RetryDelay") retryDelay := viper.GetInt("RetryDelay")
if retryDelay <= 0 { 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") timeout := viper.GetInt("TelegramNewUpdateTimeout")

16
media_store.go

@ -89,7 +89,21 @@ func appendToFile(filename string, data []byte) error {
return nil 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) files, err := ioutil.ReadDir(store.StoreLocation)
if err != nil { if err != nil {
return nil, err return nil, err

2
web/album.html.template

@ -7,7 +7,7 @@
<link rel="stylesheet" href="/css/main.css"> <link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/main.js"></script> <script type="text/javascript" src="/js/main.js"></script>
</head> </head>
<body> <body class="album">
<h1>{{ .Title }}</h1> <h1>{{ .Title }}</h1>
<ul> <ul>
{{ range .Media }} {{ range .Media }}

14
web/css/main.css

@ -8,11 +8,11 @@ h1 {
/* Album */ /* Album */
body { body.album {
margin: 3vh; margin: 3vh;
} }
ul { .album ul {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
align-items: flex-start; align-items: flex-start;
@ -20,27 +20,27 @@ ul {
margin: 0; margin: 0;
} }
li { .album li {
height: 20vh; height: 20vh;
flex-grow: 0.5; flex-grow: 0.5;
list-style-type: none; list-style-type: none;
} }
li img, li video { .album li img, .album li video {
max-height: 100%; max-height: 100%;
min-width: 100%; min-width: 100%;
object-fit: cover; object-fit: cover;
vertical-align: bottom; vertical-align: bottom;
} }
li:last-child { .album li:last-child {
flex-grow: 10; flex-grow: 10;
} }
/* Album */ /* Album */
div { .media div {
height: 100%; height: 100%;
width: 100%; width: 100%;
display: flex; display: flex;
@ -62,6 +62,6 @@ body.media h1 {
} }
div img, div video { .media div img, .media div video {
object-fit: contain; object-fit: contain;
} }

23
web/index.html.template

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<link rel="stylesheet" href="/css/main.css">
</head>
<body class="index">
<h1>{{ .Title }}</h1>
<ul>
{{ range .Albums }}
<li>
{{ if eq .ID "" }}
<a href="latest/">{{ .Date|short }} {{ .Title }}</a>
{{ else }}
<a href="{{ .ID }}/">{{ .Date|short }} {{ .Title }}</a>
{{ end }}
</li>
{{ end }}
</ul>
</body>
</html>
Loading…
Cancel
Save