bioscopelib/remote/
picture_remote.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use common::remote::services;
use std::fs::File;
use filepath::FilePath;
use std::io::Read;

use crate::jsonb::picture_data::PictureData;
use crate::dto::picture_response::PictureResponse;

pub fn create(
    token: String,
    picture: File,
    picture_data: PictureData,
) -> Result<PictureResponse, String> {
    let picture_data = serde_json::to_string(&picture_data).expect("Can't parse picture data");

    let mut headers = hyper::header::HeaderMap::new();
    headers.insert(
        hyper::header::HeaderName::from_static("data"),
        hyper::header::HeaderValue::from_str(picture_data.as_str()).expect("can't parse token"),
    );
    headers.insert(
        hyper::header::HeaderName::from_static("Authorization"),
        hyper::header::HeaderValue::from_str(token.as_str())
            .expect("can't parse token"),
    );

    let mut buf: Vec<u8> = vec![];
    let path = picture.path().map_err(|_| "Error".to_string())?;
    File::open(path).unwrap().read_to_end(&mut buf).map_err(|_| "Error".to_string())?;

    common::remote::post_bin(
        Some(services::BIOSCOPE_API),
        String::from("/picture"),
        headers,
        buf,
    )
}