You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
from pathlib import Path
|
|
import shutil
|
|
import uuid
|
|
from datetime import datetime
|
|
import cv2
|
|
import numpy as np
|
|
|
|
class FileSystemStorage:
|
|
def __init__(self, base_path: str):
|
|
self.base_path = Path(base_path)
|
|
self.base_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
async def save_image(self, image: np.ndarray, user_id: str) -> str:
|
|
user_dir = self.base_path / user_id
|
|
user_dir.mkdir(exist_ok=True)
|
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
filename = f"{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
|
|
file_path = user_dir / filename
|
|
|
|
cv2.imwrite(str(file_path), cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
|
|
return str(file_path)
|
|
|
|
async def load_image(self, file_path: str) -> np.ndarray:
|
|
image = cv2.imread(file_path)
|
|
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
|
async def delete_image(self, file_path: str) -> bool:
|
|
try:
|
|
Path(file_path).unlink()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
async def delete_user_directory(self, user_id: str) -> bool:
|
|
try:
|
|
user_dir = self.base_path / user_id
|
|
if user_dir.exists():
|
|
shutil.rmtree(user_dir)
|
|
return True
|
|
except Exception:
|
|
return False |