ctucx.git: gallery

static-site-generator for image-galleries [used @ photos.ctu.cx]

commit 72583e2641ea723445874a7986e6ba0adfb8fe62
parent b6f29916d2b15c5af6460ae5327a4bc83c79fbfb
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sat, 13 Mar 2021 19:20:30 +0100

gallery.nim fix crash when no exif-data or no exif-takestamp
3 files changed, 26 insertions(+), 9 deletions(-)
M
src/assets/album.html
|
2
++
M
src/assets/picture.html
|
2
+-
M
src/gallery.nim
|
31
+++++++++++++++++++++++--------
diff --git a/src/assets/album.html b/src/assets/album.html
@@ -120,7 +120,9 @@
 					<img src="thumbnails/small/{{name}}.jpg" alt="Photo thumbnail">
 					<span class="overlay">
 						<h1>{{name}}</h1>
+						{{#takedate}}
 						<p><span title="Camera Date"><svg class="iconic "><use xlink:href="/iconic.svg#camera-slr"></use></svg>{{takedate}}</span></p>
+						{{/takedate}}
 					</span>
 				</a>
 				{{/pictures}}
diff --git a/src/assets/picture.html b/src/assets/picture.html
@@ -100,7 +100,7 @@
 							<tbody>
 								<tr>
 									<td>Captured</td>
-									<td>{{takestamp}}</td>
+									<td>{{takedate}}{{#taketime}} {{taketime}}{{/taketime}}</td>
 								</tr>
 
 								<tr>
diff --git a/src/gallery.nim b/src/gallery.nim
@@ -94,7 +94,9 @@ proc createPicture(path: string): Picture =
     let file        = open(path)
     let jpgMetadata = collect_jpg(file)
 
-    result.exif     = jpgMetadata.exifData.get
+    if jpgMetadata.exifData.isSome():
+        result.exif     = jpgMetadata.exifData.get
+
     result.width    = int(jpgMetadata.sofData.get.width)
     result.height   = int(jpgMetadata.sofData.get.height)
 

@@ -232,7 +234,12 @@ proc generateWebsite(targetDir: string, album: Album) =
         })
 
     for index, picture in album.pictures:
-        let takestamp              = picture.exif.getOrDefault("DateTimeOriginal", "").split(" ")
+        let takestamp  = picture.exif.getOrDefault("DateTimeOriginal", "").split(" ")
+        var takedate   = "-"
+
+        if takestamp.len > 0:
+            takedate = takestamp[0].replace(":", "-")
+
         var pictureTemplateContext = mergeJson(%* {
             "name":             picture.name,
             "orig":             joinPath("/originals", picture.path.replace(config.sourceDir, ""), picture.filename),

@@ -241,7 +248,6 @@ proc generateWebsite(targetDir: string, album: Album) =
             "height":           picture.thumbHeight,
             "thumbWidth":       picture.thumbWidth,
             "thumbHeight":      picture.height,
-            "takestamp":        takestamp[0].replace(":", "-") & " " & takestamp[1],
             "make":             picture.exif.getOrDefault("Make", "-"),
             "model":            picture.exif.getOrDefault("Model", "-"),
             "lensModel":        picture.exif.getOrDefault("LensModel", "-"),

@@ -257,10 +263,16 @@ proc generateWebsite(targetDir: string, album: Album) =
             "filesize":         isaRound((picture.filesize.int/1000/1000), 2)
         }, %config)
 
+        if not picture.desc.isNone: pictureTemplateContext["description"] = %picture.desc.get
+
         if picture.exif.hasKey("FocalLengthIn35mmFilm"):
             pictureTemplateContext["focalLength35mm"] = %(picture.exif["FocalLengthIn35mmFilm"])
 
-        if not picture.desc.isNone: pictureTemplateContext["description"] = %picture.desc.get
+        if takestamp.len > 0:
+            pictureTemplateContext["takedate"] = %takedate
+
+        if takestamp.len > 1:
+            pictureTemplateContext["taketime"] = %takestamp[1]
 
         if index > 0:
             pictureTemplateContext["hasPrev"]   = %true

@@ -294,13 +306,16 @@ proc generateWebsite(targetDir: string, album: Album) =
                 quoteShell(joinPath(picture.path, picture.filename))
             ].join(" "))
 
-
-        templateContext["pictures"].add(%* {
+        var picAlbumTemplateContext = %* {
             "name":       picture.name,
             "width":      picture.thumbWidth,
             "height":     picture.thumbHeight,
-            "takedate":   takestamp[0].replace(":", "-")
-        })
+        }
+
+        if takestamp.len > 0:
+            picAlbumTemplateContext["takedate"] = %takedate
+
+        templateContext["pictures"].add(picAlbumTemplateContext)
 
     echo "Generate small thumbnails!"
     discard execProcesses(smallThumbnails)