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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*!
# File Center Raw Response on MongoDB for Rocket Framework

This crate provides response struct used for responding raw data from the File Center on MongoDB with **Etag** cache optionally.

See `examples`.
*/

pub extern crate mongo_file_center;
extern crate percent_encoding;
extern crate rocket;
extern crate rocket_etag_if_none_match;

use std::io::Cursor;

use mongo_file_center::{FileCenter, FileItem, FileData, FileCenterError, bson::oid::ObjectId};

pub use rocket_etag_if_none_match::{EntityTag, EtagIfNoneMatch};

use rocket::response::{self, Response, Responder};
use rocket::request::Request;
use rocket::http::{Status, hyper::header::ETag};

/// The response struct used for responding raw data from the File Center on MongoDB with **Etag** cache.
#[derive(Debug)]
pub struct FileCenterRawResponse {
    etag: Option<EntityTag>,
    file: Option<(Option<String>, FileItem)>,
}

impl FileCenterRawResponse {
    /// Create a `FileCenterRawResponse` instance from the object ID.
    pub fn from_object_id<S: Into<String>>(file_center: &FileCenter, client_etag: Option<EtagIfNoneMatch>, etag: Option<EntityTag>, id: &ObjectId, file_name: Option<S>) -> Result<Option<FileCenterRawResponse>, FileCenterError> {
        let is_etag_match = if let Some(client_etag) = client_etag {
            match etag.as_ref() {
                Some(etag) => {
                    client_etag.weak_eq(etag)
                }
                None => false
            }
        } else {
            false
        };

        if is_etag_match {
            Ok(Some(FileCenterRawResponse {
                etag: None,
                file: None,
            }))
        } else {
            let file_item = file_center.get_file_item_by_id(id)?;

            match file_item {
                Some(file_item) => {
                    let file_name = file_name.map(|file_name| file_name.into());

                    Ok(Some(FileCenterRawResponse {
                        etag,
                        file: Some((file_name, file_item)),
                    }))
                }
                None => Ok(None)
            }
        }
    }

    /// Create a `FileCenterRawResponse` instance from an ID token.
    pub fn from_id_token<T: AsRef<str> + Into<String>, S: Into<String>>(file_center: &FileCenter, client_etag: Option<EtagIfNoneMatch>, id_token: T, file_name: Option<S>) -> Result<Option<FileCenterRawResponse>, FileCenterError> {
        let id = file_center.decrypt_id_token(id_token.as_ref())?;

        let etag = Self::create_etag_by_id_token(id_token);

        Self::from_object_id(file_center, client_etag, Some(etag), &id, file_name)
    }

    /// Given an **id_token**, and turned into an `EntityTag` instance.
    pub fn create_etag_by_id_token<S: Into<String>>(id_token: S) -> EntityTag {
        EntityTag::new(true, id_token.into())
    }
}

impl Responder<'static> for FileCenterRawResponse {
    fn respond_to(self, _: &Request) -> response::Result<'static> {
        let mut response = Response::build();

        match self.file {
            Some((file_name, file_item)) => {
                if let Some(etag) = self.etag {
                    response.header(ETag(etag));
                }

                if let Some(file_name) = file_name {
                    if !file_name.is_empty() {
                        response.raw_header("Content-Disposition", format!("inline; filename*=UTF-8''{}", percent_encoding::percent_encode(file_name.as_bytes(), percent_encoding::QUERY_ENCODE_SET)));
                    }
                } else {
                    let file_name = file_item.get_file_name();
                    if !file_name.is_empty() {
                        response.raw_header("Content-Disposition", format!("inline; filename*=UTF-8''{}", percent_encoding::percent_encode(file_name.as_bytes(), percent_encoding::QUERY_ENCODE_SET)));
                    }
                }

                response.raw_header("Content-Type", file_item.get_mime_type().to_string());

                let file_size = file_item.get_file_size();

                match file_item.into_file_data() {
                    FileData::Collection(v) => {
                        response.sized_body(Cursor::new(v));
                    }
                    FileData::GridFS(g) => {
                        response.raw_header("Content-Length", file_size.to_string());

                        response.streamed_body(g);
                    }
                }
            }
            None => {
                response.status(Status::NotModified);
            }
        }

        response.ok()
    }
}