aboutsummaryrefslogtreecommitdiff
path: root/internal/thumbnail/cache.go
blob: 740268a015338c2eb8105351e2a0a7caf2965186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package thumbnail

import (
	"fmt"
	"path/filepath"

	"github.com/RanXom/galleryd/internal/gallery"
)

const (
	thumbnailCacheVersion = 1
	thumbnailExtension    = ".webp"
)

func cacheVersion() string {
	return fmt.Sprintf("v%d", thumbnailCacheVersion)
}

// cachePath returns the on-disk cache location for a photo thumbnail.
//
// Thumbnails are sharded by the first two characters of the photo ID to
// avoid placing large numbers of files in a single directory.
func (g *Generator) cachePath(photo gallery.Photo) string {
	shard := photo.ID[:2]

	filename := photo.ID + thumbnailExtension

	return filepath.Join(
		g.cacheDir,
		"thumbs",
		cacheVersion(),
		shard,
		filename,
	)
}