commit 646f3626047d083bd20380b9a4469c44e7975bcd
parent 48dfabc4e8058e27db764f17f63b47a995eb1bcb
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 28 Feb 2021 15:02:05 +0100
parent 48dfabc4e8058e27db764f17f63b47a995eb1bcb
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 28 Feb 2021 15:02:05 +0100
gallery.nim: improve config-file handling, cleanup non-existing files in target dir
2 files changed, 31 insertions(+), 22 deletions(-)
diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ gallery out/* +foobar/*
diff --git a/src/gallery.nim b/src/gallery.nim @@ -98,7 +98,10 @@ proc placeAssets(targetDir: string, enableJS: bool) = if enableJS: writeFile(joinPath(targetDir, "exif.js"), asset_exif_js) -proc removeOrphans (sourceDir: string, targetDir: string) = +proc removeOrphans (targetDir: string, config: JsonNode) = + echo "Checking for orphaned files and folders..." + let sourceDir = joinPath(config["SourceDir"].getStr, targetDir.replace(config["TargetDir"].getStr, "")) + #Albums for album in walkDir(targetDir): if album.kind != pcDir: continue @@ -111,23 +114,23 @@ proc removeOrphans (sourceDir: string, targetDir: string) = if dirname == "thumbnails": continue if dirname == "medium": continue - #echo dirname - #if not dirExists(joinPath(sourceDir, dirname)): - # removeDir(joinPath(targetDir, dirname)) + if not dirExists(joinPath(sourceDir, dirname)): + echo "Removing orphaned folder(" & joinPath(sourceDir, dirname) & ") from gallery!" + removeDir(joinPath(targetDir, dirname)) #Photos for photo in walkDir(joinPath(targetDir, "medium")): let (dir, name, ext) = splitFile(photo.path) let filename = name & ext - echo joinPath(sourceDir, filename) - #if not fileExists(joinPath(sourceDir, filename)): - # removeFile(joinPath(targetDir, name & ".html")) - # removeFile(joinPath(targetDir, "medium", filename)) - # removeFile(joinPath(targetDir, "thumbnails", name & ".png")) + if not fileExists(joinPath(sourceDir, filename)): + echo "Removing orphaned file(" & joinPath(sourceDir, filename) & ") from gallery!" + removeFile(joinPath(targetDir, name & ".html")) + removeFile(joinPath(targetDir, "medium", filename)) + removeFile(joinPath(targetDir, "thumbnails", name & ".png")) -proc generateWebsite(sourceDir: string, targetDir: string, album: Album, config: JsonNode) = +proc generateWebsite(targetDir: string, album: Album, config: JsonNode) = echo "============" echo "Create Album:" & album.name discard existsOrCreateDir(targetDir) @@ -153,8 +156,7 @@ proc generateWebsite(sourceDir: string, targetDir: string, album: Album, config: if album.subalbums.len > 0: templateContext["hasSubalbums"] = %true for subalbum in album.subalbums: - generateWebsite(sourceDir, joinPath(targetDir, subalbum.name), subalbum, config) -# removeOrphans(joinPath(sourceDir, subalbum.name), joinPath(targetDir, subalbum.name)) + generateWebsite(joinPath(targetDir, subalbum.name), subalbum, config) var thumbnail1 = "/no_images.svg" var thumbnail2 = "/no_images.svg" @@ -177,7 +179,7 @@ proc generateWebsite(sourceDir: string, targetDir: string, album: Album, config: for index, picture in album.pictures: var pictureTemplateContext = mergeJson(%* { "name": picture.name, - "orig": joinPath("/originals", picture.path.replace(sourceDir, ""), picture.filename), + "orig": joinPath("/originals", picture.path.replace(config["SourceDir"].getStr, ""), picture.filename), "filename": picture.filename, "description": "-", "hasPrev": false, @@ -215,21 +217,25 @@ proc generateWebsite(sourceDir: string, targetDir: string, album: Album, config: echo "Generate medium thumbnails!" discard execProcesses(mediumThumbnails) - echo "Generate album page!\n" + echo "Generate album page!" writeFile(joinPath(targetDir, "index.html"), render(asset_album_html, templateContext)) -# removeOrphans(sourceDir, targetDir) - + removeOrphans(targetDir, config) + echo "\n" proc main = randomize() if (execProcess("/usr/bin/env mogrify -v").contains("No such file or directory")): echo "It seems like you don't have ImageMagick installed, which is mandatory to use this tool.\nBye!" - quit() + quit(QuitFailure) + + if paramCount() == 0: + echo "No config-file given! Exiting..." + quit(QuitFailure) if not fileExists(paramStr(1)): - echo "The given config-file doesn't exist. Do u wanna write a default one to it? [y/N]" + stdout.write "The given config-file doesn't exist. Do u wanna write a default one to it? [y/N] " if readLine(stdin) == "y": var config = newConfig() @@ -254,14 +260,16 @@ proc main = echo "Have written a default config to this file: " & paramStr(1) echo "Please check it and rerun this program." quit() + else: + quit() var config = %* {} try: - var configFile = loadConfig(paramStr(1)) + let configFile = loadConfig(paramStr(1)) config = %* { - "SourceDir": configFile.getSectionValue("", "SourceDir"), - "TargetDir": configFile.getSectionValue("", "TargetDir"), + "SourceDir": normalizedPath(configFile.getSectionValue("", "SourceDir")), + "TargetDir": normalizedPath(configFile.getSectionValue("", "TargetDir")), "SiteName": configFile.getSectionValue("Site", "Name"), "SiteAuthor": configFile.getSectionValue("Site", "Author"), "SiteDescription": configFile.getSectionValue("Site", "Description"), @@ -304,7 +312,7 @@ proc main = createSymlink(config["SourceDir"].getStr, joinPath(config["TargetDir"].getStr, "originals")) placeAssets(config["TargetDir"].getStr, config["SiteEnableJS"].getBool) - generateWebsite(config["SourceDir"].getStr, config["TargetDir"].getStr, mainAlbum, config) + generateWebsite(config["TargetDir"].getStr, mainAlbum, config) main()