embystream 0.0.36

Another Emby streaming application (frontend/backend separation) written in Rust.
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
use std::{
    collections::HashMap,
    fs::File as StdFile,
    io::{Error as IoError, ErrorKind},
    path::{Path, PathBuf},
    pin::Pin,
    sync::Arc,
    time::{Instant, SystemTime},
};

use bytes::Bytes;
use futures_util::{StreamExt, TryStreamExt};
use http_body_util::{BodyExt, StreamBody};
use hyper::body::Frame;
use hyper::{HeaderMap, StatusCode, header};
use lazy_static::lazy_static;

use super::{
    read_stream::ReaderStream,
    response::Response,
    result::Result as AppStreamResult,
    types::{
        ClientInfo, ContentRange, PreparedLocalStreamTarget, RangeParseError,
    },
};
use crate::cache::{FileMetadata, RateLimiter};
use crate::gateway::error::Error as GatewayError;
use crate::util::string_util::StringUtil;
use crate::{
    AppState, LOCAL_STREAMER_LOGGER_DOMAIN, debug_log, error_log, info_log,
    warn_log,
};

pub(crate) struct LocalStreamer;

const LOCAL_METADATA_CACHE_KEY_PREFIX: &str = "backend:local_metadata";

#[derive(Debug, Clone, Copy)]
struct MetadataLoadStats {
    cache_hit: bool,
    lock_wait_ms: u128,
    metadata_ms: u128,
}

impl LocalStreamer {
    pub async fn stream(
        state: Arc<AppState>,
        path: PathBuf,
        mut range_header: Option<String>,
        client_info: ClientInfo,
        node_uuid: &str,
    ) -> Result<AppStreamResult, StatusCode> {
        let client_id_value = match client_info.id {
            Some(value) if !value.is_empty() => value,
            _ => {
                error_log!(
                    LOCAL_STREAMER_LOGGER_DOMAIN,
                    "Empty client id for '{:?}'",
                    &path,
                );
                return Err(StatusCode::FORBIDDEN);
            }
        };
        let playback_session_id = match client_info.playback_session_id {
            Some(value) if !value.is_empty() => value,
            _ => {
                error_log!(
                    LOCAL_STREAMER_LOGGER_DOMAIN,
                    "Empty playback session id for '{:?}'",
                    &path,
                );
                return Err(StatusCode::BAD_REQUEST);
            }
        };

        let limiter = match state.get_rate_limiter_cache(node_uuid).await {
            Some(cache) => cache.fetch_limiter(&client_id_value).await,
            None => {
                info_log!(
                    LOCAL_STREAMER_LOGGER_DOMAIN,
                    "local_stream_unlimited_no_limiter_cache node_uuid={} path={:?} hint=non-Disk_or_unknown_node",
                    node_uuid,
                    path
                );
                RateLimiter::unlimited()
            }
        };

        let problematic_clients = state.get_problematic_clients().await;

        Self::fix_range_header_if_needed(
            &mut range_header,
            &client_info.user_agent,
            problematic_clients,
        )
        .await;

        let Some(range_value) = range_header.as_deref() else {
            error_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "No-Range req for '{:?}' rejected. IP: {:?}, Client: {:?}, ClientID: {:?}",
                &path,
                client_info.ip,
                client_info.user_agent,
                client_id_value,
            );
            return Err(StatusCode::FORBIDDEN);
        };

        info_log!(
            LOCAL_STREAMER_LOGGER_DOMAIN,
            "local_stream_session device_id={} session_id={} path={:?} range={}",
            client_id_value,
            playback_session_id,
            path,
            range_value
        );

        let prepared_target =
            Self::prepare_stream_target(state.clone(), path).await?;

        let content_range = match Self::parse_content_range(
            range_value,
            prepared_target.file_metadata.file_size,
        ) {
            Ok(range) => {
                debug_log!(
                    LOCAL_STREAMER_LOGGER_DOMAIN,
                    "Successfully parsed content range: {:?} for path: {:?}",
                    range,
                    &prepared_target.path
                );
                range
            }
            Err(RangeParseError::Malformed) => {
                return Err(StatusCode::BAD_REQUEST);
            }
            Err(RangeParseError::Unsatisfiable) => {
                return Err(StatusCode::RANGE_NOT_SATISFIABLE);
            }
        };

        Self::stream_file(
            prepared_target,
            content_range,
            StatusCode::PARTIAL_CONTENT,
            limiter,
        )
        .await
    }

    async fn prepare_stream_target(
        state: Arc<AppState>,
        path: PathBuf,
    ) -> Result<PreparedLocalStreamTarget, StatusCode> {
        match Self::prepare_direct_target(state.clone(), &path).await {
            Ok(target) => Ok(target),
            Err(primary_err) => {
                warn_log!(
                    LOCAL_STREAMER_LOGGER_DOMAIN,
                    "primary_stream_target_unavailable path={:?} error={} \
                     hint=trying_fallback_video",
                    path,
                    primary_err
                );

                let Some(fallback_path) =
                    Self::fallback_path(state.clone()).await
                else {
                    return Err(StatusCode::NOT_FOUND);
                };

                if fallback_path == path {
                    return Err(StatusCode::NOT_FOUND);
                }

                match Self::prepare_direct_target(state.clone(), &fallback_path)
                    .await
                {
                    Ok(target) => {
                        warn_log!(
                            LOCAL_STREAMER_LOGGER_DOMAIN,
                            "Using fallback video for unavailable path={:?} \
                             fallback={:?}",
                            path,
                            fallback_path
                        );
                        Ok(target.with_fallback(true))
                    }
                    Err(fallback_err) => {
                        error_log!(
                            LOCAL_STREAMER_LOGGER_DOMAIN,
                            "fallback_stream_target_unavailable path={:?} \
                             fallback={:?} error={}",
                            path,
                            fallback_path,
                            fallback_err
                        );
                        Err(StatusCode::NOT_FOUND)
                    }
                }
            }
        }
    }

    async fn prepare_direct_target(
        state: Arc<AppState>,
        path: &Path,
    ) -> Result<PreparedLocalStreamTarget, IoError> {
        const SLOW_PREPARE_TARGET_THRESHOLD_MS: u128 = 500;

        let prepare_started = Instant::now();
        let probe_path = path.to_path_buf();
        let open_started = Instant::now();
        let opened_file =
            tokio::task::spawn_blocking(move || StdFile::open(&probe_path))
                .await
                .map_err(|err| {
                    IoError::other(format!(
                        "blocking open task failed for {:?}: {}",
                        path, err
                    ))
                })??;
        let open_ms = open_started.elapsed().as_millis();
        let metadata_started = Instant::now();
        let (file_metadata, metadata_stats) =
            Self::load_cached_file_metadata(state, path, &opened_file).await?;
        let metadata_total_ms = metadata_started.elapsed().as_millis();
        let prepare_ms = prepare_started.elapsed().as_millis();

        if prepare_ms >= SLOW_PREPARE_TARGET_THRESHOLD_MS {
            info_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "local_prepare_target_slow elapsed_ms={} open_ms={} metadata_total_ms={} metadata_io_ms={} lock_wait_ms={} cache_hit={} path={:?}",
                prepare_ms,
                open_ms,
                metadata_total_ms,
                metadata_stats.metadata_ms,
                metadata_stats.lock_wait_ms,
                metadata_stats.cache_hit,
                path
            );
        } else {
            debug_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "local_prepare_target_complete elapsed_ms={} open_ms={} metadata_total_ms={} metadata_io_ms={} lock_wait_ms={} cache_hit={} path={:?}",
                prepare_ms,
                open_ms,
                metadata_total_ms,
                metadata_stats.metadata_ms,
                metadata_stats.lock_wait_ms,
                metadata_stats.cache_hit,
                path
            );
        }

        Ok(
            PreparedLocalStreamTarget::new(path.to_path_buf(), file_metadata)
                .with_opened_file(opened_file),
        )
    }

    async fn load_cached_file_metadata(
        state: Arc<AppState>,
        path: &Path,
        opened_file: &StdFile,
    ) -> Result<(FileMetadata, MetadataLoadStats), IoError> {
        let cache_key = Self::local_metadata_cache_key(path);
        let cache = state.get_local_metadata_cache().await;

        if let Some(cached) = cache.get::<FileMetadata>(&cache_key) {
            debug_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "local_metadata_cache_hit key={} path={:?}",
                cache_key,
                path
            );
            return Ok((
                cached,
                MetadataLoadStats {
                    cache_hit: true,
                    lock_wait_ms: 0,
                    metadata_ms: 0,
                },
            ));
        }

        let cache_lock = AppState::request_lock(
            &state.local_metadata_request_locks,
            &cache_key,
        );
        let lock_started = Instant::now();
        let guard = cache_lock.lock().await;
        let lock_wait_ms = lock_started.elapsed().as_millis();

        if let Some(cached) = cache.get::<FileMetadata>(&cache_key) {
            debug_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "local_metadata_inflight_wait_hit key={} path={:?}",
                cache_key,
                path
            );
            drop(guard);
            AppState::cleanup_request_lock(
                &state.local_metadata_request_locks,
                &cache_key,
                &cache_lock,
            );
            return Ok((
                cached,
                MetadataLoadStats {
                    cache_hit: true,
                    lock_wait_ms,
                    metadata_ms: 0,
                },
            ));
        }

        let metadata_started = Instant::now();
        let metadata = opened_file.metadata()?;
        let metadata_ms = metadata_started.elapsed().as_millis();
        if !metadata.is_file() {
            drop(guard);
            AppState::cleanup_request_lock(
                &state.local_metadata_request_locks,
                &cache_key,
                &cache_lock,
            );
            return Err(IoError::new(
                ErrorKind::NotFound,
                format!("path is not a file: {:?}", path),
            ));
        }

        let file_metadata = FileMetadata {
            file_size: metadata.len(),
            file_name: path
                .file_name()
                .and_then(|s| s.to_str())
                .map_or_else(|| "unknown".to_string(), |s| s.to_string()),
            format: path
                .extension()
                .and_then(|s| s.to_str())
                .map_or_else(|| "unknown".to_string(), |s| s.to_string()),
            last_modified: metadata.modified().ok(),
            updated_at: SystemTime::now(),
        };

        cache.insert(cache_key.clone(), file_metadata.clone());
        debug_log!(
            LOCAL_STREAMER_LOGGER_DOMAIN,
            "local_metadata_cache_store key={} path={:?}",
            cache_key,
            path
        );

        drop(guard);
        AppState::cleanup_request_lock(
            &state.local_metadata_request_locks,
            &cache_key,
            &cache_lock,
        );

        Ok((
            file_metadata,
            MetadataLoadStats {
                cache_hit: false,
                lock_wait_ms,
                metadata_ms,
            },
        ))
    }

    fn local_metadata_cache_key(path: &Path) -> String {
        let raw_path = path.to_string_lossy();
        let path_hash = StringUtil::hash_hex(raw_path.trim_end());
        format!("{LOCAL_METADATA_CACHE_KEY_PREFIX}:path_hash:{path_hash}")
    }

    async fn fallback_path(state: Arc<AppState>) -> Option<PathBuf> {
        let config = state.get_config().await;
        let fallback_path_str = &config.fallback.video_missing_path;
        if fallback_path_str.is_empty() {
            return None;
        }

        let fallback_path = PathBuf::from(fallback_path_str);
        if fallback_path.is_absolute() {
            Some(fallback_path)
        } else {
            Some(
                config
                    .path
                    .parent()
                    .unwrap_or_else(|| Path::new(""))
                    .join(fallback_path),
            )
        }
    }

    async fn stream_file(
        prepared_target: PreparedLocalStreamTarget,
        content_range: ContentRange,
        status_code: StatusCode,
        limiter: Arc<RateLimiter>,
    ) -> Result<AppStreamResult, StatusCode> {
        let PreparedLocalStreamTarget {
            path,
            file_metadata,
            opened_file,
            ..
        } = prepared_target;

        info_log!(
            LOCAL_STREAMER_LOGGER_DOMAIN,
            "Streaming file status {:?}, range: {:?}",
            status_code,
            content_range,
        );

        type Framed = Pin<
            Box<
                dyn futures_util::Stream<
                        Item = Result<Frame<Bytes>, GatewayError>,
                    > + Send
                    + Sync,
            >,
        >;

        let reader_stream = match opened_file {
            Some(opened_file) => ReaderStream::from_opened_file(
                path.clone(),
                opened_file,
                content_range,
            ),
            None => ReaderStream::new(path.clone(), content_range),
        };

        let stream: Framed = if limiter.skip_semaphore {
            let s = reader_stream
                .into_stream()
                .map(|res| res.map(Frame::data).map_err(GatewayError::from));
            Box::pin(s)
        } else {
            let sem = limiter.semaphore.clone();
            let s = reader_stream
                .into_stream()
                .and_then(move |chunk| {
                    let sem = sem.clone();
                    async move {
                        match sem.acquire_many(chunk.len() as u32).await {
                            Ok(permit) => {
                                permit.forget();
                                Ok(chunk)
                            }
                            Err(_) => Err(IoError::new(
                                ErrorKind::BrokenPipe,
                                "Semaphore closed",
                            )),
                        }
                    }
                })
                .map_ok(Frame::data)
                .map_err(GatewayError::from);
            Box::pin(s)
        };

        let mut headers = HeaderMap::new();
        if let Ok(content_type) =
            get_content_type(&file_metadata.format).parse()
        {
            headers.insert(header::CONTENT_TYPE, content_type);
        }
        if let Ok(accept_ranges) = "bytes".parse() {
            headers.insert(header::ACCEPT_RANGES, accept_ranges);
        }

        if status_code == StatusCode::PARTIAL_CONTENT {
            headers
                .insert(header::CONTENT_LENGTH, content_range.length().into());
            let range_str = format!(
                "bytes {}-{}/{}",
                content_range.start,
                content_range.end,
                content_range.total_size
            );
            if let Ok(range_value) = range_str.parse() {
                headers.insert(header::CONTENT_RANGE, range_value);
            }
        } else {
            headers.insert(
                header::CONTENT_LENGTH,
                content_range.total_size.into(),
            );
        }

        let response = Response {
            status: status_code,
            headers,
            body: BodyExt::boxed(StreamBody::new(stream)),
        };

        Ok(AppStreamResult::Stream(response))
    }

    /// Handles requests from specific clients that do not send a Range header by applying a default.
    ///
    /// # WARNING: Temporary Workaround
    /// This method serves as a temporary compatibility layer for clients (e.g., `yamby`, `hills`)
    /// that incorrectly omit the `Range` header in streaming requests.
    ///
    /// This is considered a tactical fix. The correct long-term solution is for the client applications
    /// to solve this issue. This workaround may be deprecated or removed in future releases
    /// as clients become compliant.
    async fn fix_range_header_if_needed(
        range_header: &mut Option<String>,
        client: &Option<String>,
        problematic_clients: &[String],
    ) {
        if let Some(header) = range_header {
            if !header.is_empty() {
                return;
            }
        }

        let Some(client_str) = client else {
            return;
        };

        let client_lower = client_str.to_lowercase();

        // Check if the client user agent contains any of the known problematic substrings.
        if problematic_clients.iter().any(|c| client_lower.contains(c)) {
            warn_log!(
                LOCAL_STREAMER_LOGGER_DOMAIN,
                "Client '{:?}' missing Range header. Applying workaround 'bytes=0-'.",
                client_str
            );
            *range_header = Some("bytes=0-".to_string());
        }
    }

    fn parse_content_range(
        range_value: &str,
        total_size: u64,
    ) -> Result<ContentRange, RangeParseError> {
        debug_log!(
            LOCAL_STREAMER_LOGGER_DOMAIN,
            "Start parsing content range: {:?}",
            range_value
        );

        let ranges = http_range_header::parse_range_header(range_value)
            .map_err(|_| RangeParseError::Malformed)?;

        let validated_ranges = ranges
            .validate(total_size)
            .map_err(|_| RangeParseError::Unsatisfiable)?;

        if let Some(first_range) = validated_ranges.first() {
            Ok(ContentRange {
                start: *first_range.start(),
                end: *first_range.end(),
                total_size,
            })
        } else {
            Err(RangeParseError::Unsatisfiable)
        }
    }
}

pub fn get_content_type(extension: &str) -> &'static str {
    lazy_static! {
        static ref CONTENT_TYPES: HashMap<&'static str, &'static str> = {
            let mut m = HashMap::new();
            // video
            m.insert("mp4", "video/mp4");
            m.insert("mkv", "video/x-matroska");
            m.insert("avi", "video/x-msvideo");
            m.insert("mov", "video/quicktime");
            m.insert("flv", "video/x-flv");
            m.insert("rmvb", "application/vnd.rn-realmedia-vbr");
            m.insert("rm", "application/vnd.rn-realmedia");

            // audio
            m.insert("mka", "audio/x-matroska");
            m.insert("aac", "audio/aac");
            m.insert("mp3", "audio/mpeg");
            m.insert("wav", "audio/wav");
            m.insert("ogg", "audio/ogg");

            // subtitle
            m.insert("srt", "application/x-subrip");
            m.insert("vtt", "text/vtt");
            m.insert("ass", "text/x-ssa");

            // picture
            m.insert("jpg", "image/jpeg");
            m.insert("jpeg", "image/jpeg");
            m.insert("png", "image/png");
            m.insert("gif", "image/gif");

            m
        };
    }

    let ext = extension.trim_start_matches('.').to_lowercase();
    CONTENT_TYPES
        .get(ext.as_str())
        .unwrap_or(&"application/octet-stream")
}

#[cfg(test)]
mod tests {
    use std::{fs, path::PathBuf};

    use tempfile::TempDir;

    use super::LocalStreamer;
    use crate::{
        AppState,
        config::core::{finish_raw_config, parse_raw_config_str},
        core::backend::{result::Result as AppStreamResult, types::ClientInfo},
        util::string_util::StringUtil,
    };

    async fn test_state_with_fallback(
        fallback_path: Option<&std::path::Path>,
    ) -> AppState {
        let fallback_value = fallback_path
            .map(|path| path.to_string_lossy().replace('\\', "\\\\"))
            .unwrap_or_default();
        let raw = format!(
            r#"
[Log]
level = "info"
prefix = ""
root_path = "./logs"

[General]
memory_mode = "middle"
stream_mode = "frontend"
encipher_key = "1234567890123456"
encipher_iv = "1234567890123456"

[Emby]
url = "http://127.0.0.1"
port = "8096"
token = "tok"

[UserAgent]
mode = "allow"
allow_ua = []
deny_ua = []

[Fallback]
video_missing_path = "{fallback_value}"

[Frontend]
listen_port = 60001
"#
        );
        let parsed = match parse_raw_config_str(&raw) {
            Ok(parsed) => parsed,
            Err(err) => panic!("parse raw config failed: {err}"),
        };
        let config = match finish_raw_config(PathBuf::from("test.toml"), parsed)
        {
            Ok(config) => config,
            Err(err) => panic!("finish raw config failed: {err}"),
        };
        AppState::new(config).await
    }

    fn write_test_file(dir: &TempDir, name: &str) -> PathBuf {
        let path = dir.path().join(name);
        assert!(fs::write(&path, b"hello world").is_ok());
        path
    }

    #[tokio::test]
    async fn prepare_stream_target_prefers_primary_path() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = write_test_file(&dir, "primary.mp4");
        let fallback_path = write_test_file(&dir, "fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let target =
            LocalStreamer::prepare_stream_target(state, primary_path.clone())
                .await
                .unwrap_or_else(|err| panic!("prepare primary target: {err}"));

        assert_eq!(target.path, primary_path);
        assert!(!target.is_fallback);
        assert!(target.has_opened_file());
    }

    #[test]
    fn local_metadata_cache_key_is_structured() {
        let path = PathBuf::from("/tmp/media/Foo Bar.mp4");
        let key = LocalStreamer::local_metadata_cache_key(&path);
        let expected_hash =
            StringUtil::hash_hex(path.to_string_lossy().trim_end());

        assert_eq!(
            key,
            format!("backend:local_metadata:path_hash:{expected_hash}")
        );
    }

    #[tokio::test]
    async fn prepare_stream_target_reuses_cached_metadata() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = write_test_file(&dir, "primary.mp4");
        let state = std::sync::Arc::new(test_state_with_fallback(None).await);

        let first = LocalStreamer::prepare_stream_target(
            state.clone(),
            primary_path.clone(),
        )
        .await
        .unwrap_or_else(|err| panic!("first prepare target: {err}"));
        let key = LocalStreamer::local_metadata_cache_key(&primary_path);
        let cache = state.get_local_metadata_cache().await;
        let cached = cache
            .get::<crate::cache::FileMetadata>(&key)
            .unwrap_or_else(|| panic!("metadata cached after first prepare"));

        let second = LocalStreamer::prepare_stream_target(state, primary_path)
            .await
            .unwrap_or_else(|err| panic!("second prepare target: {err}"));

        assert_eq!(cached.file_size, first.file_metadata.file_size);
        assert_eq!(cached.updated_at, first.file_metadata.updated_at);
        assert_eq!(second.file_metadata.updated_at, cached.updated_at);
    }

    #[tokio::test]
    async fn prepare_stream_target_uses_fallback_when_primary_missing() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = dir.path().join("missing.mp4");
        let fallback_path = write_test_file(&dir, "fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let target = LocalStreamer::prepare_stream_target(state, primary_path)
            .await
            .unwrap_or_else(|err| panic!("prepare fallback target: {err}"));

        assert_eq!(target.path, fallback_path);
        assert!(target.is_fallback);
        assert!(target.has_opened_file());
    }

    #[tokio::test]
    async fn prepare_stream_target_returns_not_found_when_primary_and_fallback_fail()
     {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = dir.path().join("missing.mp4");
        let fallback_path = dir.path().join("missing-fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let err = LocalStreamer::prepare_stream_target(state, primary_path)
            .await
            .expect_err("missing primary and fallback should fail");

        assert_eq!(err, hyper::StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn stream_returns_partial_content_for_primary_target() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = write_test_file(&dir, "primary.mp4");
        let fallback_path = write_test_file(&dir, "fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let result = LocalStreamer::stream(
            state,
            primary_path,
            Some("bytes=0-".to_string()),
            ClientInfo::new(
                Some("client-1".to_string()),
                Some("play-123-1".to_string()),
                None,
                None,
            ),
            "test-node",
        )
        .await
        .unwrap_or_else(|err| panic!("stream primary file: {err}"));

        match result {
            AppStreamResult::Stream(response) => {
                assert_eq!(response.status, hyper::StatusCode::PARTIAL_CONTENT);
            }
            AppStreamResult::Redirect(_) => {
                unreachable!("expected stream response")
            }
            AppStreamResult::AccelRedirect(_) => {
                unreachable!("expected stream response")
            }
        }
    }

    #[tokio::test]
    async fn stream_uses_fallback_when_primary_missing() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = dir.path().join("missing.mp4");
        let fallback_path = write_test_file(&dir, "fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let result = LocalStreamer::stream(
            state,
            primary_path,
            Some("bytes=0-".to_string()),
            ClientInfo::new(
                Some("client-1".to_string()),
                Some("play-123-1".to_string()),
                None,
                None,
            ),
            "test-node",
        )
        .await
        .unwrap_or_else(|err| panic!("stream fallback file: {err}"));

        match result {
            AppStreamResult::Stream(response) => {
                assert_eq!(response.status, hyper::StatusCode::PARTIAL_CONTENT);
            }
            AppStreamResult::Redirect(_) => {
                unreachable!("expected stream response")
            }
            AppStreamResult::AccelRedirect(_) => {
                unreachable!("expected stream response")
            }
        }
    }

    #[tokio::test]
    async fn stream_returns_not_found_when_primary_and_fallback_missing() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = dir.path().join("missing.mp4");
        let fallback_path = dir.path().join("missing-fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let err = LocalStreamer::stream(
            state,
            primary_path,
            Some("bytes=0-".to_string()),
            ClientInfo::new(
                Some("client-1".to_string()),
                Some("play-123-1".to_string()),
                None,
                None,
            ),
            "test-node",
        )
        .await;

        match err {
            Ok(_) => unreachable!("missing primary and fallback should fail"),
            Err(status) => assert_eq!(status, hyper::StatusCode::NOT_FOUND),
        }
    }

    #[tokio::test]
    async fn stream_rejects_malformed_range() {
        let dir = match TempDir::new() {
            Ok(dir) => dir,
            Err(err) => panic!("temp dir failed: {err}"),
        };
        let primary_path = write_test_file(&dir, "primary.mp4");
        let fallback_path = write_test_file(&dir, "fallback.mp4");
        let state = std::sync::Arc::new(
            test_state_with_fallback(Some(&fallback_path)).await,
        );

        let err = LocalStreamer::stream(
            state,
            primary_path,
            Some("bytes=bad".to_string()),
            ClientInfo::new(
                Some("client-1".to_string()),
                Some("play-123-1".to_string()),
                None,
                None,
            ),
            "test-node",
        )
        .await;

        match err {
            Ok(_) => unreachable!("malformed range should fail"),
            Err(status) => assert_eq!(status, hyper::StatusCode::BAD_REQUEST),
        }
    }
}