From f079a64a56e277f8994849e5991ca39e924532d2 Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Mon, 4 May 2020 17:53:58 +0200 Subject: [PATCH] support http header "range" (needed for videos in safari) --- README.md | 6 ++++++ http.go | 5 ++--- media_store.go | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4e01591..42cc1f8 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,13 @@ service photo-bot enable service photo-bot start ``` +## Useful notes +Video autoplay is tricky: + +- On Firefox, you have to interact with the page first (click somewhere in the page) +- On Safari, you have to [explicitly enable auto-play for this website](https://support.apple.com/fr-fr/guide/safari/ibrw29c6ecf8/mac) +- On Chrome, it seems to be enabled out-of-the-box ## Documentation diff --git a/http.go b/http.go index d3e90a1..20b28d8 100644 --- a/http.go +++ b/http.go @@ -2,7 +2,6 @@ package main import ( "html/template" - "io" "io/ioutil" "log" "net/http" @@ -200,14 +199,14 @@ func (bot *PhotoBot) HandleGetMedia(w http.ResponseWriter, r *http.Request, albu albumName = "" } - fd, err := bot.MediaStore.OpenFile(albumName, mediaFilename) + fd, modtime, err := bot.MediaStore.OpenFile(albumName, mediaFilename) if err != nil { log.Printf("MediaStore.OpenFile: %s", err) bot.HandleError(w, r) return } defer fd.Close() - io.Copy(w, fd) // Best effort + http.ServeContent(w, r, mediaFilename, modtime, fd) } func (bot *PhotoBot) ServeHTTP(w http.ResponseWriter, r *http.Request) { diff --git a/media_store.go b/media_store.go index b85dada..855b037 100644 --- a/media_store.go +++ b/media_store.go @@ -122,12 +122,23 @@ func (store *MediaStore) ListAlbums() (AlbumList, error) { return albums, nil } -func (store *MediaStore) OpenFile(albumName string, filename string) (*os.File, error) { +func (store *MediaStore) OpenFile(albumName string, filename string) (*os.File, time.Time, error) { if albumName == "" { albumName = ".current" } - return os.OpenFile(filepath.Join(store.StoreLocation, albumName, filename), os.O_RDONLY, 0600) + path := filepath.Join(store.StoreLocation, albumName, filename) + stat, err := os.Stat(path) + if err != nil { + return nil, time.Time{}, err + } + + fd, err := os.OpenFile(path, os.O_RDONLY, 0600) + if err != nil { + return nil, time.Time{}, err + } + + return fd, stat.ModTime(), nil } func (store *MediaStore) GetAlbum(name string, metadataOnly bool) (*Album, error) {