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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! API client for managing data through Algorithmia
//!
//! Instantiate from the [`Algorithmia`](../struct.Algorithmia.html) struct

pub use self::dir::*;
pub use self::file::*;
pub use self::object::*;
pub use self::path::*;

use crate::error::*;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use headers_ext::{ContentLength, Date, HeaderMapExt};
use http::header::HeaderMap;
use std::ops::Deref;
use std::time::{SystemTime, UNIX_EPOCH};

mod dir;
mod file;
mod object;
mod path;

static DATA_BASE_PATH: &'static str = "v1/connector";

mod header {
    use http::header::HeaderValue;
    pub const X_DATA_TYPE: &'static str = "x-data-type";
    pub const X_ERROR_MESSAGE: &'static str = "x-error-message";
    pub(crate) fn lossy_header(val: &HeaderValue) -> String {
        String::from_utf8_lossy(val.as_bytes()).to_string()
    }
}
use self::header::{lossy_header, X_DATA_TYPE, X_ERROR_MESSAGE};

/// Minimal representation of data type
pub enum DataType {
    File,
    Dir,
}

/// Data type wrapping the data item (including any metadata)
pub enum DataItem {
    File(DataFileItem),
    Dir(DataDirItem),
}

/// `DataFile` wrapper with metadata
pub struct DataFileItem {
    /// Size of file in bytes
    pub size: u64,
    /// Last modified timestamp
    pub last_modified: DateTime<Utc>,
    file: DataFile,
}

impl Deref for DataFileItem {
    type Target = DataFile;
    fn deref(&self) -> &DataFile {
        &self.file
    }
}

/// `DataDir` wrapper (currently no metadata)
pub struct DataDirItem {
    dir: DataDir,
}

impl Deref for DataDirItem {
    type Target = DataDir;
    fn deref(&self) -> &DataDir {
        &self.dir
    }
}

struct HeaderData {
    pub data_type: DataType,
    pub content_length: Option<u64>,
    pub last_modified: Option<DateTime<Utc>>,
}

fn parse_headers(headers: &HeaderMap) -> Result<HeaderData> {
    if let Some(err_header) = headers.get(X_ERROR_MESSAGE).map(lossy_header) {
        return Err(ErrorKind::Api(ApiError::from(err_header)).into());
    };

    let data_type = match &headers.get(X_DATA_TYPE).map(lossy_header) {
        Some(dt) if dt == "directory" => DataType::Dir,
        Some(dt) if dt == "file" => DataType::File,
        Some(dt) => return Err(ErrorKind::InvalidDataType(dt.to_string()).into()),
        None => return Err(ErrorKind::MissingDataType.into()),
    };

    let content_length = headers.typed_get::<ContentLength>().map(|c| c.0);
    let last_modified = headers.typed_get::<Date>().map(|d| {
        let time = SystemTime::from(d);
        let ts = time
            .duration_since(UNIX_EPOCH)
            .expect("date header predates unix epoch");
        let naive_datetime =
            NaiveDateTime::from_timestamp(ts.as_secs() as i64, ts.subsec_nanos() as u32);
        Utc.from_utc_datetime(&naive_datetime)
    });

    Ok(HeaderData {
        data_type: data_type,
        content_length: content_length,
        last_modified: last_modified,
    })
}

fn parse_data_uri(data_uri: &str) -> String {
    match data_uri {
        p if p.contains("://") => p.split_terminator("://").collect::<Vec<_>>().join("/"),
        p if p.starts_with('/') => format!("data/{}", &p[1..]),
        p => format!("data/{}", p),
    }
}

#[cfg(test)]
mod tests {
    use super::parse_data_uri;

    #[test]
    fn test_parse_protocol() {
        assert_eq!(parse_data_uri("data://"), "data");
        assert_eq!(parse_data_uri("data://foo"), "data/foo");
        assert_eq!(parse_data_uri("data://foo/"), "data/foo/");
        assert_eq!(parse_data_uri("data://foo/bar"), "data/foo/bar");
        assert_eq!(parse_data_uri("dropbox://"), "dropbox");
        assert_eq!(parse_data_uri("dropbox://foo"), "dropbox/foo");
        assert_eq!(parse_data_uri("dropbox://foo/"), "dropbox/foo/");
        assert_eq!(parse_data_uri("dropbox://foo/bar"), "dropbox/foo/bar");
    }

    #[test]
    fn test_parse_leading_slash() {
        assert_eq!(parse_data_uri("/foo"), "data/foo");
        assert_eq!(parse_data_uri("/foo/"), "data/foo/");
        assert_eq!(parse_data_uri("/foo/bar"), "data/foo/bar");
    }

    #[test]
    fn test_parse_unprefixed() {
        assert_eq!(parse_data_uri("foo"), "data/foo");
        assert_eq!(parse_data_uri("foo/"), "data/foo/");
        assert_eq!(parse_data_uri("foo/bar"), "data/foo/bar");
    }
}