lens @ 1e4613aa1113b373a8d841c28e222599237a33c5

feat: Add option to preload metadata

Let the use pick if they want to preload videos' metadata.
diff --git a/cmd/server/main.go b/cmd/server/main.go
index b940145af604f507d5625e711765dfb3c27dd86e..c7f901914667d673201e36568756c6201240f863 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -108,7 +108,7 @@ 	for _, v := range []view.View{
 		view.NewAuthView(userController),
 		view.NewFileSystemView(*fileSystemController, settingsRepository),
 		view.NewSettingsView(settingsRepository, userRepository),
-		view.NewMediaView(mediaRepository, userRepository),
+		view.NewMediaView(mediaRepository, userRepository, settingsRepository),
 	} {
 		v.SetMyselfIn(extRouter)
 	}
diff --git a/pkg/database/repository/settings.go b/pkg/database/repository/settings.go
index 6ed1eb61e66b2ddbc5f9ddcafc13a568e232e348..c3d8fbb94e4c82a841e151d158dd5b0ea5172e5d 100644
--- a/pkg/database/repository/settings.go
+++ b/pkg/database/repository/settings.go
@@ -4,8 +4,9 @@ import "context"
 
 type (
 	Settings struct {
-		ShowMode  bool
-		ShowOwner bool
+		ShowMode             bool
+		ShowOwner            bool
+		PreloadVideoMetadata bool
 	}
 
 	SettingsRepository interface {
diff --git a/pkg/database/sql/settings.go b/pkg/database/sql/settings.go
index 4e738789296ad2129eb5aa504461c312070f2fca..3f4ad08acee47dfe46a871170f97772cfb10099e 100644
--- a/pkg/database/sql/settings.go
+++ b/pkg/database/sql/settings.go
@@ -11,8 +11,9 @@
 type (
 	Settings struct {
 		gorm.Model
-		ShowMode  bool
-		ShowOwner bool
+		ShowMode             bool
+		ShowOwner            bool
+		PreloadVideoMetadata bool
 	}
 
 	SettingsRepository struct {
@@ -51,6 +52,7 @@ 	}
 
 	s.ShowMode = toSaveSettings.ShowMode
 	s.ShowOwner = toSaveSettings.ShowOwner
+	s.PreloadVideoMetadata = toSaveSettings.PreloadVideoMetadata
 
 	result := db.Save(s)
 	return result.Error
@@ -63,7 +65,8 @@ 		return nil, err
 	}
 
 	return &repository.Settings{
-		ShowMode:  s.ShowMode,
-		ShowOwner: s.ShowOwner,
+		ShowMode:             s.ShowMode,
+		ShowOwner:            s.ShowOwner,
+		PreloadVideoMetadata: s.PreloadVideoMetadata,
 	}, nil
 }
diff --git a/pkg/service/auth_test.go b/pkg/service/auth_test.go
index 7083d0c90ea75569f4e2532f86228a57c49df253..179ded71f0c45eda11fa0008b0e39982b8e1bfea 100644
--- a/pkg/service/auth_test.go
+++ b/pkg/service/auth_test.go
@@ -7,7 +7,6 @@ 	"context"
 	"testing"
 
 	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
-	"git.sr.ht/~gabrielgio/img/pkg/ext"
 	"git.sr.ht/~gabrielgio/img/pkg/testkit"
 )
 
diff --git a/pkg/view/media.go b/pkg/view/media.go
index e2479405d5a8e49a238872bad1eb74b2c665175b..6e34fd6c4505d418f9a57c57f20d9156866a9b2f 100644
--- a/pkg/view/media.go
+++ b/pkg/view/media.go
@@ -12,8 +12,9 @@ )
 
 type (
 	MediaView struct {
-		mediaRepository repository.MediaRepository
-		userRepository  repository.UserRepository
+		mediaRepository    repository.MediaRepository
+		userRepository     repository.UserRepository
+		settingsRepository repository.SettingsRepository
 	}
 )
 
@@ -50,10 +51,12 @@
 func NewMediaView(
 	mediaRepository repository.MediaRepository,
 	userRepository repository.UserRepository,
+	settingsRepository repository.SettingsRepository,
 ) *MediaView {
 	return &MediaView{
-		mediaRepository: mediaRepository,
-		userRepository:  userRepository,
+		mediaRepository:    mediaRepository,
+		userRepository:     userRepository,
+		settingsRepository: settingsRepository,
 	}
 }
 
@@ -72,12 +75,18 @@ 	if err != nil {
 		return err
 	}
 
+	settings, err := self.settingsRepository.Load(ctx)
+	if err != nil {
+		return err
+	}
+
 	page := &templates.MediaPage{
 		Medias: medias,
 		Next: &repository.Pagination{
 			Size: p.Size,
 			Page: p.Page + 1,
 		},
+		Settings: settings,
 	}
 
 	templates.WritePageTemplate(ctx, page)
diff --git a/pkg/view/settings.go b/pkg/view/settings.go
index bee3dc201b1f21d24ceb66ed7ae59df7cb95b491..ffce86b2ab39bfd887d06d37d2f22dfc7b324d13 100644
--- a/pkg/view/settings.go
+++ b/pkg/view/settings.go
@@ -47,13 +47,15 @@ }
 
 func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error {
 	var (
-		showMode  = string(ctx.FormValue("showMode")) == "on"
-		showOwner = string(ctx.FormValue("showOwner")) == "on"
+		showMode             = string(ctx.FormValue("showMode")) == "on"
+		showOwner            = string(ctx.FormValue("showOwner")) == "on"
+		preloadVideoMetadata = string(ctx.FormValue("preloadVideoMetadata")) == "on"
 	)
 
 	err := self.settingsRepository.Save(ctx, &repository.Settings{
-		ShowMode:  showMode,
-		ShowOwner: showOwner,
+		ShowMode:             showMode,
+		ShowOwner:            showOwner,
+		PreloadVideoMetadata: preloadVideoMetadata,
 	})
 	if err != nil {
 		return err
diff --git a/templates/media.qtpl b/templates/media.qtpl
index 18eac0df9a08dcd92a8ab70ac012eb459c8f43f8..621789945412ff643221945ab5ea59d590722f66 100644
--- a/templates/media.qtpl
+++ b/templates/media.qtpl
@@ -2,8 +2,16 @@ {% import "git.sr.ht/~gabrielgio/img/pkg/database/repository" %}
 
 {% code
 type MediaPage struct {
-	Medias []*repository.Media
-	Next   *repository.Pagination
+	Medias   []*repository.Media
+	Next     *repository.Pagination
+	Settings *repository.Settings
+}
+
+func (m *MediaPage) PreloadAttr() string {
+    if m.Settings.PreloadVideoMetadata {
+        return "metadata"
+    }
+    return "none"
 }
 %}
 
@@ -14,7 +22,7 @@ <div class="columns is-multiline">
 {% for _, media := range p.Medias %}
     <div class="card-image">
        {% if media.IsVideo() %}
-       <video class="image is-fit" controls muted="true" poster="/media/thumbnail?path_hash={%s media.PathHash %}" preload="metadata">
+       <video class="image is-fit" controls muted="true" poster="/media/thumbnail?path_hash={%s media.PathHash %}" preload="{%s p.PreloadAttr() %}">
            <source src="/media/image?path_hash={%s media.PathHash %}" type="{%s media.MIMEType %}">
        </video>
        {% else %}
diff --git a/templates/settings.qtpl b/templates/settings.qtpl
index 509cddd76e1a34dadd953a7d84a2f838cd3ade31..6eee1abd3d87e60ae598ea50651c5c04992932ec 100644
--- a/templates/settings.qtpl
+++ b/templates/settings.qtpl
@@ -30,6 +30,14 @@                     </label>
                 </div>
             </div>
             <div class="field">
+                <div class="control">
+                    <label class="checkbox">
+                        <input type="checkbox" id="preloadVideoMetadata" name="preloadVideoMetadata" {% if p.Settings.PreloadVideoMetadata %}checked{% endif %}>
+                        Preload video metadata
+                    </label>
+                </div>
+            </div>
+            <div class="field">
                 <input class="button" value="save" type="submit">
             </div>
         </form>