src — media

Module: src-media Cohesion: 0.80 Members: 0

src — media

The src/media/media-pipeline.ts module provides a robust and configurable pipeline for handling temporary media files within the application. Its primary purpose is to safely ingest, store, manage, and process various media types, ensuring adherence to size limits and offering automatic cleanup mechanisms.

This module is crucial for features that involve receiving or generating media (e.g., images, audio, video, documents) that need temporary storage and potential processing before being used or discarded.

Core Concepts

The module revolves around a few key interfaces and the central MediaPipeline class:

The MediaPipeline Class

The MediaPipeline class is the central component, extending Node.js's EventEmitter to signal important lifecycle events.

1. Initialization and Configuration

The MediaPipeline is initialized with an optional configuration object. If not provided, DEFAULT_CONFIG is used.

const pipeline = new MediaPipeline({
  tempDir: './my-app-media-cache',
  maxFileSizeMb: 50,
  allowedTypes: ['image', 'audio'],
});

Upon instantiation:

2. Media Ingestion

The ingest(filePath: string) method is the primary way to add files to the pipeline.

graph TD
    A[External File Path] --> B{MediaPipeline.ingest()};
    B --> C{File Exists?};
    C -- No --> E[Return Error: Not Found];
    C -- Yes --> D{Size & Total Size Limits?};
    D -- Exceeded --> E;
    D -- OK --> F{Detect Type & Allowed?};
    F -- Not Allowed --> E;
    F -- Allowed --> G[Generate UUID & Temp Path];
    G --> H[Copy File to Temp Dir];
    H --> I[Create MediaFile Object];
    I --> J[Store MediaFile & Update Total Size];
    J --> K[Emit 'ingested' Event];
    K --> L[Return MediaFile];

Ingestion Flow:

  1. Validation: Checks if the filePath exists, if its size exceeds maxFileSizeMb, and if adding it would exceed maxTotalSizeMb.
  2. Type Detection: Uses MediaPipeline.detectType() to determine the MediaType based on the file extension and verifies it against allowedTypes.
  3. Temporary Storage: Generates a unique ID (randomUUID) and a temporary path within tempDir. The file is then copied to this temporary location.
  4. MediaFile Creation: A MediaFile object is created, storing all relevant information.
  5. Internal Tracking: The MediaFile is stored in an internal Map keyed by its id, and the totalSize is updated.
  6. Event Emission: An ingested event is emitted with the MediaFile object.

3. Media Retrieval and Management

4. Extending Functionality with Hooks

The pipeline supports custom processing logic through TranscriptionHooks.

interface MyTranscriptionHook extends TranscriptionHook {
  name: 'audio-transcriber';
  mediaTypes: ['audio'];
  process: async (file: MediaFile) => {
    class="hl-cmt">// Call an external transcription service
    const transcription = await transcribeAudio(file.tempPath);
    return transcription;
  };
}

pipeline.registerHook(new MyTranscriptionHook());
const results = await pipeline.processHooks(audioFile.id);
class="hl-cmt">// results might contain the transcription string

5. Automatic Cleanup and Disposal

6. Event Emitter

MediaPipeline extends EventEmitter and emits the following events:

Static Utility Methods

The MediaPipeline class provides two static helper methods for type detection:

Internal Mappings

The module uses two internal constant maps for efficient type and MIME type detection:

Integration with the System

Based on the call graph, the MediaPipeline module is integrated as follows:

The MediaPipeline module does not make explicit outgoing calls to other application modules. Its interactions are primarily internal (e.g., ingest calling detectType) and through its EventEmitter interface, allowing other parts of the system to react to media lifecycle events. This design promotes loose coupling, where the pipeline manages media, and other modules subscribe to its events for further processing or UI updates.