diff --git a/pkg/migrations/migrations.go b/pkg/migrations/migrations.go index 9aefa3a76..01b991567 100644 --- a/pkg/migrations/migrations.go +++ b/pkg/migrations/migrations.go @@ -747,6 +747,15 @@ func Migrate() { return tx.AutoMigrate(Scene{}).Error }, }, + { + ID: "0069-file-refresh-heatmap-cache", + Migrate: func(tx *gorm.DB) error { + type File struct { + RefreshHeatmapCache bool `json:"refresh_heatmap_cache" gorm:"default:false"` + } + return tx.AutoMigrate(File{}).Error + }, + }, // =============================================================================================== // Put DB Schema migrations above this line and migrations that rely on the updated schema below diff --git a/pkg/models/model_file.go b/pkg/models/model_file.go index 5702e7d85..6242bbda2 100644 --- a/pkg/models/model_file.go +++ b/pkg/models/model_file.go @@ -38,9 +38,10 @@ type File struct { VideoDuration float64 `json:"duration" xbvrbackup:"duration"` VideoProjection string `json:"projection" xbvrbackup:"projection"` - HasHeatmap bool `json:"has_heatmap" xbvrbackup:"-"` - IsSelectedScript bool `json:"is_selected_script" xbvrbackup:"is_selected_script"` - IsExported bool `json:"is_exported" xbvrbackup:"-"` + HasHeatmap bool `json:"has_heatmap" xbvrbackup:"-"` + IsSelectedScript bool `json:"is_selected_script" xbvrbackup:"is_selected_script"` + IsExported bool `json:"is_exported" xbvrbackup:"-"` + RefreshHeatmapCache bool `json:"refresh_heatmap_cache" xbvrbackup:"-"` } func (f *File) GetPath() string { diff --git a/pkg/server/heatmapproxy.go b/pkg/server/heatmapproxy.go index bb7971a82..e19d098f4 100644 --- a/pkg/server/heatmapproxy.go +++ b/pkg/server/heatmapproxy.go @@ -60,28 +60,28 @@ func NewHeatmapThumbnailProxy(imageproxy *imageproxy.Proxy, cache imageproxy.Cac return proxy } -func getScriptFileIds(urlpart string) ([]uint, error) { +func getScriptFiles(urlpart string) ([]models.File, error) { sceneId, err := strconv.Atoi(urlpart) - ids := make([]uint, 0) + files := make([]models.File, 0) if err != nil { - return ids, err + return files, err } var scene models.Scene err = scene.GetIfExistByPK(uint(sceneId)) if err != nil { - return ids, err + return files, err } scriptfiles, err := scene.GetScriptFilesSorted(config.Config.Interfaces.Players.ScriptSortSeq) if err != nil || len(scriptfiles) < 1 { - return ids, fmt.Errorf("scene %d has no script files", sceneId) + return files, fmt.Errorf("scene %d has no script files", sceneId) } for i := range scriptfiles { - ids = append(ids, scriptfiles[i].ID) + files = append(files, scriptfiles[i]) } - return ids, nil + return files, nil } func getHeatmapImageForScene(fileId uint) (image.Image, error) { @@ -149,27 +149,39 @@ func (p *HeatmapThumbnailProxy) ServeHTTP(w http.ResponseWriter, r *http.Request } imageURL := parts[2] - fileIds, err := getScriptFileIds(parts[1]) + files, err := getScriptFiles(parts[1]) if err != nil { p.serveImageproxyResponse(w, r, imageURL) return } - cacheKey := fmt.Sprintf("%d:%s", fileIds[0], imageURL) - cachedContent, ok := p.Cache.Get(cacheKey) - if ok { - w.Header().Add("Content-Type", "image/jpeg") - w.Header().Add("Content-Length", fmt.Sprint(len(cachedContent))) - if _, err := io.Copy(w, bytes.NewReader(cachedContent)); err != nil { - log.Printf("Failed to send out response: %v", err) + loadFromCache := true + + for i := range files { + if files[i].RefreshHeatmapCache { + loadFromCache = false + break + } + } + + cacheKey := fmt.Sprintf("%d:%s", files[0].ID, imageURL) + + if loadFromCache { + cachedContent, ok := p.Cache.Get(cacheKey) + if ok { + w.Header().Add("Content-Type", "image/jpeg") + w.Header().Add("Content-Length", fmt.Sprint(len(cachedContent))) + if _, err := io.Copy(w, bytes.NewReader(cachedContent)); err != nil { + log.Printf("Failed to send out response: %v", err) + } + return } - return } heatmapImages := make([]image.Image, 0) - for i := range fileIds { - heatmapImage, err := getHeatmapImageForScene(fileIds[i]) + for i := range files { + heatmapImage, err := getHeatmapImageForScene(files[i].ID) if err == nil { heatmapImages = append(heatmapImages, heatmapImage) if len(heatmapImages) == maximumHeatmaps { @@ -183,6 +195,12 @@ func (p *HeatmapThumbnailProxy) ServeHTTP(w http.ResponseWriter, r *http.Request return } + for i := range files { + file := files[i] + file.RefreshHeatmapCache = false + file.Save() + } + heatmapsHeight := len(heatmapImages) * (heatmapHeight + heatmapMargin) proxyURL := fmt.Sprintf("/%dx%d,jpeg/%s", thumbnailWidth, thumbnailHeight-heatmapsHeight, imageURL) r2 := new(http.Request) diff --git a/pkg/tasks/heatmap.go b/pkg/tasks/heatmap.go index 4efd0d4d1..eb68610ad 100644 --- a/pkg/tasks/heatmap.go +++ b/pkg/tasks/heatmap.go @@ -81,6 +81,7 @@ func GenerateHeatmaps(tlog *logrus.Entry) { ) if err == nil { file.HasHeatmap = true + file.RefreshHeatmapCache = true file.Save() } else { log.Warn(err)