qubit_mime/classifier/file_based_media_stream_classifier.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! File-backed media stream classifier helpers.
11
12use std::fmt::Debug;
13use std::io::Read;
14use std::path::Path;
15
16use crate::{
17 MediaStreamClassifierBackend,
18 MediaStreamType,
19 MimeResult,
20};
21
22use super::media_stream_classifier_helpers::with_temp_reader;
23
24/// Core implementation contract for classifiers that only operate on local files.
25pub trait FileBasedMediaStreamClassifier: Debug + Send + Sync {
26 /// Classifies one validated local file.
27 ///
28 /// # Parameters
29 /// - `file`: Readable local media file.
30 ///
31 /// # Returns
32 /// Media stream classification.
33 ///
34 /// # Errors
35 /// Returns [`MimeError::Io`](crate::MimeError::Io) or another [`MimeError`](crate::MimeError)
36 /// when backend classification fails.
37 fn classify_by_local_file(&self, file: &Path) -> MimeResult<MediaStreamType>;
38}
39
40impl<T> MediaStreamClassifierBackend for T
41where
42 T: FileBasedMediaStreamClassifier,
43{
44 /// Delegates local-file classification to the file-based hook.
45 fn classify_by_local_file(&self, file: &Path) -> MimeResult<MediaStreamType> {
46 FileBasedMediaStreamClassifier::classify_by_local_file(self, file)
47 }
48
49 /// Stages stream content to a temporary local file before classification.
50 fn classify_by_content(&self, reader: &mut dyn Read) -> MimeResult<MediaStreamType> {
51 with_temp_reader(reader, |path| {
52 FileBasedMediaStreamClassifier::classify_by_local_file(self, path)
53 })
54 }
55}