---
# System prepended metadata

title: Multimedia Repository Documentation

---

# Multimedia Repository Documentation

## Overview

The `MultimediaRepo` class is a repository for managing multimedia files (e.g., images, audio, video) using GridFS in MongoDB. It provides methods to upload, retrieve, update, and delete files in GridFS.

## Class Initialization

```python
MultimediaRepo(db: database.Database)
```

- Parameters:
  - `db: database.Database`: The MongoDB database instance.

## Methods

### `get_all(limit: int = 0) -> list[dict]`

- Retrieve all files from GridFS.
- Parameters:
  - `limit: int`: (Optional) The maximum number of files to retrieve. Default is 0, which retrieves all files.
- Returns:
  - `list[dict]`: A list of dictionaries, each representing a file in GridFS.

### `upload(file_data: bytes, content_type: str) -> ObjectId`

- Upload a file to GridFS.
- Parameters:
  - `file_data: bytes`: The binary data of the file to be uploaded.
  - `content_type: str`: The MIME type of the file (e.g., "image/jpeg", "audio/mp3").
- Returns:
  - `ObjectId`: The `_id` of the newly uploaded file in GridFS.

### `get(file_id: ObjectId) -> Optional[bytes]`

- Retrieve a file from GridFS by its `_id`.
- Parameters:
  - `file_id: ObjectId`: The `_id` of the file to retrieve from GridFS.
- Returns:
  - `Optional[bytes]`: The binary data of the retrieved file, or `None` if the file does not exist.

### `update(file_id: ObjectId, new_file_data: bytes, content_type: str) -> None`

- Update an existing file in GridFS.
- Parameters:
  - `file_id: ObjectId`: The `_id` of the file to update in GridFS.
  - `new_file_data: bytes`: The new binary data of the file to be updated.
  - `content_type: str`: The MIME type of the new file data (e.g., "image/jpeg", "audio/mp3").
- Returns:
  - `None`
- Note: This method deletes the existing file with the specified `_id` and uploads the new file data with the same `_id`.

### `delete(file_id: ObjectId) -> None`

- Delete a file from GridFS.
- Parameters:
  - `file_id: ObjectId`: The `_id` of the file to be deleted from GridFS.
- Returns:
  - `None`
- Exception:
  - Raises `NoFile` exception if the file with the specified `_id` does not exist in GridFS.

## Example Usage

```python
db = ...  # MongoDB database instance
multimedia_repo = MultimediaRepo(db)

# Upload a file
file_data = b'binary file data'
content_type = 'image/jpeg'
file_id = multimedia_repo.upload(file_data, content_type)

# Retrieve a file
retrieved_file_data = multimedia_repo.get(file_id)

# Update a file
new_file_data = b'new binary file data'
multimedia_repo.update(file_id, new_file_data, content_type)

# Delete a file
multimedia_repo.delete(file_id)
```
