use std::sync::Arc;
use chrono::{DateTime, Utc};
use futures_util::StreamExt;
use reqwest::header::{CONTENT_LENGTH, CONTENT_TYPE};
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use crate::authentification::{CredentialsOrApiKey, Profile};
use crate::error::{Error, Result};
use crate::http::{client, ok};
const DIRECTORY: &str = "application/x-directory";
pub type Progress = Arc<dyn Fn(f64) + Send + Sync>;
#[derive(Clone)]
pub struct File {
pub authorization: DriveAuthorization,
pub content_type: String,
pub content_length: u64,
pub access_date: DateTime<Utc>,
pub create_date: DateTime<Utc>,
name: String,
online: bool,
data: Option<Vec<u8>>,
preview_data: Option<Option<Vec<u8>>>,
}
impl std::fmt::Debug for File {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("File")
.field("authorization", &self.authorization)
.field("name", &self.name)
.field("online", &self.online)
.field("content_type", &self.content_type)
.field("content_length", &self.content_length)
.finish_non_exhaustive()
}
}
fn authorization_header(authorization: &CredentialsOrApiKey) -> String {
match authorization {
CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
}
}
fn status_error(status: u16) -> Error {
match status {
401 => Error::Message(format!("{status}: Unauthorized request.")),
403 => Error::Message(format!("{status}: Forbidden request.")),
_ => Error::Message(format!("{status}: Bad request.")),
}
}
impl File {
#[allow(clippy::too_many_arguments)]
fn new(
authorization: DriveAuthorization,
name: String,
online: bool,
content_type: String,
content_length: u64,
access_date: DateTime<Utc>,
create_date: DateTime<Utc>,
data: Option<Vec<u8>>,
) -> Self {
File {
authorization,
name,
online,
content_type,
content_length,
access_date,
create_date,
data,
preview_data: None,
}
}
pub fn from_json(json: &Value, authorization: DriveAuthorization) -> File {
let now = Utc::now();
let date = |key: &str| crate::tickets::parse_date(&json[key]).unwrap_or(now);
let content_length = match &json["contentLength"] {
Value::String(length) => length.parse().unwrap_or(0),
length => length.as_f64().unwrap_or(0.0) as u64,
};
File::new(
authorization,
json["name"].as_str().unwrap_or_default().to_string(),
true,
json["contentType"]
.as_str()
.unwrap_or("application/octet-stream")
.to_string(),
content_length,
date("accessDate"),
date("createDate"),
None,
)
}
pub fn name(&self) -> &str {
&self.name
}
pub fn online(&self) -> bool {
self.online
}
fn is_directory(&self) -> bool {
self.content_type == DIRECTORY
}
fn file_request(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
match &self.authorization {
DriveAuthorization::Path {
authorization,
path,
group,
} => {
let request = request
.header("Authorization", authorization_header(authorization))
.header(
"Path",
if self.is_directory() {
format!("{path}/")
} else {
path.clone()
},
);
match group {
Some(group) => request.header("Group", group),
None => request,
}
}
DriveAuthorization::AccessCode {
authorization,
access_code,
} => request
.header("Authorization", authorization_header(authorization))
.header("Access-Code", access_code),
DriveAuthorization::Repository {
authorization,
group,
..
} => repository_request(request, authorization, group),
}
}
fn get(&self, route: &str, query: &str) -> reqwest::RequestBuilder {
let url = match &self.authorization {
DriveAuthorization::Repository {
repository, path, ..
} => format!("{}{query}", repository_url(*repository, path)),
_ => format!("{}{route}", crate::get_url()),
};
self.file_request(client().get(url))
}
pub async fn download(
&mut self,
revalidate: bool,
on_progress: Option<Progress>,
) -> Result<Vec<u8>> {
if let Some(data) = &self.data
&& !revalidate
{
return Ok(data.clone());
}
let response = self.get("/file/download", "?download=1").send().await?;
if !response.status().is_success() {
return Err(status_error(response.status().as_u16()));
}
let content_length = self.content_length;
let on_loaded = on_progress.clone().map(|progress| {
move |loaded: u64| {
if content_length > 0 {
progress((loaded as f64 / content_length as f64).clamp(0.0, 1.0));
}
}
});
let buffer = parse_stream(response, on_loaded).await?;
if let Some(progress) = on_progress {
progress(1.0);
}
let data = buffer.into_iter().next().unwrap_or_default();
self.data = Some(data.clone());
Ok(data)
}
pub async fn preview(&mut self, revalidate: bool) -> Result<Option<Vec<u8>>> {
if let Some(preview) = &self.preview_data
&& !revalidate
{
return Ok(preview.clone());
}
let response = self.get("/file/preview", "?preview=1").send().await?;
if response.status().as_u16() == 204 {
self.preview_data = Some(None);
return Ok(None);
}
if !response.status().is_success() {
let status = response.status().as_u16();
if status == 401 || status == 403 {
return Err(status_error(status));
}
self.preview_data = Some(None);
return Ok(None);
}
let buffer = parse_stream(response, None::<fn(u64)>).await?;
let data = buffer.into_iter().next().unwrap_or_default();
self.preview_data = Some(Some(data.clone()));
Ok(Some(data))
}
pub async fn upload(&mut self, on_progress: Option<Progress>) -> Result<()> {
let request = match &self.authorization {
DriveAuthorization::AccessCode { .. } => {
return Err(Error::Message("401: Unauthorized request.".to_string()));
}
DriveAuthorization::Path {
authorization,
path,
group,
} => {
let request = client()
.post(format!("{}/file/upload", crate::get_url()))
.header("Authorization", authorization_header(authorization))
.header(
"Path",
if self.is_directory() {
format!("{path}/")
} else {
path.clone()
},
);
match group {
Some(group) => request.header("Group", group),
None => request,
}
}
DriveAuthorization::Repository {
authorization,
repository,
path,
group,
} => repository_request(
client().post(repository_url(*repository, path)),
authorization,
group,
),
};
let body: Vec<u8> = if self.is_directory() {
b"Folder".to_vec()
} else {
match &self.data {
Some(data) => data.clone(),
None => return Err(Error::Message("400: No data to upload.".to_string())),
}
};
let total = body.len();
let content_type = if self.content_type.is_empty() {
"application/octet-stream".to_string()
} else {
self.content_type.clone()
};
let response = request
.header(CONTENT_TYPE, content_type)
.header(CONTENT_LENGTH, total)
.body(crate::http::progress_body(body, on_progress.clone()))
.send()
.await
.map_err(|_| Error::Message("Failed to upload file.".to_string()))?;
if let Some(progress) = on_progress {
progress(1.0);
}
if response.status().is_success() {
self.online = true;
return Ok(());
}
Err(status_error(response.status().as_u16()))
}
pub async fn copy(&self, path: &str) -> Result<()> {
if matches!(self.authorization, DriveAuthorization::AccessCode { .. }) {
return Err(Error::Message("401: Unauthorized request.".to_string()));
}
if matches!(self.authorization, DriveAuthorization::Repository { .. }) {
return Err(Error::Message(
"403: Not available for repository files.".to_string(),
));
}
let response = self
.file_request(client().post(format!("{}/file/copy", crate::get_url())))
.json(&json!({ "copyPath": path }))
.send()
.await?;
ok(response).await?;
Ok(())
}
pub async fn text(&mut self) -> Result<String> {
Ok(String::from_utf8_lossy(&self.download(false, None).await?).into_owned())
}
pub async fn json<T: DeserializeOwned>(&mut self) -> Result<T> {
Ok(serde_json::from_str(&self.text().await?)?)
}
pub async fn remove(&mut self) -> Result<()> {
let (authorization, path, group) = match &self.authorization {
DriveAuthorization::Path {
authorization,
path,
group,
} => (authorization, path, group),
DriveAuthorization::AccessCode { .. } => {
return Err(Error::Message("401: Unauthorized request.".to_string()));
}
DriveAuthorization::Repository { .. } => {
return Err(Error::Message(
"403: Not available for repository files.".to_string(),
));
}
};
let path = if self.is_directory() {
format!("{path}/")
} else {
path.clone()
};
remove(authorization, &path, group.as_deref()).await?;
self.data = None;
self.online = false;
Ok(())
}
pub async fn generate_access_code(&self, expiration: Option<u64>) -> Result<String> {
if matches!(self.authorization, DriveAuthorization::AccessCode { .. }) {
return Err(Error::Message("401: Unauthorized request.".to_string()));
}
if matches!(self.authorization, DriveAuthorization::Repository { .. }) {
return Err(Error::Message(
"403: Not available for repository files.".to_string(),
));
}
let response = self
.file_request(client().post(format!("{}/file/access-code", crate::get_url())))
.json(&json!({ "expiration": expiration }))
.send()
.await?;
Ok(ok(response).await?.text().await?)
}
pub async fn owner(&self) -> Result<Profile> {
if matches!(self.authorization, DriveAuthorization::Repository { .. }) {
return Err(Error::Message(
"403: Not available for repository files.".to_string(),
));
}
let response = correct_header(
&self.authorization,
client().get(format!("{}/file/owner", crate::get_url())),
)
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
}
#[derive(Debug, Clone)]
pub enum DriveAuthorization {
Path {
authorization: CredentialsOrApiKey,
path: String,
group: Option<String>,
},
AccessCode {
authorization: CredentialsOrApiKey,
access_code: String,
},
Repository {
authorization: CredentialsOrApiKey,
repository: u64,
path: String,
group: Option<String>,
},
}
fn repository_url(repository: u64, path: &str) -> String {
let tail: String = path
.split('/')
.filter(|segment| !segment.is_empty())
.map(|segment| format!("/{}", crate::http::encode(segment)))
.collect();
format!(
"{}/termite/repositories/{repository}/files{tail}",
crate::termite::get_url()
)
}
fn repository_request(
request: reqwest::RequestBuilder,
authorization: &CredentialsOrApiKey,
group: &Option<String>,
) -> reqwest::RequestBuilder {
let request = request.header("Authorization", authorization_header(authorization));
match group {
Some(group) => request.header("Group", group),
None => request,
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct FileDates {
pub access_date: Option<DateTime<Utc>>,
pub create_date: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Default)]
pub struct FileMeta {
pub name: String,
pub content_type: String,
pub content_length: Option<u64>,
pub access_date: Option<DateTime<Utc>>,
pub create_date: Option<DateTime<Utc>>,
}
fn correct_header(
authorization: &DriveAuthorization,
request: reqwest::RequestBuilder,
) -> reqwest::RequestBuilder {
match authorization {
DriveAuthorization::Path {
authorization,
path,
group,
} => {
let request = request
.header("Authorization", authorization_header(authorization))
.header("Path", path);
match group {
Some(group) => request.header("Group", group),
None => request,
}
}
DriveAuthorization::AccessCode {
authorization,
access_code,
} => request
.header("Authorization", authorization_header(authorization))
.header("Access-Code", access_code),
DriveAuthorization::Repository {
authorization,
group,
..
} => repository_request(request, authorization, group),
}
}
pub async fn list(
authorization: &CredentialsOrApiKey,
path: &str,
group: Option<&str>,
) -> Result<Vec<String>> {
let request = client()
.get(format!("{}/file/list", crate::get_url()))
.header("Authorization", authorization_header(authorization))
.header("Path", path);
let request = match group {
Some(group) => request.header("Group", group),
None => request,
};
let response = request.send().await?;
Ok(ok(response).await?.json().await?)
}
pub async fn wrap(
authorization: DriveAuthorization,
name: &str,
content_type: &str,
data: Vec<u8>,
options: FileDates,
) -> Result<File> {
let now = Utc::now();
let content_type = if content_type.is_empty() {
mime_guess::from_path(name)
.first()
.map(|mime| mime.essence_str().to_string())
.unwrap_or_else(|| "application/octet-stream".to_string())
} else {
content_type.to_string()
};
Ok(File::new(
authorization,
name.to_string(),
false,
content_type,
data.len() as u64,
options.access_date.unwrap_or(now),
options.create_date.unwrap_or(now),
Some(data),
))
}
pub fn from_bytes(
authorization: CredentialsOrApiKey,
path: &str,
content_type: &str,
data: Vec<u8>,
options: FileDates,
) -> File {
let now = Utc::now();
let name = path
.rsplit('/')
.next()
.filter(|name| !name.is_empty())
.unwrap_or(path);
File::new(
DriveAuthorization::Path {
authorization,
path: path.to_string(),
group: None,
},
name.to_string(),
false,
content_type.to_string(),
data.len() as u64,
options.access_date.unwrap_or(now),
options.create_date.unwrap_or(now),
Some(data),
)
}
pub fn from_access_code(
authorization: CredentialsOrApiKey,
access_code: &str,
meta: FileMeta,
) -> File {
let now = Utc::now();
File::new(
DriveAuthorization::AccessCode {
authorization,
access_code: access_code.to_string(),
},
meta.name,
true,
meta.content_type,
meta.content_length.unwrap_or(0),
meta.access_date.unwrap_or(now),
meta.create_date.unwrap_or(now),
None,
)
}
pub async fn folder(authorization: DriveAuthorization, options: FileDates) -> Result<File> {
let path = match &authorization {
DriveAuthorization::Path { path, .. } | DriveAuthorization::Repository { path, .. } => path,
DriveAuthorization::AccessCode { .. } => {
return Err(Error::Message("401: Unauthorized request.".to_string()));
}
};
let normalized = path.replace('\\', "/");
let normalized = normalized.trim_end_matches('/');
let name = normalized
.rsplit('/')
.next()
.unwrap_or_default()
.to_string();
let now = Utc::now();
Ok(File::new(
authorization,
name,
false,
DIRECTORY.to_string(),
0,
options.access_date.unwrap_or(now),
options.create_date.unwrap_or(now),
None,
))
}
pub async fn pull(authorization: DriveAuthorization) -> Result<File> {
let url = match &authorization {
DriveAuthorization::Repository {
repository, path, ..
} => repository_url(*repository, path),
_ => format!("{}/file/pull", crate::get_url()),
};
let response = correct_header(&authorization, client().get(url))
.send()
.await?;
let json: Value = ok(response).await?.json().await?;
Ok(File::from_json(&json, authorization))
}
pub async fn remove(
authorization: &CredentialsOrApiKey,
path: &str,
group: Option<&str>,
) -> Result<bool> {
let request = client()
.delete(format!("{}/file", crate::get_url()))
.header("Authorization", authorization_header(authorization))
.header("Path", path);
let request = match group {
Some(group) => request.header("Group", group),
None => request,
};
let response = request.send().await?;
ok(response).await?;
Ok(true)
}
pub async fn parse_stream(
response: reqwest::Response,
on_loaded: Option<impl Fn(u64)>,
) -> Result<Vec<Vec<u8>>> {
let encoding_size = response
.headers()
.get("Encoding-Size")
.and_then(|value| value.to_str().ok())
.and_then(|value| value.trim().parse::<i64>().ok());
let mut buffer = Vec::new();
let mut loaded = 0u64;
let mut body = response.bytes_stream();
while let Some(chunk) = body.next().await {
let chunk = chunk?;
loaded += chunk.len() as u64;
buffer.extend_from_slice(&chunk);
if let Some(on_loaded) = &on_loaded {
on_loaded(loaded);
}
}
if encoding_size.is_none_or(|size| size <= 1) {
return Ok(vec![buffer]);
}
const BOUNDARY: &[u8] = b"--BOUNDARY";
let mut parts = Vec::new();
let mut current = 0;
while let Some(offset) = buffer[current..]
.windows(BOUNDARY.len())
.position(|window| window == BOUNDARY)
{
let index = current + offset;
if index > current {
parts.push(buffer[current..index].to_vec());
}
current = index + BOUNDARY.len();
}
if current < buffer.len() {
parts.push(buffer[current..].to_vec());
}
Ok(parts)
}
#[cfg(test)]
mod tests {
use super::parse_stream;
fn response(encoding_size: Option<&str>, body: &'static [u8]) -> reqwest::Response {
let mut builder = http::Response::builder();
if let Some(size) = encoding_size {
builder = builder.header("Encoding-Size", size);
}
reqwest::Response::from(builder.body(body).unwrap())
}
#[tokio::test]
async fn single_part_bodies_come_back_whole() {
let parts = parse_stream(response(None, b"hello"), None::<fn(u64)>)
.await
.unwrap();
assert_eq!(parts, vec![b"hello".to_vec()]);
}
#[tokio::test]
async fn multi_part_bodies_split_on_the_boundary() {
let loaded = std::sync::Mutex::new(0);
let parts = parse_stream(
response(Some("2"), b"--BOUNDARYfirst--BOUNDARYsecond"),
Some(|bytes| *loaded.lock().unwrap() = bytes),
)
.await
.unwrap();
assert_eq!(parts, vec![b"first".to_vec(), b"second".to_vec()]);
assert_eq!(*loaded.lock().unwrap(), 31);
}
}