|
1 |
| -import os |
| 1 | +from mutagen.mp3 import MP3 |
| 2 | +from mutagen.id3 import ID3, APIC |
| 3 | +from pathlib import Path |
| 4 | +import hashlib |
| 5 | +import logging |
| 6 | +from queue import Queue |
| 7 | +from threading import Thread |
2 | 8 | import jukebox.cfghandler
|
3 | 9 |
|
| 10 | +COVER_PREFIX = 'cover' |
| 11 | +NO_COVER_ART_EXTENSION = 'no-art' |
| 12 | +NO_CACHE = '' |
| 13 | +CACHE_PENDING = 'CACHE_PENDING' |
| 14 | + |
| 15 | +logger = logging.getLogger('jb.CoverartCacheManager') |
4 | 16 | cfg = jukebox.cfghandler.get_handler('jukebox')
|
5 | 17 |
|
6 | 18 |
|
7 | 19 | class CoverartCacheManager:
|
8 | 20 | def __init__(self):
|
9 | 21 | coverart_cache_path = cfg.setndefault('webapp', 'coverart_cache_path', value='../../src/webapp/build/cover-cache')
|
10 |
| - self.cache_folder_path = os.path.expanduser(coverart_cache_path) |
| 22 | + self.cache_folder_path = Path(coverart_cache_path).expanduser() |
| 23 | + self.write_queue = Queue() |
| 24 | + self.worker_thread = Thread(target=self.process_write_requests) |
| 25 | + self.worker_thread.daemon = True # Ensure the thread closes with the program |
| 26 | + self.worker_thread.start() |
| 27 | + |
| 28 | + def generate_cache_key(self, base_filename: str) -> str: |
| 29 | + return f"{COVER_PREFIX}-{hashlib.sha256(base_filename.encode()).hexdigest()}" |
| 30 | + |
| 31 | + def get_cache_filename(self, mp3_file_path: str) -> str: |
| 32 | + base_filename = Path(mp3_file_path).stem |
| 33 | + cache_key = self.generate_cache_key(base_filename) |
| 34 | + |
| 35 | + for path in self.cache_folder_path.iterdir(): |
| 36 | + if path.stem == cache_key: |
| 37 | + if path.suffix == f".{NO_COVER_ART_EXTENSION}": |
| 38 | + return NO_CACHE |
| 39 | + return path.name |
| 40 | + |
| 41 | + self.save_to_cache(mp3_file_path) |
| 42 | + return CACHE_PENDING |
| 43 | + |
| 44 | + def save_to_cache(self, mp3_file_path: str): |
| 45 | + self.write_queue.put(mp3_file_path) |
11 | 46 |
|
12 |
| - def find_file_by_hash(self, hash_value): |
13 |
| - for filename in os.listdir(self.cache_folder_path): |
14 |
| - if filename.startswith(hash_value): |
15 |
| - return filename |
16 |
| - return None |
| 47 | + def _save_to_cache(self, mp3_file_path: str): |
| 48 | + base_filename = Path(mp3_file_path).stem |
| 49 | + cache_key = self.generate_cache_key(base_filename) |
| 50 | + file_extension, data = self._extract_album_art(mp3_file_path) |
17 | 51 |
|
18 |
| - def save_to_cache(self, base_filename, album_art_data): |
19 |
| - mime_type = album_art_data['type'] |
20 |
| - file_extension = 'jpg' if mime_type == 'image/jpeg' else mime_type.split('/')[-1] |
21 |
| - cache_filename = f"{base_filename}.{file_extension}" |
| 52 | + cache_filename = f"{cache_key}.{file_extension}" |
| 53 | + full_path = self.cache_folder_path / cache_filename # Works due to Pathlib |
22 | 54 |
|
23 |
| - with open(os.path.join(self.cache_folder_path, cache_filename), 'wb') as file: |
24 |
| - file.write(album_art_data['binary']) |
| 55 | + with full_path.open('wb') as file: |
| 56 | + file.write(data) |
| 57 | + logger.debug(f"Created file: {cache_filename}") |
25 | 58 |
|
26 | 59 | return cache_filename
|
| 60 | + |
| 61 | + def _extract_album_art(self, mp3_file_path: str) -> tuple: |
| 62 | + try: |
| 63 | + audio_file = MP3(mp3_file_path, ID3=ID3) |
| 64 | + except Exception as e: |
| 65 | + logger.error(f"Error reading MP3 file {mp3_file_path}: {e}") |
| 66 | + return (NO_COVER_ART_EXTENSION, b'') |
| 67 | + |
| 68 | + for tag in audio_file.tags.values(): |
| 69 | + if isinstance(tag, APIC): |
| 70 | + mime_type = tag.mime |
| 71 | + file_extension = 'jpg' if mime_type == 'image/jpeg' else mime_type.split('/')[-1] |
| 72 | + return (file_extension, tag.data) |
| 73 | + |
| 74 | + return (NO_COVER_ART_EXTENSION, b'') |
| 75 | + |
| 76 | + def process_write_requests(self): |
| 77 | + while True: |
| 78 | + mp3_file_path = self.write_queue.get() |
| 79 | + try: |
| 80 | + self._save_to_cache(mp3_file_path) |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Error processing write request: {e}") |
| 83 | + self.write_queue.task_done() |
| 84 | + |
| 85 | + def flush_cache(self): |
| 86 | + for path in self.cache_folder_path.iterdir(): |
| 87 | + if path.is_file(): |
| 88 | + path.unlink() |
| 89 | + logger.debug(f"Deleted cached file: {path.name}") |
| 90 | + logger.info("Cache flushed successfully.") |
0 commit comments