- 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
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
from importlib import import_module
|
|
from itertools import product
|
|
|
|
from apscheduler.schedulers.base import BaseScheduler
|
|
|
|
for version, pkgname in product(range(6, 1, -1), ("PySide", "PyQt")):
|
|
try:
|
|
qtcore = import_module(pkgname + str(version) + ".QtCore")
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
QTimer = qtcore.QTimer
|
|
break
|
|
else:
|
|
raise ImportError("QtScheduler requires either PySide/PyQt (v6 to v2) installed")
|
|
|
|
|
|
class QtScheduler(BaseScheduler):
|
|
"""A scheduler that runs in a Qt event loop."""
|
|
|
|
_timer = None
|
|
|
|
def shutdown(self, *args, **kwargs):
|
|
super().shutdown(*args, **kwargs)
|
|
self._stop_timer()
|
|
|
|
def _start_timer(self, wait_seconds):
|
|
self._stop_timer()
|
|
if wait_seconds is not None:
|
|
wait_time = min(int(wait_seconds * 1000), 2147483647)
|
|
self._timer = QTimer.singleShot(wait_time, self._process_jobs)
|
|
|
|
def _stop_timer(self):
|
|
if self._timer:
|
|
if self._timer.isActive():
|
|
self._timer.stop()
|
|
del self._timer
|
|
|
|
def wakeup(self):
|
|
self._start_timer(0)
|
|
|
|
def _process_jobs(self):
|
|
wait_seconds = super()._process_jobs()
|
|
self._start_timer(wait_seconds)
|