PiGallery2 Documentation

repository·master·Indexed 24 days ago

https://github.com/bpatrik/pigallery2

A high-performance, directory-first, read-only photo gallery optimized for low-resource servers such as Raspberry Pi. It mirrors existing file system structures and supports Docker deployment, MariaDB integration, and custom extensions via the pigallery2-extension-kit. Features include Markdown-based blog posts with date tagging and typographic replacements.

Tokens
31.8K
Snippets
53
Records
151
Agent score
79%

What's inside PiGallery2

  1. Overview of PiGallery2 features

    master

    PiGallery2 is a fast, directory-first photo gallery website designed to run on low-resource servers.

    Key characteristics:

    • Fast: Optimized for low-end hardware (e.g., Raspberry Pi).
    • Simple: Configuration is minimal; point the application to your photo directory to begin.
    • Directory-first: The UI reflects your existing folder structure.
    • Read-only: The application does not modify your original photo files.
  2. PiGallery2 performance benchmark results

    master

    The benchmark/README.md file contains historical performance data for PiGallery2. These benchmarks provide insight into how different operations (scanning, listing, searching, and face listing) perform on specific hardware configurations.

    Note that these are historical records and may not reflect the performance of the current version or your specific hardware setup.

  3. Understand the PiGallery2 data model and source of truth

    master

    PiGallery2 is designed with a directory-first approach where the disk is the sole source of truth.

    Key architectural constraints to keep in mind:

    • The Database is a Cache: The database is used to store scanned file structures, metadata, and transcoded thumbnail information to ensure high performance. It is not a primary data store for user configurations like directory covers, hidden photos, or keywords.
    • No DB-only Edits: Because the database is treated as a cache that can be deleted and rebuilt at any time, any changes made exclusively in the database (and not reflected on the disk) will be lost if the database is cleared or rebuilt.
    • No Editing Support: The application does not support photo editing. It is optimized for a frequent read path rather than a write path.
    • Cache Invalidation: The app uses multiple hooks to check if the client or server-side (DB) cache is still valid. Cache invalidation occurs when changes are detected in a directory (e.g., uploading a new photo).
  4. Understand Pigallery2 user roles and rights

    master

    Pigallery2 supports several user roles, each granting different levels of access to the application's features. Access is determined by the user's role ID or role name.

    idRolerights
    1Limited Guestlisting directory
    2Guest+ search
    3User+ share, list faces, create random link
    4Admin+ settings, see duplicate photos
    5Developer+ see errors

    Key details:

    • Limited Guest (ID 1): Accessed via shared links; can only list directories.
    • Guest (ID 2): Can search; no default assignment (must be set manually in the database).
    • User (ID 3): The default role for new users; includes sharing and face listing.
    • Admin (ID 4): The default role for the pregenerated user and the fallback role if authentication is disabled; includes settings and duplicate photo detection.
    • Developer (ID 5): Can view errors; no default assignment (must be set manually in the database).
  5. Navigate the backend architecture

    master

    The backend is located in src/backend/ and follows this organization:

    • middlewares/: Input validation and transformation.
    • model/: Business logic, further divided into:
      • database/: Data retrieval (includes sql/ for SQL databases and memory/ for memory-based logic).
      • interfaces/: Database-independent interfaces.
    • diagnostics/: Startup checks (settings validation, package availability, etc.).
    • fileprocessing/: Photo and video conversion logic.
    • jobs/: Job scheduling (crontab-like features).
    • threading/: Code running on separate threads (e.g., DiskManagerWorker.ts for directory scanning, Metadataloader.ts for metadata parsing).
    • routers/: HTTP API endpoint declarations.
    • index.ts & server.ts: Server startup and HTTP server configuration.
    • ProjectPath.ts: A singleton containing project-related paths.
  6. Customize directory behavior with .pg2conf files

    master

    You can place specific .pg2conf files within your gallery directories to customize how PiGallery2 handles that specific directory. These files control sorting, upload permissions, and saved searches.

    Note that some files are processed on the Client side (affecting how the UI displays the directory), while others are processed on the Server side (affecting how the directory is indexed or managed).

  7. Understand the project and source structure

    master

    The repository is organized into several key directories:

    Root Structure

    • src/: The primary source code directory.
    • test/: Unit and integration tests.
    • docker/: Docker configurations (recommended for deployment).
    • docs/: Project documentation.

    Source Structure (src/)

    • backend/: Node.js backend code.
    • frontend/: Angular-based frontend.
    • common/: Shared files (primarily DTOs) used by both frontend and backend.
  8. Understanding the PiGallery2 data model: Disk vs Database

    master

    PiGallery2 operates on a philosophy where the disk is the source of truth and the database is only a cache. The application is designed to reflect your photo organization exactly as it exists on your storage.

    Because the database is a cache, it can be deleted and rebuilt at any time without losing your photo organization. This design choice ensures portability and ease of rebuilding, but it means that traditional database-only albums are replaced by Logical Albums (saved search queries) to maintain the directory-first approach.

  9. Use date tagging in Markdown files

    master

    To associate specific sections of an index.md file with a specific date, use the @pg-date HTML comment tag.

    When you use the 'group by date' feature in the gallery, the content tagged with a specific date will be moved from the main markdown view and displayed only under that assigned day.

    Format: <!-- @pg-date <ISO_DATE> -->

    <!-- @pg-date 2015-06-29 -->
  10. How PiGallery2 extensions work

    master

    PiGallery2 extensions are subdirectories within a configured extension folder. For Docker installations, this is typically config/extension/.

    When the app starts or a configuration change occurs, it performs the following lifecycle:

    1. Scans the extension folder for subdirectories.
    2. If a package.js is present, it runs npm install for the dependencies listed there.
    3. If a server.js is present, it calls the exported init(ext) function.

    Extensions can implement an init function for setup and a cleanUp function to undo changes. The app calls init first, then cleanUp if it needs to re-initialize (e.g., during a config change).

  11. How PiGallery2 optimizes photo loading and performance

    master

    PiGallery2 uses several layers of optimization to provide a fast user experience, especially when browsing large directories or using metered networks:

    Photo Transcoding

    To avoid downloading massive original files (e.g., 5-10MB JPGs), the app uses:

    • Thumbnails: The app generates multiple sizes of thumbnails (supporting formats like jpg and webm). When rendering, it selects the best size to minimize unnecessary data transfer.
    • Lightbox Previews: Photos are transcoded to FHD or 4K resolutions for the lightbox view, saving bandwidth compared to loading full-resolution originals.

    Multi-layer Caching

    To prevent slow directory scanning, the app implements a three-tier caching strategy:

    1. Server Scan: The app scans the directory when first navigated to.
    2. Database Cache: The scanned file structure and metadata are stored in the DB for reuse.
    3. Client Cache: The client stores the scanned file structure locally, allowing directories to be displayed instantly without server communication if they were recently visited.
  12. Enable typographic replacements in Markdown

    master
    PiGallery2 supports typographic replacements (smart quotes, symbols, etc.) if the typographer option is enabled in your configuration. This allows for automatic conversion of characters like (c) to © or handling of ellipses and smart quotes.