yt-dlp 2.7.2

🎬️ A Rust library (with auto dependencies installation) for Youtube downloading
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! JSON file-system based cache backend implementation.
//!
//! This module provides a simple file-system based cache where metadata is stored as JSON files.

use std::path::{Path, PathBuf};

use super::{
    DEFAULT_FILE_TTL, DEFAULT_PLAYLIST_TTL, DEFAULT_VIDEO_TTL, FileBackend, PlaylistBackend, VideoBackend,
    copy_to_cache, url_hash,
};

/// An expired JSON cache entry found during a directory scan.
struct ExpiredJsonEntry {
    /// Path to the JSON metadata file on disk.
    path: PathBuf,
    /// Original URL, used to derive and delete the companion `.url` index file.
    url: Option<String>,
}

/// Scans `dir` for `.json` files whose `cached_at` field indicates expiry under `ttl`.
///
/// Reads each file as a `serde_json::Value` to avoid a generic bound, extracting
/// `cached_at` (i64 Unix timestamp) and optionally `url` for companion index cleanup.
async fn list_expired_json_entries(dir: &Path, ttl: u64) -> Result<Vec<ExpiredJsonEntry>> {
    let mut expired = Vec::new();
    let mut entries = tokio::fs::read_dir(dir).await?;

    while let Ok(Some(entry)) = entries.next_entry().await {
        let path = entry.path();
        if path.extension().is_none_or(|ext| ext != "json") {
            continue;
        }
        let Ok(content) = tokio::fs::read_to_string(&path).await else {
            continue;
        };
        let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) else {
            continue;
        };
        let Some(cached_at) = val.get("cached_at").and_then(|v| v.as_i64()) else {
            continue;
        };
        if is_expired(cached_at, ttl) {
            let url = val.get("url").and_then(|v| v.as_str()).map(str::to_string);
            expired.push(ExpiredJsonEntry { path, url });
        }
    }

    Ok(expired)
}
use crate::cache::playlist::CachedPlaylist;
use crate::cache::video::{CachedFile, CachedThumbnail, CachedVideo};
use crate::error::Result;
use crate::model::Video;
use crate::model::playlist::Playlist;
use crate::model::selector::FormatPreferences;
use crate::utils::is_expired;

/// JSON-backed video cache implementation.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
///
/// use yt_dlp::cache::backend::VideoBackend;
/// use yt_dlp::cache::backend::json::JsonVideoCache;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let cache = JsonVideoCache::new(PathBuf::from("/tmp/cache"), None).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct JsonVideoCache {
    cache_dir: PathBuf,
    ttl: u64,
}

impl JsonVideoCache {
    pub async fn new(cache_dir: PathBuf, ttl: Option<u64>) -> Result<Self> {
        let video_dir = cache_dir.join("videos");
        if !video_dir.exists() {
            tokio::fs::create_dir_all(&video_dir).await?;
        }
        Ok(Self {
            cache_dir: video_dir,
            ttl: ttl.unwrap_or(DEFAULT_VIDEO_TTL),
        })
    }
}

impl JsonVideoCache {
    /// Looks up a video by URL using the O(1) URL→ID index file.
    ///
    /// Returns `Ok(Some(video))` on a valid, non-expired cache hit, `Ok(None)` on a miss or expiry.
    /// Removes stale index and data files when an expired entry is found.
    async fn try_index_lookup(&self, url: &str) -> Result<Option<Video>> {
        let index_path = self.cache_dir.join(format!("{}.url", url_hash(url)));
        if !index_path.exists() {
            return Ok(None);
        }
        let Ok(id) = tokio::fs::read_to_string(&index_path).await else {
            let _ = tokio::fs::remove_file(&index_path).await;
            return Ok(None);
        };
        let file_path = self.cache_dir.join(format!("{}.json", id.trim()));
        if !file_path.exists() {
            let _ = tokio::fs::remove_file(&index_path).await;
            return Ok(None);
        }
        let content = tokio::fs::read_to_string(&file_path).await?;
        if let Ok(cached) = serde_json::from_str::<CachedVideo>(&content)
            && cached.url == url
        {
            if is_expired(cached.cached_at, self.ttl) {
                tracing::debug!(
                    url = url,
                    cached_at = cached.cached_at,
                    ttl = self.ttl,
                    "⚙️ Cache expired for video"
                );
                let _ = tokio::fs::remove_file(&file_path).await;
                let _ = tokio::fs::remove_file(&index_path).await;
                return Ok(None);
            }
            tracing::debug!(
                url = url,
                video_id = cached.id,
                video_title = cached.title,
                "✅ Cache hit for video (indexed)"
            );
            return Ok(Some(cached.video()?));
        }
        // Stale index entry (URL mismatch — hash collision or overwrite).
        let _ = tokio::fs::remove_file(&index_path).await;
        Ok(None)
    }

    /// Scans the cache directory for a video entry matching `url`.
    ///
    /// Used as a fallback when no URL→ID index exists (backward compatibility
    /// with entries written before index files were introduced).
    async fn fallback_dir_scan(&self, url: &str) -> Result<Option<Video>> {
        let mut entries = tokio::fs::read_dir(&self.cache_dir).await?;
        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let content = tokio::fs::read_to_string(entry.path()).await?;
                if let Ok(cached) = serde_json::from_str::<CachedVideo>(&content)
                    && cached.url == url
                {
                    if is_expired(cached.cached_at, self.ttl) {
                        tracing::debug!(
                            url = url,
                            cached_at = cached.cached_at,
                            ttl = self.ttl,
                            "⚙️ Cache expired for video"
                        );
                        let _ = tokio::fs::remove_file(entry.path()).await;
                        return Ok(None);
                    }
                    tracing::debug!(
                        url = url,
                        video_id = cached.id,
                        video_title = cached.title,
                        "✅ Cache hit for video"
                    );
                    return Ok(Some(cached.video()?));
                }
            }
        }
        Ok(None)
    }
}

impl VideoBackend for JsonVideoCache {
    async fn get(&self, url: &str) -> Result<Option<Video>> {
        tracing::debug!(
            url = url,
            cache_dir = ?self.cache_dir,
            ttl = self.ttl,
            "🔍 Looking for video in JSON cache by URL"
        );

        // Try the URL→ID index first for O(1) lookup.
        if let Some(video) = self.try_index_lookup(url).await? {
            return Ok(Some(video));
        }

        // Fallback: directory scan for backward compatibility with pre-index entries.
        self.fallback_dir_scan(url).await
    }

    async fn put(&self, url: String, video: Video) -> Result<()> {
        tracing::debug!(
            url = url,
            video_id = video.id,
            video_title = video.title,
            cache_dir = ?self.cache_dir,
            "⚙️ Caching video to JSON backend"
        );
        let id = video.id.clone();
        let cached = CachedVideo::new(url.clone(), &video)?;
        let file_path = self.cache_dir.join(format!("{}.json", cached.id));
        let content = serde_json::to_string(&cached)?;
        tokio::fs::write(file_path, content).await?;

        // Write URL→ID index for O(1) lookup
        let index_path = self.cache_dir.join(format!("{}.url", url_hash(&url)));
        tokio::fs::write(index_path, &id).await?;

        Ok(())
    }

    async fn remove(&self, url: &str) -> Result<()> {
        tracing::debug!(
            url = url,
            cache_dir = ?self.cache_dir,
            "⚙️ Removing video from JSON cache"
        );

        // Remove URL→ID index
        let index_path = self.cache_dir.join(format!("{}.url", url_hash(url)));
        let _ = tokio::fs::remove_file(&index_path).await;

        let mut entries = tokio::fs::read_dir(&self.cache_dir).await?;
        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let content = tokio::fs::read_to_string(entry.path()).await?;
                if let Ok(cached) = serde_json::from_str::<CachedVideo>(&content)
                    && cached.url == url
                {
                    tokio::fs::remove_file(entry.path()).await?;
                    return Ok(());
                }
            }
        }
        Ok(())
    }

    async fn clean(&self) -> Result<()> {
        tracing::debug!(
            ttl = self.ttl,
            cache_dir = ?self.cache_dir,
            "⚙️ Cleaning JSON video cache"
        );
        let expired = list_expired_json_entries(&self.cache_dir, self.ttl).await?;
        for entry in expired {
            let _ = tokio::fs::remove_file(&entry.path).await;
            if let Some(url) = entry.url {
                let url_index = self.cache_dir.join(format!("{}.url", url_hash(&url)));
                let _ = tokio::fs::remove_file(&url_index).await;
            }
        }
        Ok(())
    }

    async fn get_by_id(&self, id: &str) -> Result<CachedVideo> {
        tracing::debug!(video_id = id, cache_dir = ?self.cache_dir, "🔍 Looking up video by ID in JSON cache");

        let file_path = self.cache_dir.join(format!("{}.json", id));
        if file_path.exists() {
            let content = tokio::fs::read_to_string(file_path).await?;
            let cached: CachedVideo =
                serde_json::from_str(&content).map_err(|e| crate::error::Error::json("Deserialize cached video", e))?;

            if is_expired(cached.cached_at, self.ttl) {
                return Err(crate::error::Error::cache_expired(id));
            }
            return Ok(cached);
        }
        Err(crate::error::Error::cache_miss(id))
    }
}

/// JSON-backed playlist cache implementation.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
///
/// use yt_dlp::cache::backend::PlaylistBackend;
/// use yt_dlp::cache::backend::json::JsonPlaylistCache;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let cache = JsonPlaylistCache::new(PathBuf::from("/tmp/cache"), None).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct JsonPlaylistCache {
    cache_dir: PathBuf,
    ttl: u64,
}

impl JsonPlaylistCache {
    pub async fn new(cache_dir: PathBuf, ttl: Option<u64>) -> Result<Self> {
        let list_dir = cache_dir.join("playlists");
        if !list_dir.exists() {
            tokio::fs::create_dir_all(&list_dir).await?;
        }
        Ok(Self {
            cache_dir: list_dir,
            ttl: ttl.unwrap_or(DEFAULT_PLAYLIST_TTL),
        })
    }
}

impl PlaylistBackend for JsonPlaylistCache {
    async fn get(&self, url: &str) -> Result<Option<Playlist>> {
        tracing::debug!(
            url = url,
            cache_dir = ?self.cache_dir,
            ttl = self.ttl,
            "🔍 Looking for playlist in JSON cache by URL"
        );
        let mut entries = tokio::fs::read_dir(&self.cache_dir).await?;
        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let content = tokio::fs::read_to_string(entry.path()).await?;
                if let Ok(cached) = serde_json::from_str::<CachedPlaylist>(&content)
                    && cached.url == url
                {
                    if is_expired(cached.cached_at, self.ttl) {
                        tracing::debug!(
                            url = url,
                            cached_at = cached.cached_at,
                            ttl = self.ttl,
                            "⚙️ Cache expired for playlist"
                        );
                        let _ = tokio::fs::remove_file(entry.path()).await;
                        return Ok(None);
                    }
                    tracing::debug!(
                        url = url,
                        playlist_id = cached.id,
                        playlist_title = cached.title,
                        "✅ Cache hit for playlist"
                    );
                    return Ok(Some(cached.playlist()?));
                }
            }
        }
        Ok(None)
    }

    async fn get_by_id(&self, id: &str) -> Result<Option<Playlist>> {
        tracing::debug!(playlist_id = id, cache_dir = ?self.cache_dir, "🔍 Looking up playlist by ID in JSON cache");

        let file_path = self.cache_dir.join(format!("{}.json", id));
        if file_path.exists() {
            let content = tokio::fs::read_to_string(file_path).await?;
            let cached: CachedPlaylist = serde_json::from_str(&content)
                .map_err(|e| crate::error::Error::json("Deserialize cached playlist", e))?;

            if is_expired(cached.cached_at, self.ttl) {
                return Ok(None);
            }
            return Ok(Some(cached.playlist()?));
        }
        Ok(None)
    }

    async fn put(&self, url: String, playlist: Playlist) -> Result<()> {
        tracing::debug!(
            url = url,
            playlist_id = playlist.id,
            playlist_title = playlist.title,
            entry_count = playlist.entries.len(),
            cache_dir = ?self.cache_dir,
            "⚙️ Caching playlist to JSON backend"
        );
        let cached = CachedPlaylist::from((url, playlist));
        let file_path = self.cache_dir.join(format!("{}.json", cached.id));
        let content = serde_json::to_string(&cached)?;
        tokio::fs::write(file_path, content).await?;
        Ok(())
    }

    async fn invalidate(&self, url: &str) -> Result<()> {
        tracing::debug!(
            url = url,
            cache_dir = ?self.cache_dir,
            "⚙️ Invalidating playlist in JSON cache"
        );
        let mut entries = tokio::fs::read_dir(&self.cache_dir).await?;
        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let content = tokio::fs::read_to_string(entry.path()).await?;
                if let Ok(cached) = serde_json::from_str::<CachedPlaylist>(&content)
                    && cached.url == url
                {
                    tokio::fs::remove_file(entry.path()).await?;
                    return Ok(());
                }
            }
        }
        Ok(())
    }

    async fn clean(&self) -> Result<()> {
        tracing::debug!(
            ttl = self.ttl,
            cache_dir = ?self.cache_dir,
            "⚙️ Cleaning JSON playlist cache"
        );
        let expired = list_expired_json_entries(&self.cache_dir, self.ttl).await?;
        for entry in expired {
            let _ = tokio::fs::remove_file(&entry.path).await;
            if let Some(url) = entry.url {
                let url_index = self.cache_dir.join(format!("{}.url", url_hash(&url)));
                let _ = tokio::fs::remove_file(&url_index).await;
            }
        }
        Ok(())
    }

    async fn clear_all(&self) -> Result<()> {
        tracing::debug!(cache_dir = ?self.cache_dir, "⚙️ Clearing all playlists from JSON cache");

        tokio::fs::remove_dir_all(&self.cache_dir).await?;
        tokio::fs::create_dir_all(&self.cache_dir).await?;
        Ok(())
    }
}

/// JSON-backed file cache implementation.
///
/// # Examples
///
/// ```rust
/// use std::path::PathBuf;
///
/// use yt_dlp::cache::backend::FileBackend;
/// use yt_dlp::cache::backend::json::JsonFileCache;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let cache = JsonFileCache::new(PathBuf::from("/tmp/cache"), None).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct JsonFileCache {
    cache_dir: PathBuf,
    ttl: u64,
}

impl JsonFileCache {
    pub async fn new(cache_dir: PathBuf, ttl: Option<u64>) -> Result<Self> {
        let files_dir = cache_dir.join("files_meta");
        if !files_dir.exists() {
            tokio::fs::create_dir_all(&files_dir).await?;
        }
        // Also ensure actual file storage exists
        let storage_dir = cache_dir.join("files");
        if !storage_dir.exists() {
            tokio::fs::create_dir_all(&storage_dir).await?;
        }

        // Ensure thumbnail directories exist
        let thumbnails_meta = cache_dir.join("thumbnails_meta");
        if !thumbnails_meta.exists() {
            tokio::fs::create_dir_all(&thumbnails_meta).await?;
        }
        let thumbnails_dir = cache_dir.join("thumbnails");
        if !thumbnails_dir.exists() {
            tokio::fs::create_dir_all(&thumbnails_dir).await?;
        }

        Ok(Self {
            cache_dir, // We keep root cache dir to access subdirectories
            ttl: ttl.unwrap_or(DEFAULT_FILE_TTL),
        })
    }

    async fn clean_expired_files(&self) -> Result<()> {
        let meta_dir = self.cache_dir.join("files_meta");
        let mut entries = tokio::fs::read_dir(&meta_dir).await?;

        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_none_or(|ext| ext != "json") {
                continue;
            }

            let content = tokio::fs::read_to_string(entry.path()).await?;
            if let Ok(cached) = serde_json::from_str::<CachedFile>(&content)
                && is_expired(cached.cached_at, self.ttl)
            {
                let file_path = self.cache_dir.join(&cached.relative_path);
                if file_path.exists() {
                    let _ = tokio::fs::remove_file(file_path).await;
                }
                let _ = tokio::fs::remove_file(entry.path()).await;
            }
        }

        Ok(())
    }

    async fn clean_expired_thumbnails(&self) -> Result<()> {
        let thumb_meta_dir = self.cache_dir.join("thumbnails_meta");
        let Ok(mut entries) = tokio::fs::read_dir(&thumb_meta_dir).await else {
            return Ok(());
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_none_or(|ext| ext != "json") {
                continue;
            }

            let content = tokio::fs::read_to_string(entry.path()).await?;
            if let Ok(cached) = serde_json::from_str::<CachedThumbnail>(&content)
                && is_expired(cached.cached_at, self.ttl)
            {
                let file_path = self.cache_dir.join(&cached.relative_path);
                if file_path.exists() {
                    let _ = tokio::fs::remove_file(file_path).await;
                }
                let _ = tokio::fs::remove_file(entry.path()).await;
            }
        }

        Ok(())
    }
}

impl FileBackend for JsonFileCache {
    async fn get_by_hash(&self, hash: &str) -> Result<Option<(CachedFile, PathBuf)>> {
        tracing::debug!(
            hash = hash,
            cache_dir = ?self.cache_dir,
            ttl = self.ttl,
            "🔍 Looking for file in JSON cache by hash"
        );
        let meta_path = self.cache_dir.join("files_meta").join(format!("{}.json", hash));
        if meta_path.exists() {
            let content = tokio::fs::read_to_string(&meta_path).await?;
            let cached: CachedFile = serde_json::from_str(&content)?;

            if is_expired(cached.cached_at, self.ttl) {
                tracing::debug!(
                    hash = hash,
                    cached_at = cached.cached_at,
                    ttl = self.ttl,
                    "⚙️ Cache expired for file"
                );
                return Ok(None);
            }

            let file_path = self.cache_dir.join(&cached.relative_path);
            if file_path.exists() {
                tracing::debug!(
                    hash = hash,
                    filename = cached.filename,
                    file_path = ?file_path,
                    "✅ Cache hit for file"
                );
                return Ok(Some((cached, file_path)));
            }
        }
        Ok(None)
    }

    async fn get_by_video_and_format(&self, video_id: &str, format_id: &str) -> Result<Option<(CachedFile, PathBuf)>> {
        tracing::debug!(video_id = video_id, format_id = format_id, cache_dir = ?self.cache_dir, "🔍 Looking for file by video and format in JSON cache");

        let meta_dir = self.cache_dir.join("files_meta");
        let mut entries = match tokio::fs::read_dir(&meta_dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e.into()),
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let Ok(content) = tokio::fs::read_to_string(entry.path()).await else {
                    continue;
                };
                if let Ok(cached) = serde_json::from_str::<CachedFile>(&content)
                    && cached.video_id.as_deref() == Some(video_id)
                    && cached.format_id.as_deref() == Some(format_id)
                {
                    if is_expired(cached.cached_at, self.ttl) {
                        continue;
                    }
                    let file_path = self.cache_dir.join(&cached.relative_path);
                    if file_path.exists() {
                        return Ok(Some((cached, file_path)));
                    }
                }
            }
        }
        Ok(None)
    }

    #[cfg(cache)]
    async fn get_by_video_and_preferences(
        &self,
        video_id: &str,
        preferences: &FormatPreferences,
    ) -> Result<Option<(CachedFile, PathBuf)>> {
        // Scan and filter
        let meta_dir = self.cache_dir.join("files_meta");
        let mut entries = match tokio::fs::read_dir(&meta_dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e.into()),
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let Ok(content) = tokio::fs::read_to_string(entry.path()).await else {
                    continue;
                };
                if let Ok(cached) = serde_json::from_str::<CachedFile>(&content)
                    && cached.video_id.as_deref() == Some(video_id)
                    && cached.matches_preferences(preferences)
                {
                    if is_expired(cached.cached_at, self.ttl) {
                        continue;
                    }
                    let file_path = self.cache_dir.join(&cached.relative_path);
                    if file_path.exists() {
                        return Ok(Some((cached, file_path)));
                    }
                }
            }
        }
        Ok(None)
    }

    async fn put(&self, file: CachedFile, source_path: &Path) -> Result<PathBuf> {
        tracing::debug!(
            filename = file.filename,
            file_id = file.id,
            source_path = ?source_path,
            video_id = ?file.video_id,
            format_id = ?file.format_id,
            cache_dir = ?self.cache_dir,
            "⚙️ Caching file to JSON backend"
        );
        let file_path = copy_to_cache(&self.cache_dir, &file.relative_path, source_path).await?;

        // Write metadata
        let meta_path = self.cache_dir.join("files_meta").join(format!("{}.json", file.id));
        let meta_json = serde_json::to_string(&file)?;
        tokio::fs::write(meta_path, meta_json).await?;

        Ok(file_path)
    }

    async fn remove(&self, id: &str) -> Result<()> {
        tracing::debug!(
            file_id = id,
            cache_dir = ?self.cache_dir,
            "⚙️ Removing file from JSON cache"
        );
        let meta_path = self.cache_dir.join("files_meta").join(format!("{}.json", id));
        if meta_path.exists() {
            // Read to get relative path and delete file
            let content = tokio::fs::read_to_string(&meta_path).await?;
            if let Ok(cached) = serde_json::from_str::<CachedFile>(&content) {
                let file_path = self.cache_dir.join(&cached.relative_path);
                if file_path.exists() {
                    tokio::fs::remove_file(file_path).await?;
                }
            }
            tokio::fs::remove_file(meta_path).await?;
        }
        Ok(())
    }

    async fn clean(&self) -> Result<()> {
        tracing::debug!(
            ttl = self.ttl,
            cache_dir = ?self.cache_dir,
            "⚙️ Cleaning JSON file cache"
        );

        self.clean_expired_files().await?;
        self.clean_expired_thumbnails().await?;

        Ok(())
    }

    async fn get_thumbnail_by_video_id(&self, video_id: &str) -> Result<Option<(CachedThumbnail, PathBuf)>> {
        tracing::debug!(video_id = video_id, cache_dir = ?self.cache_dir, "🔍 Looking for thumbnail by video ID in JSON cache");

        let meta_dir = self.cache_dir.join("thumbnails_meta");
        let mut entries = match tokio::fs::read_dir(&meta_dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e.into()),
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            if entry.path().extension().is_some_and(|ext| ext == "json") {
                let Ok(content) = tokio::fs::read_to_string(entry.path()).await else {
                    continue;
                };
                if let Ok(cached) = serde_json::from_str::<CachedThumbnail>(&content)
                    && cached.video_id == video_id
                {
                    if is_expired(cached.cached_at, self.ttl) {
                        continue;
                    }
                    let file_path = self.cache_dir.join(&cached.relative_path);
                    if file_path.exists() {
                        return Ok(Some((cached, file_path)));
                    }
                }
            }
        }
        Ok(None)
    }

    async fn put_thumbnail(&self, thumbnail: CachedThumbnail, source_path: &Path) -> Result<PathBuf> {
        let file_path = copy_to_cache(&self.cache_dir, &thumbnail.relative_path, source_path).await?;

        // Store metadata
        let meta_path = self
            .cache_dir
            .join("thumbnails_meta")
            .join(format!("{}.json", thumbnail.id));
        let json = serde_json::to_string(&thumbnail)
            .map_err(|e| crate::error::Error::json("Serialize cached thumbnail", e))?;
        tokio::fs::write(&meta_path, json).await?;

        Ok(file_path)
    }

    async fn get_subtitle_by_language(&self, video_id: &str, language: &str) -> Result<Option<(CachedFile, PathBuf)>> {
        tracing::debug!(video_id = video_id, language = language, cache_dir = ?self.cache_dir, "🔍 Looking for subtitle by language in JSON cache");

        let meta_dir = self.cache_dir.join("files_meta");
        let mut entries = match tokio::fs::read_dir(&meta_dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e.into()),
        };

        while let Ok(Some(entry)) = entries.next_entry().await {
            if let Some(result) =
                try_match_subtitle_entry(&entry.path(), video_id, language, self.ttl, &self.cache_dir).await
            {
                return Ok(Some(result));
            }
        }
        Ok(None)
    }
}

/// Attempts to load and validate a subtitle cache entry at `path`.
///
/// Returns `Some((cached, file_path))` when the entry matches the requested
/// `video_id` / `language`, has not expired, and the cached file exists on disk.
/// Returns `None` for any other case (wrong extension, parse error, mismatch, expired,
/// or missing file).
async fn try_match_subtitle_entry(
    path: &std::path::Path,
    video_id: &str,
    language: &str,
    ttl: u64,
    cache_dir: &std::path::Path,
) -> Option<(CachedFile, PathBuf)> {
    if path.extension().is_none_or(|ext| ext != "json") {
        return None;
    }
    let content = tokio::fs::read_to_string(path).await.ok()?;
    let cached: CachedFile = serde_json::from_str(&content).ok()?;
    if cached.video_id.as_deref() != Some(video_id) || cached.language_code.as_deref() != Some(language) {
        return None;
    }
    if is_expired(cached.cached_at, ttl) {
        return None;
    }
    let file_path = cache_dir.join(&cached.relative_path);
    file_path.exists().then_some((cached, file_path))
}