mirror of
https://git.srhtnin.garden/sarah/SaraTune.git
synced 2026-10-13 18:18:11 +02:00
Media controls (MPRIS, Windows SMTC); stop old tracks when switching
This commit is contained in:
1 parent
e2d7515e08
commit
d7f5eba1c2
14 files changed
+799
-26
No files matched your search
@@ -31,18 +31,33 @@ def Thumbnail(Data: bytes, Size: int) -> Optional[bytes]:
|
||||
_Memory: dict[tuple[str, int], bytes] = {}
|
||||
|
||||
|
||||
def _CachePath(FilePath: str, Size: int) -> Optional[Path]:
|
||||
try:
|
||||
Stamp = Path(FilePath).stat().st_mtime_ns
|
||||
except OSError:
|
||||
return None
|
||||
Name = hashlib.sha1(f"{FilePath}|{Size}|{Stamp}".encode("utf-8", "surrogateescape")).hexdigest()
|
||||
return CoversDir / f"{Name}.jpg"
|
||||
|
||||
|
||||
def CoverPath(FilePath: str, Size: int) -> Optional[Path]:
|
||||
"""The cached thumbnail's file (made if needed), for the system media
|
||||
controls, which want a picture file rather than bytes. Blocking."""
|
||||
if not CoverFor(FilePath, Size):
|
||||
return None
|
||||
CachePath = _CachePath(FilePath, Size)
|
||||
return CachePath if CachePath and CachePath.exists() else None
|
||||
|
||||
|
||||
def CoverFor(FilePath: str, Size: int) -> Optional[bytes]:
|
||||
"""FilePath's cover at most Size pixels wide. Blocking (run it in a
|
||||
thread). Cached on disk by path, size and the file's mtime."""
|
||||
Key = (FilePath, Size)
|
||||
if Key in _Memory:
|
||||
return _Memory[Key] or None
|
||||
try:
|
||||
Stamp = Path(FilePath).stat().st_mtime_ns
|
||||
except OSError:
|
||||
CachePath = _CachePath(FilePath, Size)
|
||||
if CachePath is None:
|
||||
return None
|
||||
Name = hashlib.sha1(f"{FilePath}|{Size}|{Stamp}".encode("utf-8", "surrogateescape")).hexdigest()
|
||||
CachePath = CoversDir / f"{Name}.jpg"
|
||||
if CachePath.exists():
|
||||
Data = CachePath.read_bytes()
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user