Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-photo

Source of release 0.1.0

0.1.0 / src / library.lucb

## The library bundle on disk:
##
##     <root>/library.prism            the catalog (catalog.lucb)
##     <root>/originals/YYYY/MM/DD/    imported files, by capture date
##     <root>/cache/thumbs/<id>.jpg    512 px thumbnails for the grid
##     <root>/cache/previews/<id>.jpg  2048 px previews (the camera's embedded one)
##     <root>/cache/developed/<id>.jpg 3072 px, the photo developed with its settings
##     <root>/cache/developed/<id>.settings  the settings it was developed with
##
## The Library mode shows the developed picture when its settings are the
## photo's, without developing again (rerender.lucb makes it after edits, or
## when a photo is shown without one).
## Everything under cache/ can be made again from the originals.
import c
import strings
from luce_std import files
from luce_std import paths
import interop
from luce_std.collections import Buffer

## The longer side of a grid thumbnail and of a viewer preview, in pixels.
pub let thumb_edge: i64 = 512
pub let preview_edge: i64 = 2048
pub let developed_edge: i64 = 3072

## `root`/`relative`, onto `out`.
pub func join_into(root: str, relative: str, out: Buffer*) -> !:
    out.clear()
    try out.put_text(root)
    if root.length > 0 and root.bytes[root.length - 1] != '/':
        try out.put_byte('/')
    try out.put_text(relative)

## The thumbnail (`kind` "thumbs"), preview ("previews") or developed picture
## ("developed") file of photo `id`, onto `out`.
pub func cache_path(root: str, kind: str, id: i64, out: Buffer*) -> !:
    try join_into(root, "cache/", out)
    try out.put_text(kind)
    try out.put_byte('/')
    try out.put_number(id)
    try out.put(b".jpg")

## Make the bundle's folders when they are missing.
pub func ensure(root: str) -> !:
    var path: Buffer
    defer path.destroy()
    for relative in ["originals", "cache/thumbs", "cache/previews", "cache/developed"]:
        try join_into(root, relative, &path)
        try files.ensure_directory(path.text())

## The settings photo `id`'s developed picture was made with, onto `out`.
pub func stamp_path(root: str, id: i64, out: Buffer*) -> !:
    try join_into(root, "cache/developed/", out)
    try out.put_number(id)
    try out.put(b".settings")

## The same, for Luce.
pub func stamp(root: str, id: i64) -> interop.Owned[str]!:
    var path: Buffer
    defer path.destroy()
    try stamp_path(root, id, &path)
    return try interop.copy_text(path.text())

## The catalog file of the bundle at `root`, for Luce.
pub func catalog_path(root: str) -> interop.Owned[str]!:
    return try paths.join(root, "library.prism")

## Photo `id`'s thumbnail (`kind` "thumbs") or preview ("previews") file, for Luce.
pub func cached(root: str, kind: str, id: i64) -> interop.Owned[str]!:
    var path: Buffer
    defer path.destroy()
    try cache_path(root, kind, id, &path)
    return try interop.copy_text(path.text())

## Whether a file exists at `path`.
pub func exists(path: str) -> bool:
    let terminated = strings.copy(path) catch failure:
        return false
    defer strings.release(terminated)
    return files.exists((c.str)terminated)