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"
"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

3
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")

16
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

2
web/album.html.template

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

14
web/css/main.css

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