Skip to content

Commit

Permalink
More modular code.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Jun 1, 2023
1 parent 2b8ca49 commit 0212613
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/libs/medialib.liq
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
def medialib(~persistency=null(), directories=[])
def medialib(~id=null(), ~persistency=null(), directories=[])
id = string.id.default(default="medialib", id)

db = ref([])

# Read metadata from file
def metadata(f)
m = file.metadata(f)
m = metadata.cover.remove(m)
m = ("filename",f) :: m
m = ("scan-time",string.float(time())) :: m
m
end

# Add a file to the database.
def add(f)
# If file doesn't exist remove it
Expand All @@ -9,11 +20,7 @@ def medialib(~persistency=null(), directories=[])
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
m = ("scan-time",string.float(time())) :: m
db := (f,m) :: list.assoc.remove(f, db())
db := (f,metadata(f)) :: list.assoc.remove(f, db())
end
end
end
Expand All @@ -39,9 +46,14 @@ def medialib(~persistency=null(), directories=[])
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)
try
let json.parse ( parsed : [(string * [(string * string)])]? ) = file.contents(f)
if null.defined(parsed) then
db := null.get(parsed)
end
catch e do
log.important(label=id,"Failed to parse persistent file #{f}")
log.important(label=id,"#{e.kind}: #{e.message}")
end
end
end
Expand All @@ -50,8 +62,10 @@ def medialib(~persistency=null(), directories=[])
# Store the file in a persistent file.
def store()
if null.defined(persistency) then
f = null.get(persistency)
data = json.stringify(db())
file.write(data=data, null.get(persistency))
file.write(data=data, f)
log.info(label=id,"Wrote persistent file #{f}")
end
end

Expand Down

0 comments on commit 0212613

Please sign in to comment.