glitch_in_the_matrix/
media.rs

1//! Media repository management.
2
3use futures::{self, Future};
4use crate::request::{self, MatrixRequest, MatrixRequestable};
5use http::Method;
6use std::collections::HashMap;
7use http::header::{HeaderValue, CONTENT_TYPE};
8use types::replies::UploadReply;
9use futures::future::Either;
10use crate::errors::MatrixError;
11
12/// Contains media repository endpoints.
13pub struct Media;
14
15impl Media {
16    /// Upload some data (convertible to a `Body`) of a given `ContentType`, like an image.
17    pub fn upload<T: Into<Vec<u8>>, R: MatrixRequestable>(rq: &mut R, data: T, content_type: &str) -> impl Future<Item = UploadReply, Error = MatrixError> {
18        let req = MatrixRequest {
19            meth: Method::POST,
20            endpoint: "/upload".into(),
21            params: HashMap::new(),
22            body: (),
23            typ: request::apis::r0::MediaApi
24        }.make_request(rq);
25        let mut req = match req {
26            Ok(r) => r,
27            Err(e) => return Either::B(futures::future::err(e.into()))
28        };
29        *req.body_mut() = data.into();
30        let hv = match HeaderValue::from_str(content_type) {
31            Ok(r) => r,
32            Err(e) => return Either::B(futures::future::err(e.into()))
33        };
34        req.headers_mut().insert(CONTENT_TYPE, hv);
35        Either::A(rq.typed_api_call(req, false))
36    }
37
38}