#![allow(clippy::await_holding_refcell_ref)]
#![allow(clippy::collapsible_else_if)]
#![warn(anonymous_parameters, bad_style, missing_docs)]
#![warn(unused, unused_extern_crates, unused_import_braces, unused_qualifications)]
#![warn(unsafe_code)]
use async_trait::async_trait;
use endbasic_std::storage::DiskSpace;
use serde::{Deserialize, Serialize};
use std::io;
mod cloud;
pub use cloud::CloudService;
mod cmds;
pub use cmds::add_all;
mod drive;
pub(crate) use drive::CloudDriveFactory;
#[cfg(test)]
pub(crate) mod testutils;
pub const PROD_API_ADDRESS: &str = "https://service.endbasic.dev/";
#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Serialize))]
struct SerdeDiskSpace {
bytes: u64,
files: u64,
}
impl From<DiskSpace> for SerdeDiskSpace {
fn from(ds: DiskSpace) -> Self {
SerdeDiskSpace { bytes: ds.bytes, files: ds.files }
}
}
impl From<SerdeDiskSpace> for DiskSpace {
fn from(sds: SerdeDiskSpace) -> Self {
DiskSpace { bytes: sds.bytes, files: sds.files }
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[cfg_attr(test, derive(Serialize))]
pub struct AccessToken(String);
impl AccessToken {
#[cfg(test)]
pub(crate) fn new<S: Into<String>>(token: S) -> Self {
Self(token.into())
}
pub(crate) fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Debug, Serialize))]
pub struct ErrorResponse {
pub(crate) message: String,
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Debug, Serialize))]
pub struct LoginResponse {
pub(crate) access_token: AccessToken,
motd: Vec<String>,
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Debug, Serialize))]
pub struct DirectoryEntry {
filename: String,
mtime: u64,
length: u64,
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Debug, Serialize))]
pub struct GetFilesResponse {
files: Vec<DirectoryEntry>,
disk_quota: Option<SerdeDiskSpace>,
disk_free: Option<SerdeDiskSpace>,
}
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
#[cfg_attr(test, derive(Deserialize))]
pub struct GetFileRequest {
get_content: bool,
get_readers: bool,
}
impl GetFileRequest {
fn with_get_content(mut self) -> Self {
self.get_content = true;
self
}
fn with_get_readers(mut self) -> Self {
self.get_readers = true;
self
}
}
#[derive(Default, Deserialize)]
#[cfg_attr(test, derive(Debug, PartialEq, Serialize))]
pub struct GetFileResponse {
content: Option<String>,
readers: Option<Vec<String>>,
}
impl GetFileResponse {
fn decoded_content(&self) -> io::Result<Option<Vec<u8>>> {
match self.content.as_ref() {
Some(content) => match base64::decode(content) {
Ok(content) => Ok(Some(content)),
Err(e) => Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("File content is not properly base64-encoded: {}", e),
)),
},
None => Ok(None),
}
}
}
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
#[cfg_attr(test, derive(Deserialize))]
pub struct PatchFileRequest {
content: Option<String>,
add_readers: Option<Vec<String>>,
remove_readers: Option<Vec<String>>,
}
impl PatchFileRequest {
fn with_content<C: AsRef<[u8]>>(mut self, content: C) -> Self {
self.content = Some(base64::encode(content));
self
}
#[cfg(test)]
fn with_add_readers<R: Into<Vec<String>>>(mut self, readers: R) -> Self {
self.add_readers = Some(readers.into());
self
}
#[cfg(test)]
fn with_remove_readers<R: Into<Vec<String>>>(mut self, readers: R) -> Self {
self.remove_readers = Some(readers.into());
self
}
}
#[derive(Debug, Default, Eq, PartialEq, Serialize)]
#[cfg_attr(test, derive(Deserialize))]
pub struct SignupRequest {
username: String,
password: String,
email: String,
promotional_email: bool,
}
#[async_trait(?Send)]
pub trait Service {
async fn signup(&mut self, request: &SignupRequest) -> io::Result<()>;
async fn login(&mut self, username: &str, password: &str) -> io::Result<LoginResponse>;
async fn logout(&mut self) -> io::Result<()>;
fn is_logged_in(&self) -> bool;
fn logged_in_username(&self) -> Option<String>;
async fn get_files(&mut self, username: &str) -> io::Result<GetFilesResponse>;
async fn get_file(
&mut self,
username: &str,
filename: &str,
request: &GetFileRequest,
) -> io::Result<GetFileResponse>;
async fn patch_file(
&mut self,
username: &str,
filename: &str,
request: &PatchFileRequest,
) -> io::Result<()>;
async fn delete_file(&mut self, username: &str, filename: &str) -> io::Result<()>;
}