matrix_sdk/encryption/
futures.rs

1// Copyright 2023 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
15//! Named futures returned from methods on types in
16//! [the `encryption` module][super].
17
18#![deny(unreachable_pub)]
19
20use std::{future::IntoFuture, io::Read};
21
22use eyeball::SharedObservable;
23#[cfg(not(target_arch = "wasm32"))]
24use eyeball::Subscriber;
25use matrix_sdk_common::boxed_into_future;
26use ruma::events::room::{EncryptedFile, EncryptedFileInit};
27
28use crate::{config::RequestConfig, Client, Media, Result, TransmissionProgress};
29
30/// Future returned by [`Client::upload_encrypted_file`].
31#[allow(missing_debug_implementations)]
32pub struct UploadEncryptedFile<'a, R: ?Sized> {
33    client: &'a Client,
34    content_type: &'a mime::Mime,
35    reader: &'a mut R,
36    send_progress: SharedObservable<TransmissionProgress>,
37    request_config: Option<RequestConfig>,
38}
39
40impl<'a, R: ?Sized> UploadEncryptedFile<'a, R> {
41    pub(crate) fn new(client: &'a Client, content_type: &'a mime::Mime, reader: &'a mut R) -> Self {
42        Self {
43            client,
44            content_type,
45            reader,
46            send_progress: Default::default(),
47            request_config: None,
48        }
49    }
50
51    /// Replace the default `SharedObservable` used for tracking upload
52    /// progress.
53    ///
54    /// Note that any subscribers obtained from
55    /// [`subscribe_to_send_progress`][Self::subscribe_to_send_progress]
56    /// will be invalidated by this.
57    pub fn with_send_progress_observable(
58        mut self,
59        send_progress: SharedObservable<TransmissionProgress>,
60    ) -> Self {
61        self.send_progress = send_progress;
62        self
63    }
64
65    /// Replace the default request config used for the upload request.
66    ///
67    /// The timeout value will be overridden with a reasonable default, based on
68    /// the size of the encrypted payload.
69    pub fn with_request_config(mut self, request_config: RequestConfig) -> Self {
70        self.request_config = Some(request_config);
71        self
72    }
73
74    /// Get a subscriber to observe the progress of sending the request
75    /// body.
76    #[cfg(not(target_arch = "wasm32"))]
77    pub fn subscribe_to_send_progress(&self) -> Subscriber<TransmissionProgress> {
78        self.send_progress.subscribe()
79    }
80}
81
82impl<'a, R> IntoFuture for UploadEncryptedFile<'a, R>
83where
84    R: Read + Send + ?Sized + 'a,
85{
86    type Output = Result<EncryptedFile>;
87    boxed_into_future!(extra_bounds: 'a);
88
89    fn into_future(self) -> Self::IntoFuture {
90        let Self { client, content_type, reader, send_progress, request_config } = self;
91        Box::pin(async move {
92            let mut encryptor = matrix_sdk_base::crypto::AttachmentEncryptor::new(reader);
93
94            let mut buf = Vec::new();
95            encryptor.read_to_end(&mut buf)?;
96
97            // Override the reasonable upload timeout value, based on the size of the
98            // encrypted payload.
99            let request_config =
100                request_config.map(|config| config.timeout(Media::reasonable_upload_timeout(&buf)));
101
102            let response = client
103                .media()
104                .upload(content_type, buf, request_config)
105                .with_send_progress_observable(send_progress)
106                .await?;
107
108            let file: EncryptedFile = {
109                let keys = encryptor.finish();
110                EncryptedFileInit {
111                    url: response.content_uri,
112                    key: keys.key,
113                    iv: keys.iv,
114                    hashes: keys.hashes,
115                    v: keys.version,
116                }
117                .into()
118            };
119
120            Ok(file)
121        })
122    }
123}