summaryrefslogtreecommitdiff
path: root/app/app.go
diff options
context:
space:
mode:
authorJames Mills <prologic@shortcircuit.net.au>2020-03-25 12:40:00 +1000
committerJames Mills <prologic@shortcircuit.net.au>2020-03-25 12:40:00 +1000
commit7113a7bd05c72406fc971fa720b05bb934732c04 (patch)
tree5275cce369c47d2163292705cdfe47575af7c4b8 /app/app.go
parent6fa6432090b4878701ff05cebd4f25658c761288 (diff)
Add support for keeping trakc of and viewing no. of views per video
Diffstat (limited to 'app/app.go')
-rw-r--r--app/app.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/app/app.go b/app/app.go
index 278d9ff..03937fc 100644
--- a/app/app.go
+++ b/app/app.go
@@ -29,6 +29,7 @@ import (
type App struct {
Config *Config
Library *media.Library
+ Store Store
Watcher *fsnotify.Watcher
Templates *templateStore
Feed []byte
@@ -46,6 +47,13 @@ func NewApp(cfg *Config) (*App, error) {
}
// Setup Library
a.Library = media.NewLibrary()
+ // Setup Store
+ store, err := NewBitcaskStore(cfg.Server.StorePath)
+ if err != nil {
+ err := fmt.Errorf("error opening store %s: %w", cfg.Server.StorePath, err)
+ return nil, err
+ }
+ a.Store = store
// Setup Watcher
w, err := fsnotify.NewWatcher()
if err != nil {
@@ -283,6 +291,15 @@ func (a *App) pageHandler(w http.ResponseWriter, r *http.Request) {
a.render("upload", w, ctx)
return
}
+
+ views, err := a.Store.GetViews(prefix, id)
+ if err != nil {
+ err := fmt.Errorf("error retrieving views for %s %s: %w", prefix, id, err)
+ log.Warn(err)
+ }
+
+ playing.Views = views
+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
ctx := &struct {
Playing *media.Video
@@ -307,6 +324,12 @@ func (a *App) videoHandler(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
+
+ if err := a.Store.IncView(prefix, id); err != nil {
+ err := fmt.Errorf("error updating view for %s %s: %w", prefix, id, err)
+ log.Warn(err)
+ }
+
title := m.Title
disposition := "attachment; filename=\"" + title + ".mp4\""
w.Header().Set("Content-Disposition", disposition)