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#[derive(Debug, Deserialize)]
28pub struct MediaScanResponse {
29 /// Whether the media is clean or contained something dangerous.
30 pub clean: bool,
31 /// Extra information about the scan.
32 pub info: String,
33}
34
35impl IncomingResponse for MediaScanResponse {
36 type EndpointError = RumaApiError;
37
38 fn try_from_http_response<T: AsRef<[u8]>>(
39 response: Response<T>,
40 ) -> Result<Self, FromHttpResponseError<Self::EndpointError>> {
41 if response.status().is_success() {
42 let content = response.body().as_ref().to_vec();
43 let media_scan_response: MediaScanResponse = serde_json::from_slice(&content)?;
44 Ok(media_scan_response)
45 } else {
46 Err(FromHttpResponseError::Server(Self::EndpointError::from_http_response(response)))
47 }
48 }
49}