# Storage

Shared by the client and server: `MSFormat` identifies a file's format, and `StorePlan` decides where a transferred file lands on disk and whether it is converted (`store_as`) on the way.

## store

Decide where and in what format a transferred file is stored on disk.

### MSFormat

Bases: `StrEnum`

An on-disk MS data format.

Members compare equal to their `DownloadFormat` string values.

#### ext

```python
ext: str
```

The canonical file extension, used to name converted outputs.

#### from\_path

```python
from_path(name: str) -> MSFormat | None
```

Infer the format from a filename extension, or *None*.

#### from\_url

```python
from_url(url: str) -> MSFormat | None
```

Infer the format from the URL filename extension, or *None*.

#### from\_header

```python
from_header(value: str) -> MSFormat
```

Parse an `X-Source-Format` header value, case-insensitively.

Raises `ValueError` for an unrecognised value.

### Conversion

```python
Conversion(source: MSFormat, target: MSFormat)
```

Turning a *source*-format file into a *target*-format file.

#### supported

```python
supported: bool
```

Whether a converter exists for this pair of formats.

#### run

```python
run(src: Path, dst: Path) -> None
```

Convert *src* to *dst* in a worker thread.

### MszxExpansion

```python
MszxExpansion(
    src: Path, entries: tuple[str, ...], target: MSFormat
)
```

Writes every member of a multi-member .mszx into one directory.

Each spectra *entry* of *src* becomes `<entry stem>.<ext>` stored as *target* (`MSFormat.MSZ` or `MSFormat.MZML`).

#### targets

```python
targets: list[tuple[str, str]]
```

Map each spectra entry to an output file name.

Raises `ValueError` for unsafe names, or if two entries map to the same output name.

#### write

```python
write(archive_dir: Path) -> Path
```

Store every member in *archive\_dir*.

Members are extracted into a uniquely named staging directory that is renamed to *archive\_dir* only once all of them succeed, so a failure leaves no partial output behind and concurrent downloads never share staging files. Names are validated before anything is written.

### StorePlan

```python
StorePlan(
    dest: Path,
    source: MSFormat | None,
    target: MSFormat | None,
)
```

How a file downloaded to *dest* ends up on disk.

*source* is the format of the downloaded bytes (or *None* if unknown) and *target* the requested `store_as` format (or *None* to keep the file as-is). Build one with `for_download` to also validate the combination before any I/O.

#### conversion

```python
conversion: Conversion | None
```

The conversion needed, or *None* if the file is stored as-is.

#### final\_dest

```python
final_dest: Path
```

The output path, with its extension adjusted for any conversion.

#### part\_path

```python
part_path: Path
```

Where the download is streamed before it is finalized.

#### may\_expand

```python
may_expand: bool
```

Whether a multi-member .mszx would be expanded into a directory.

#### archive\_dir

```python
archive_dir: Path
```

Where the members of a multi-member .mszx are written.

#### for\_download

```python
for_download(
    url: str, dest: Path, store_as: DownloadFormat | None
) -> StorePlan
```

Plan storage for *url*, failing fast on unsupported conversions.

#### for\_upload

```python
for_upload(
    dest_dir: Path,
    filename: str,
    source: MSFormat | None,
    store_as: MSFormat,
) -> StorePlan
```

Plan storage for an upload of *filename* into *dest\_dir*.

*source* is the format the client declared, or *None* to infer it from *filename*. mzML is compressed on the wire, so anything but an .mszx archive arrives as MSZ. Archives are always kept as-is, whatever *store\_as* says.

#### unique\_part\_path

```python
unique_part_path() -> Path
```

A `part_path` variant that concurrent writers never share.

Two uploads can target the same *dest* (e.g. `run.msz` and `run.mzML` both arrive as `run.msz`), so each needs its own file.

#### existing

```python
existing() -> Path | None
```

Return the output already on disk, if any.

#### finalize

```python
finalize(part: Path) -> Path
```

Move or convert the downloaded *part* file into its final form.

Returns the final destination, or the archive directory for a multi-member `.mszx`.
