Metadata-Version: 2.3
Name: mcms-files
Version: 0.1.0
Summary: Provides managed file and media handling for MCMS
Author: MikemathuKE
Author-email: MikemathuKE <michaelmathu6@gmail.com>
Requires-Dist: mcms-core
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# MCMS Files

`mcms_files` provides managed file and media handling for MCMS. It separates the file's domain identity and metadata from the physical storage of its bytes and from the persistence of its metadata.

## Responsibilities

- Represent managed files as domain objects.
- Define the physical storage contract through `FileStorage`.
- Provide `LocalFileStorage` as the initial storage implementation.
- Persist file metadata through `FileModel` and `FileRepository`.
- Coordinate operations spanning persistence and physical storage through `FileManager`.
- Distinguish archiving a file from permanently purging its physical contents.

## Architecture

```text
File (domain object)
    |
    | storage_key
    v
FileStorage
    |
    +--> LocalFileStorage
    |
    v
Physical file bytes

File
    |
    v
FileRepository
    |
    v
FileModel (Django persistence)
```

### Domain object

`File` represents CMS-level identity and metadata, including its ID, name, MIME type, size, storage key, status, metadata, and timestamps. It does not depend on Django or a particular storage backend, and it does not contain the file bytes.

### Storage

`FileStorage` is an abstract base class defining the storage operations:

- `save()`
- `delete()`
- `exists()`
- `open()`
- `url()`

`LocalFileStorage` is the initial concrete implementation. Storage keys are relative to the configured root and must not escape that root through path traversal.

### Persistence

`FileModel` is the Django persistence representation of the file metadata. It does not use Django's `FileField` to manage physical storage.

`FileRepository` translates between `File` and `FileModel`. It owns persistence operations and does not manipulate physical file contents.

### Coordination

`FileManager` coordinates operations that involve both physical storage and persistence. For example, file creation stores the bytes and then persists metadata; if persistence fails, the manager attempts compensating cleanup of the stored file.

### Lifecycle

The current lifecycle distinguishes:

- `ACTIVE`
- `ARCHIVED`
- Purging as a separate physical deletion operation

Archiving removes a file from active CMS use without immediately deleting its bytes.

## Configuration

This package has the file_repository and file_manager accessible trhough its application definition.

## Architectural integrations

| Package / system | Relationship |
|---|---|
| Django | Persistence model and database operations |
| `mcms_core` | Shared infrastructure and exception conventions |
| Host application | Supplies storage root/base URL configuration and Django app setup |

## Explicitly excluded from the current architecture

- A `FileRegistry`
- A generic `FileService` layer
- Django `FileField` as the physical storage mechanism
- Immediate physical deletion when a file is archived

S3/object storage, cleanup of archived files, orphan detection, deduplication, versioning, transformations, chunked uploads, permissions, CDN integration, and quotas are future considerations rather than current guarantees.

## Dependencies

### Architectural integrations

The domain model is independent of Django and storage infrastructure. The repository owns persistence; the storage implementation owns physical bytes; the manager coordinates cross-boundary operations.

### Python distribution dependencies

All MCMS Packages rely on django

## Development

```bash
uv run pytest packages/mcms-files/tests
```

Run Ruff and mypy using the repository's configured commands.
