Skip to content

Commit

Permalink
Merge pull request #29 from BTx123/feature/kyoto
Browse files Browse the repository at this point in the history
Feature/kyoto
  • Loading branch information
BTx123 authored Nov 29, 2024
2 parents 92833d2 + 13bc380 commit 8ce6406
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 27 deletions.
45 changes: 45 additions & 0 deletions content/galleries/20241114-kyoto/gallery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"generatedOn": "2024-11-29T02:32:16.071608-08:00",
"images": [
"images/20241114-kyoto/DSC04050.jpg",
"images/20241114-kyoto/DSC04058.jpg",
"images/20241114-kyoto/DSC04060.jpg",
"images/20241114-kyoto/DSC04068.jpg",
"images/20241114-kyoto/DSC04087.jpg",
"images/20241114-kyoto/DSC04132.jpg",
"images/20241114-kyoto/DSC04201.jpg",
"images/20241114-kyoto/DSC04243.jpg",
"images/20241114-kyoto/DSC04257.jpg",
"images/20241114-kyoto/DSC04276.jpg",
"images/20241114-kyoto/DSC04281.jpg",
"images/20241114-kyoto/DSC04285.jpg",
"images/20241114-kyoto/DSC04317.jpg",
"images/20241114-kyoto/DSC04327.jpg",
"images/20241114-kyoto/DSC04347.jpg",
"images/20241114-kyoto/DSC04399.jpg",
"images/20241114-kyoto/DSC04413.jpg",
"images/20241114-kyoto/DSC04420.jpg",
"images/20241114-kyoto/DSC04429.jpg",
"images/20241114-kyoto/DSC04439.jpg",
"images/20241114-kyoto/DSC04440.jpg",
"images/20241114-kyoto/DSC04451.jpg",
"images/20241114-kyoto/DSC04456.jpg",
"images/20241114-kyoto/DSC04477.jpg",
"images/20241114-kyoto/DSC04509.jpg",
"images/20241114-kyoto/DSC04527.jpg",
"images/20241114-kyoto/DSC04538.jpg",
"images/20241114-kyoto/DSC04591.jpg",
"images/20241114-kyoto/DSC04597.jpg",
"images/20241114-kyoto/DSC04643.jpg",
"images/20241114-kyoto/DSC04654.jpg",
"images/20241114-kyoto/DSC04655.jpg",
"images/20241114-kyoto/DSC04695.jpg",
"images/20241114-kyoto/DSC04716.jpg",
"images/20241114-kyoto/DSC04772.jpg",
"images/20241114-kyoto/DSC04794.jpg",
"images/20241114-kyoto/DSC04797.jpg",
"images/20241114-kyoto/DSC04841.jpg",
"images/20241114-kyoto/DSC04894.jpg",
"images/20241114-kyoto/DSC04999.jpg"
]
}
13 changes: 13 additions & 0 deletions content/galleries/20241114-kyoto/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
date: 2024-11-14
lastmod: 2024-11-29
draft: false
title: Kyoto 2024
type: gallery
tags:
- vacation
---

Photos from my Kyoto trip.

{{< gallery gallery.json >}}
64 changes: 47 additions & 17 deletions layouts/shortcodes/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,74 @@
{{ $thumbnail := .Filter (slice images.AutoOrient (images.Process "fit 600x600")) | resources.Copy $thumbnailPath }}

{{/* Generate caption using EXIF data */}}
{{ $dataTitleParts := slice }}
{{ $title := "" }}
{{ $captureMetadata := slice }}
{{ $modelLensMetadata := slice }}
{{ with .Exif }}
<pre>{{ debug.Dump .Tags }}</pre>
{{ if .Tags.ImageDescription }}
{{ $dataTitleParts = $dataTitleParts | append .Tags.ImageDescription }}
{{ $title = .Tags.ImageDescription }}
{{ end }}
{{ $captureMetadata := slice }}
{{ if .Tags.ExposureTime }}
{{ $captureMetadata = $captureMetadata | append (printf "%ss" .Tags.ExposureTime) }}
{{ $exposureTime := .Tags.ExposureTime }}
{{ if strings.Contains $exposureTime "/" }}
{{ $exposureTimeParts := split $exposureTime "/" }}
{{ if lt ($exposureTimeParts | len) 2 }}
{{ errorf "Failed to convert ExposureTime %q from fraction to float" $exposureTime }}
{{ end }}
{{ $numerator := float (collections.Index $exposureTimeParts 0) }}
{{ $denominator := float (collections.Index $exposureTimeParts 1) }}
{{ if gt $numerator $denominator }}
{{ $exposureTime = string (math.Div $numerator $denominator) }}
{{ end }}
{{ end }}
{{ $captureMetadata = $captureMetadata | append (printf "%ss" $exposureTime) }}
{{ end }}
{{ if .Tags.FNumber }}
{{ $captureMetadata = $captureMetadata | append (printf "f/%s" .Tags.FNumber) }}
{{ $fNumber := .Tags.FNumber }}
{{/* Compute floating point FNumber in case string representation is fractional */}}
{{ if strings.Contains $fNumber "/" }}
{{ $fNumberParts := split $fNumber "/" }}
{{ if lt ($fNumberParts | len) 2 }}
{{ errorf "Failed to convert FNumber %q from fraction to float" $fNumber }}
{{ end }}
{{ $numerator := float (collections.Index $fNumberParts 0) }}
{{ $denominator := float (collections.Index $fNumberParts 1) }}
{{ $fNumber = string (math.Div $numerator $denominator) }}
{{ end }}
{{ $captureMetadata = $captureMetadata | append (printf "f/%s" $fNumber) }}
{{ end }}
{{ if .Tags.ISO }}
{{ $captureMetadata = $captureMetadata | append (printf "ISO %d" .Tags.ISO) }}
{{ end }}
{{ if .Tags.FocalLength }}
{{ $captureMetadata = $captureMetadata | append (printf "%smm" .Tags.FocalLength) }}
{{ end }}
{{ if $captureMetadata }}
{{ $dataTitleParts = $dataTitleParts | append (delimit $captureMetadata " &#183; ") }}
{{ end }}
{{ $cameraMetadata := slice }}
{{ if (and (and .Tags.Make .Tags.Model) .Tags.LensModel) }}
{{ $cameraMetadata = $cameraMetadata | append (delimit (slice .Tags.Make .Tags.Model) " ") }}
{{ $cameraMetadata = $cameraMetadata | append .Tags.LensModel }}
{{ end }}
{{ if $cameraMetadata }}
{{ $dataTitleParts = $dataTitleParts | append (delimit $cameraMetadata " &#183; ") }}
{{ $modelLensMetadata = $modelLensMetadata | append (delimit (slice .Tags.Make .Tags.Model) " ") }}
{{ $modelLensMetadata = $modelLensMetadata | append .Tags.LensModel }}
{{ end }}
{{ end }}
{{ $dataTitle := (delimit $dataTitleParts "<br>") }}

{{ $color := index $thumbnail.Colors 0 | default "transparent" }}
<a class="gallery-item" href="{{ $url }}" data-pswp-src="{{ $url }}" data-pswp-width="{{ .Width }}" data-pswp-height="{{ .Height }}" title="{{ $dataTitle }}" itemscope itemtype="https://schema.org/ImageObject" style="aspect-ratio: {{ $thumbnail.Width }} / {{ $thumbnail.Height }}">
<a class="gallery-item" href="{{ $url }}" data-pswp-src="{{ $url }}" data-pswp-width="{{ .Width }}" data-pswp-height="{{ .Height }}" title="{{ $title }}" itemscope itemtype="https://schema.org/ImageObject" style="aspect-ratio: {{ $thumbnail.Width }} / {{ $thumbnail.Height }}">
<figure style="background-color: {{ $color }}; aspect-ratio: {{ $thumbnail.Width }} / {{ $thumbnail.Height }}">
<img class="lazyload" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" data-src="{{ $thumbnail.RelPermalink }}" alt="{{ $dataTitle }}" />
<img class="lazyload" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" data-src="{{ $thumbnail.RelPermalink }}" alt="{{ $title }}" />
<span class="pswp-caption-content">
{{ if not (eq $title "") }}
<div style="font-weight: bold;">{{ $title }}</div>
{{ end }}
{{ if gt ($captureMetadata | len) 0 }}
<div style="opacity: 0.33">
{{ delimit $captureMetadata " · " }}
</div>
{{ end }}
{{ if gt ($modelLensMetadata | len) 0 }}
<div style="opacity: 0.33">
{{ delimit $modelLensMetadata " · " }}
</div>
{{ end }}
</span>
</figure>
<meta itemprop="contentUrl" content="{{ $url }}" />
</a>
Expand Down
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -34,14 +34,17 @@ func goDotEnvVariable(key string) string {
err := godotenv.Load(".env")

if err != nil {
log.Fatalf("Error loading .env file")
slog.Error("Error loading .env file", "key", key, "error", err)
panic(err)
}

return os.Getenv(key)
}

func main() {

slog.SetLogLoggerLevel(slog.LevelInfo)

// Variables
galleriesDirectory := filepath.Join("content", "galleries")
accountId := goDotEnvVariable("ACCOUNT_ID")
Expand All @@ -54,6 +57,7 @@ func main() {
config.WithRegion("auto"),
)
if err != nil {
slog.Error("config.LoadDefaultConfig", "error", err)
panic(err)
}

Expand All @@ -65,6 +69,7 @@ func main() {
Bucket: &r2Bucket,
})
if err != nil {
slog.Error("s3.client.ListObjectsV2", "error", err)
panic(err)
}

Expand All @@ -74,15 +79,15 @@ func main() {
for _, object := range listObjectsOutput.Contents {
// Get key for R2 object
key := *object.Key
// fmt.Println("Key:", key)
slog.Debug("Checking", "key", key)

matches := re.FindStringSubmatch(key)
if matches != nil {
slug := matches[0]
gallery := matches[1]
imageName := matches[2]
imageType := matches[3]
// fmt.Println(slug, gallery, imageName, imageType)
slog.Debug("Match found", "slug", slug, "gallery", gallery, "imageName", imageName, "imageType", imageType)

if data[gallery] == nil {
images := make([]GalleryImage, 0)
Expand All @@ -91,40 +96,43 @@ func main() {
value := GalleryImage{slug, gallery, imageName, imageType}
data[gallery] = append(data[gallery], value)
} else {
fmt.Printf("Ignoring key: %s\n", key)
slog.Info("Ignoring", "key", key)
}
}

// fmt.Println(data)
slog.Debug("Got", "data", data)

// Get all gallery folders
entries, err := os.ReadDir(galleriesDirectory)
if err != nil {
slog.Error("os.ReadDir", "error", err)
panic(err)
}

for _, entry := range entries {
info, err := entry.Info()
if err != nil {
slog.Error("fs.Info", "error", err)
panic(err)
}

// Make sure entry is a directory
if !info.IsDir() {
slog.Debug("Not a directory", "id", info.Name())
continue
}

// Make sure data contains directory name (gallery ID)
if data[info.Name()] == nil {
fmt.Printf("Gallery ID %s not found in image data, skipping...\n", info.Name())
slog.Info("Gallery ID not found in image data, skipping...", "id", info.Name())
continue
}

// Get images for gallery
galleryPath := filepath.Join(galleriesDirectory, info.Name())
galleryJsonPath := filepath.Join(galleryPath, "gallery.json")
galleryImages := data[entry.Name()]
fmt.Printf("%d image(s) found for gallery: %s\n", len(galleryImages), entry.Name())
slog.Info("Image(s) found for gallery", "imageCount", len(galleryImages), "galleryName", entry.Name())

// Get slugs for images
slugs := make([]string, 0)
Expand All @@ -136,24 +144,27 @@ func main() {
// Serialize to string
jsonString, err := json.MarshalIndent(galleryData, "", " ")
if err != nil {
slog.Error("json.MarshalIndent", "error", err)
panic(err)
}

// fmt.Println(jsonString)
slog.Debug("Serialized", "json", jsonString)

// Open the file for writing
file, err := os.Create(galleryJsonPath)
if err != nil {
slog.Error("os.Create", "error", err)
panic(err)
}
defer file.Close()

// Write the string to the file
_, err = file.WriteString(string(jsonString))
if err != nil {
slog.Error("os.File.WriteString", "error", err)
panic(err)
}

fmt.Printf("Generated gallery data for %s\n", info.Name())
slog.Info("Generated gallery data", "galleryName", info.Name())
}
}

0 comments on commit 8ce6406

Please sign in to comment.