add database manager and media manager
This commit is contained in:
79
core/media.py
Normal file
79
core/media.py
Normal file
@@ -0,0 +1,79 @@
|
||||
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)
|
||||
|
||||
# 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
|
||||
|
||||
disk_files = {f.name for f in color_dir.iterdir() if f.is_file() and f.name != "icon.jpg"}
|
||||
db_files = {m["filename"] 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)
|
||||
else:
|
||||
shutil.copy(DEFAULT_ICON, icon_file)
|
||||
|
||||
self.db.add_color(name, has_icon=True)
|
||||
|
||||
def remove_color(self, name: str):
|
||||
if (MEDIA_DIR / name).exists():
|
||||
shutil.rmtree(MEDIA_DIR / name)
|
||||
self.db.delete_color(name)
|
||||
|
||||
def add_file(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, file_path.name, ftype)
|
||||
|
||||
def remove_file(self, color: str, filename: str):
|
||||
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, filename)
|
||||
Reference in New Issue
Block a user