Skip to main content

matrix_sdk_contentscanner/api/scan/
mod.rs

1// Copyright 2026 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use matrix_sdk::RumaApiError;
16use ruma::{
17    api::{EndpointError, IncomingResponse, error::FromHttpResponseError},
18    exports::{http::Response, serde_json},
19};
20use serde::Deserialize;
21
22pub mod encrypted;
23pub mod unencrypted;
24
25/// A media scan response containing the result of the scan.
26/// Spec: <https://github.com/element-hq/matrix-content-scanner-python/blob/main/docs/api.md#get-_matrixmedia_proxyunstablescanservernamemediaid>
27#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
28#[derive(Debug, Deserialize)]
29pub struct MediaScanResponse {
30    /// Whether the media is clean or contained something dangerous.
31    pub clean: bool,
32    /// Extra information about the scan.
33    pub info: String,
34}
35
36impl IncomingResponse for MediaScanResponse {
37    type EndpointError = RumaApiError;
38
39    fn try_from_http_response<T: AsRef<[u8]>>(
40        response: Response<T>,
41    ) -> Result<Self, FromHttpResponseError<Self::EndpointError>> {
42        if response.status().is_success() {
43            let content = response.body().as_ref().to_vec();
44            let media_scan_response: MediaScanResponse = serde_json::from_slice(&content)?;
45            Ok(media_scan_response)
46        } else {
47            Err(FromHttpResponseError::Server(Self::EndpointError::from_http_response(response)))
48        }
49    }
50}