Files
MayoStainHelper/core/media.py

96 lines
3.6 KiB
Python

from pathlib import Path
import shutil
from core.database import DatabaseManager
MEDIA_DIR = Path("media")
DEFAULT_ICON = Path("media/default_icon.jpg")
class MediaRepository:
def __init__(self, db: DatabaseManager):
self.db = db
def sync_media(self):
disk_colors = {d.name for d in MEDIA_DIR.iterdir() if d.is_dir()}
db_colors = {c["name"] for c in self.db.get_all_colors()}
# usuwanie brakujących kolorów
for missing in db_colors - disk_colors:
self.db.delete_color(missing)
# dodawanie nowych kolorów
for color in disk_colors - db_colors:
# has_icon = (MEDIA_DIR / color / "icon.jpg").exists()
# self.db.add_color(color, has_icon=has_icon)
# icon = MEDIA_DIR / color / "icon.jpg"
self.add_color(color)
# sprawdzanie plików dla każdego koloru
for color in disk_colors:
color_id = self.db.get_color_id(color)
if color_id is None:
continue
color_dir = MEDIA_DIR / color
icon_file = color_dir / "icon.jpg"
self.db.update_color_icon(color, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
disk_files = {f.as_posix() for f in color_dir.iterdir() if f.is_file() and f.name != "icon.jpg"}
db_files = {m["media_path"] for m in self.db.get_media_for_color(color_id)}
# usuń brakujące
for missing in db_files - disk_files:
self.db.delete_media(color_id, missing)
# dodaj nowe
for new in disk_files - db_files:
ftype = "photo" if Path(new).suffix.lower() in [".jpg", ".png"] else "video"
self.db.add_media(color_id, new, ftype)
def add_color(self, name: str, icon_path: Path | None = None):
color_dir = MEDIA_DIR / name
color_dir.mkdir(parents=True, exist_ok=True)
icon_file = color_dir / "icon.jpg"
if icon_path and icon_path.exists():
shutil.copy(icon_path, icon_file)
self.db.add_color(name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
def remove_color(self, name: str):
if (MEDIA_DIR / name).exists():
shutil.rmtree(MEDIA_DIR / name)
self.db.delete_color(name)
def update_color(self, old_name: str, new_name: str, icon_path: Path | None = None):
old_dir = MEDIA_DIR / old_name
new_dir = MEDIA_DIR / new_name
icon_file = new_dir / "icon.jpg"
if old_dir.exists():
old_dir.rename(new_dir)
if icon_path and icon_path.exists():
shutil.copy(icon_path, icon_file)
self.db.update_color_name(old_name, new_name)
self.db.update_color_icon(new_name, icon_file.as_posix() if icon_file.exists() else DEFAULT_ICON.as_posix())
def add_media(self, color: str, file_path: Path):
target_dir = MEDIA_DIR / color
target_dir.mkdir(exist_ok=True)
target_file = target_dir / file_path.name
shutil.copy(file_path, target_file)
ftype = "photo" if file_path.suffix.lower() in [".jpg", ".png"] else "video"
color_id = self.db.get_color_id(color)
if color_id is not None:
self.db.add_media(color_id, target_file.as_posix(), ftype)
def remove_media(self, color: str, file_path: Path):
# file_path = MEDIA_DIR / color / filename
if file_path.exists():
file_path.unlink()
color_id = self.db.get_color_id(color)
if color_id is not None:
self.db.delete_media(color_id, file_path.as_posix())