Skip to content

Commit

Permalink
Peristency and mtime support.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed May 31, 2023
1 parent 9fcc81b commit 76e16cd
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions src/libs/medialib.liq
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
def medialib(directories=[])
def medialib(~persistency=null(), directories=[])
db = ref([])

def scan()
for d = list.iterator(directories) do
for f = list.iterator(file.ls(absolute=true, recursive=true, d)) do
log.debug("adding file: #{f}")
# Add a file to the database.
def add(f)
# If file doesn't exist remove it
if not (file.exists(f)) then
db := list.assoc.remove(f, db())
else
# Not recent enough metadata
if not list.assoc.mem(f, db()) or (string.to_float(list.assoc("scan-time",list.assoc(f, db()))) > file.mtime(f)) then
m = file.metadata(f)
m = metadata.cover.remove(m)
m = ("filename",f) :: m
db := (f,m) :: db()
m = ("scan-time",string.float(time())) :: m
db := (f,m) :: list.assoc.remove(f, db())
end
end
end

# Update database by renewing metadata and removing removed files.
def update()
for fm = list.iterator(db()) do
let (f,_) = fm
add(f)
end
end

# Make sure that new files from directories are registered.
def scan()
for d = list.iterator(directories) do
list.iter(add, file.ls(absolute=true, recursive=true, d))
end
end

# Load from the persistent file.
def load()
db := []
if null.defined(persistency) then
f = null.get(persistency)
if file.exists(f) then
let json.parse ( parsed : [(string * [(string * string)])]? ) = file.contents(f)
if null.defined(parsed) then
db := null.get(parsed)
end
end
end
end

# Store the file in a persistent file.
def store()
if null.defined(persistency) then
data = json.stringify(db())
file.write(data=data, null.get(persistency))
end
end

def find(~artist=null(), ~artist_contains=null())
def p(m)
(null.defined(artist) ? m["artist"] == null.get(artist) : true)
Expand All @@ -23,6 +66,9 @@ def medialib(directories=[])
l
end

load()
scan()
store()

{find = find}
end

0 comments on commit 76e16cd

Please sign in to comment.