- 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
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
from typing import Union, Any
|
|
from array import array
|
|
from .protocol import HTTPProtocol
|
|
|
|
class HttpParser:
|
|
def __init__(self, protocol: Union[HTTPProtocol, Any]) -> None:
|
|
"""
|
|
protocol -- a Python object with the following methods
|
|
(all optional):
|
|
|
|
- on_message_begin()
|
|
- on_url(url: bytes)
|
|
- on_header(name: bytes, value: bytes)
|
|
- on_headers_complete()
|
|
- on_body(body: bytes)
|
|
- on_message_complete()
|
|
- on_chunk_header()
|
|
- on_chunk_complete()
|
|
- on_status(status: bytes)
|
|
"""
|
|
|
|
def get_http_version(self) -> str:
|
|
"""Return an HTTP protocol version."""
|
|
...
|
|
|
|
def should_keep_alive(self) -> bool:
|
|
"""Return ``True`` if keep-alive mode is preferred."""
|
|
...
|
|
|
|
def should_upgrade(self) -> bool:
|
|
"""Return ``True`` if the parsed request is a valid Upgrade request.
|
|
The method exposes a flag set just before on_headers_complete.
|
|
Calling this method earlier will only yield `False`."""
|
|
...
|
|
|
|
def feed_data(self, data: Union[bytes, bytearray, memoryview, array]) -> None:
|
|
"""Feed data to the parser.
|
|
|
|
Will eventually trigger callbacks on the ``protocol``
|
|
object.
|
|
|
|
On HTTP upgrade, this method will raise an
|
|
``HttpParserUpgrade`` exception, with its sole argument
|
|
set to the offset of the non-HTTP data in ``data``.
|
|
"""
|
|
|
|
class HttpRequestParser(HttpParser):
|
|
"""Used for parsing http requests from the server's side"""
|
|
|
|
def get_method(self) -> bytes:
|
|
"""Return HTTP request method (GET, HEAD, etc)"""
|
|
|
|
class HttpResponseParser(HttpParser):
|
|
"""Used for parsing http requests from the client's side"""
|
|
|
|
def get_status_code(self) -> int:
|
|
"""Return the status code of the HTTP response"""
|