This commit is contained in:
Marek Lenczewski
2026-04-07 18:01:34 +02:00
parent ca988345e9
commit 375a9cd386
5 changed files with 73 additions and 65 deletions

View File

@@ -1,3 +1,5 @@
from pathlib import Path
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import Session, relationship
@@ -17,6 +19,14 @@ class Video(Base):
filePath = Column("file_path", String, nullable=True)
profiles = relationship("Profile", secondary=videoProfiles, backref="videos")
@property
def isDownloaded(self) -> bool:
return self.filePath is not None
@property
def profileIds(self) -> list[int]:
return [p.id for p in self.profiles]
@classmethod
def deleteIfExists(cls, db: Session, youtubeUrl: str, profileId: int | None):
if not profileId:
@@ -80,6 +90,29 @@ class Video(Base):
video.filePath = path
db.commit()
@classmethod
def getValidPath(cls, db: Session, videoId: int):
video = cls.getById(db, videoId)
if not video or not video.filePath:
return None, None
path = Path(video.filePath)
if not path.exists():
cls.updateFilePath(db, videoId, None)
return None, None
return path, video
@classmethod
def deleteServerFile(cls, db: Session, videoId: int) -> bool:
video = cls.getById(db, videoId)
if not video:
return False
if video.filePath:
path = Path(video.filePath)
if path.exists():
path.unlink()
cls.updateFilePath(db, videoId, None)
return True
@classmethod
def deleteNotDownloaded(cls, db: Session, profileId: int, excludeIds: list[int] | None = None) -> int:
query = db.query(cls).filter(