- Create backend/services/image_storage.py with 4 core functions:
- sanitize_filename(): remove unsafe chars, limit to 255 chars, convert to lowercase
- get_unique_filename(): handle collisions with UUID suffix (format: {name}_{uuid8}_{variant}.jpg)
- ensure_image_directories(): create /images/ root and category subdirs on startup
- save_image(): save bytes to /images/{category}/{filename}, returns relative path
- Create comprehensive test suite (22 tests) covering all functionality
- Integrate ensure_image_directories() into FastAPI startup event
- Directory structure: /images/{category}/{filename}
- Collision handling: auto-suffix with UUID if filename exists
- All tests passing, pathlib.Path for safe operations
37 lines
943 B
Python
37 lines
943 B
Python
from __future__ import annotations
|
|
|
|
import enum
|
|
import typing as t
|
|
|
|
|
|
class Sentinel(enum.Enum):
|
|
"""Enum used to define sentinel values.
|
|
|
|
.. seealso::
|
|
|
|
`PEP 661 - Sentinel Values <https://peps.python.org/pep-0661/>`_.
|
|
"""
|
|
|
|
UNSET = object()
|
|
FLAG_NEEDS_VALUE = object()
|
|
|
|
def __repr__(self) -> str:
|
|
return f"{self.__class__.__name__}.{self.name}"
|
|
|
|
|
|
UNSET = Sentinel.UNSET
|
|
"""Sentinel used to indicate that a value is not set."""
|
|
|
|
FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE
|
|
"""Sentinel used to indicate an option was passed as a flag without a
|
|
value but is not a flag option.
|
|
|
|
``Option.consume_value`` uses this to prompt or use the ``flag_value``.
|
|
"""
|
|
|
|
T_UNSET = t.Literal[UNSET] # type: ignore[valid-type]
|
|
"""Type hint for the :data:`UNSET` sentinel value."""
|
|
|
|
T_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE] # type: ignore[valid-type]
|
|
"""Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value."""
|