LDOC Viewer Backend

This directory contains the backend implementation for the LDOC Viewer application.

Overview

The backend provides:

  1. LDOC format implementation (reader and writer classes)
  2. API endpoints for handling LDOC operations
  3. Static file serving for the frontend

Core Components

LDOC Writer

The LDOCWriter class handles the creation of LDOC containers:

  • Compresses files using zlib
  • Detects file types and MIME types
  • Creates the LDOC manifest
  • Assembles the final container with header, manifest, file data, and trailer

LDOC Reader

The LDOCReader class handles the extraction and inspection of LDOC containers:

  • Parses the LDOC header
  • Reads the manifest
  • Extracts files from the container
  • Provides metadata about the container and its contents

API Endpoints

The backend exposes the following API endpoints:

  • POST /api/encode: Creates an LDOC container from uploaded files
  • POST /api/info: Returns metadata about an LDOC container
  • POST /api/extract: Extracts all files from an LDOC container as a ZIP archive
  • POST /api/view: Returns a list of files in an LDOC container with metadata
  • POST /api/view/file: Returns the content of a specific file from an LDOC container

File Type Detection

The backend includes helper functions for detecting file types:

  • isTextFile: Determines if a file is a text file based on extension or MIME type
  • isImageFile: Determines if a file is an image based on extension or MIME type
  • isViewableFile: Determines if a file can be viewed in the browser

LDOC Format Specification

The LDOC format consists of:

  1. Header (32 bytes):

    • Magic bytes "LDOC01" (6 bytes)
    • Version (1 byte)
    • Flags (1 byte)
    • Manifest length (4 bytes)
    • Manifest offset (8 bytes)
    • Trailer offset (8 bytes)
    • Reserved (4 bytes)
  2. Manifest:

    • CBOR-encoded array of objects
    • Each object contains metadata about a file in the container
  3. File Data:

    • Compressed file contents using zlib
  4. Trailer:

    • JSON-encoded revision history