Remove old info cache

This commit is contained in:
Zoe Roux 2024-07-29 23:24:05 +02:00
parent e91e0151ba
commit 3ea2004267
2 changed files with 2 additions and 66 deletions

View File

@ -59,7 +59,7 @@ create table subtitles(
path varchar(4096)
);
create type chapter_type as enum('content', 'recap', 'intro', 'credits');
create type chapter_type as enum('content', 'recap', 'intro', 'credits', 'preview');
create table chapters(
sha varchar(20) not null references info(sha) on delete cascade,

View File

@ -4,16 +4,11 @@ import (
"cmp"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"mime"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/text/language"
@ -140,6 +135,7 @@ const (
Recap ChapterType = "recap"
Intro ChapterType = "intro"
Credits ChapterType = "credits"
Preview ChapterType = "preview"
)
func ParseFloat(str string) float32 {
@ -215,66 +211,6 @@ var SubtitleExtensions = map[string]string{
"vtt": "vtt",
}
type MICache struct {
info *MediaInfo
ready sync.WaitGroup
}
var infos = NewCMap[string, *MICache]()
func GetInfo(path string, sha string) (*MediaInfo, error) {
var err error
ret, _ := infos.GetOrCreate(sha, func() *MICache {
mi := &MICache{info: &MediaInfo{Sha: sha}}
mi.ready.Add(1)
go func() {
save_path := fmt.Sprintf("%s/%s/info.json", Settings.Metadata, sha)
if err := getSavedInfo(save_path, mi.info); err == nil {
log.Printf("Using mediainfo cache on filesystem for %s", path)
mi.ready.Done()
return
}
var val *MediaInfo
val, err = RetriveMediaInfo(path, sha)
if err == nil {
*mi.info = *val
mi.info.Sha = sha
}
mi.ready.Done()
saveInfo(save_path, mi.info)
}()
return mi
})
ret.ready.Wait()
return ret.info, err
}
func getSavedInfo[T any](save_path string, mi *T) error {
saved_file, err := os.Open(save_path)
if err != nil {
return err
}
saved, err := io.ReadAll(saved_file)
if err != nil {
return err
}
err = json.Unmarshal([]byte(saved), mi)
if err != nil {
return err
}
return nil
}
func saveInfo[T any](save_path string, mi *T) error {
content, err := json.Marshal(*mi)
if err != nil {
return err
}
return os.WriteFile(save_path, content, 0o644)
}
func RetriveMediaInfo(path string, sha string) (*MediaInfo, error) {
defer printExecTime("mediainfo for %s", path)()