- 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
34 lines
872 B
Python
34 lines
872 B
Python
from threading import TIMEOUT_MAX, Event
|
|
|
|
from apscheduler.schedulers.base import STATE_STOPPED, BaseScheduler
|
|
|
|
|
|
class BlockingScheduler(BaseScheduler):
|
|
"""
|
|
A scheduler that runs in the foreground
|
|
(:meth:`~apscheduler.schedulers.base.BaseScheduler.start` will block).
|
|
"""
|
|
|
|
_event = None
|
|
|
|
def start(self, *args, **kwargs):
|
|
if self._event is None or self._event.is_set():
|
|
self._event = Event()
|
|
|
|
super().start(*args, **kwargs)
|
|
self._main_loop()
|
|
|
|
def shutdown(self, wait=True):
|
|
super().shutdown(wait)
|
|
self._event.set()
|
|
|
|
def _main_loop(self):
|
|
wait_seconds = TIMEOUT_MAX
|
|
while self.state != STATE_STOPPED:
|
|
self._event.wait(wait_seconds)
|
|
self._event.clear()
|
|
wait_seconds = self._process_jobs()
|
|
|
|
def wakeup(self):
|
|
self._event.set()
|