use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct UploadableFile {
pub data: Vec<u8>,
pub mime_type: String,
}
impl UploadableFile {
pub fn new(data: Vec<u8>, mime_type: impl Into<String>) -> Self {
Self {
data,
mime_type: mime_type.into(),
}
}
pub fn size(&self) -> usize {
self.data.len()
}
}
#[derive(Debug, Clone)]
pub struct DownloadResult {
pub data: Vec<u8>,
pub mime_type: String,
}
impl DownloadResult {
pub fn new(data: Vec<u8>, mime_type: impl Into<String>) -> Self {
Self {
data,
mime_type: mime_type.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadFileResult {
#[serde(rename = "uhrpUrl")]
pub uhrp_url: String,
pub published: bool,
}
impl UploadFileResult {
pub fn new(uhrp_url: impl Into<String>, published: bool) -> Self {
Self {
uhrp_url: uhrp_url.into(),
published,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FindFileData {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub size: String,
#[serde(rename = "mimeType")]
pub mime_type: String,
#[serde(rename = "expiryTime")]
pub expiry_time: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadMetadata {
#[serde(rename = "uhrpUrl")]
pub uhrp_url: String,
#[serde(rename = "expiryTime")]
pub expiry_time: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenewFileResult {
pub status: String,
#[serde(rename = "prevExpiryTime")]
pub previous_expiry: i64,
#[serde(rename = "newExpiryTime")]
pub new_expiry: i64,
pub amount: i64,
}
impl RenewFileResult {
pub fn is_success(&self) -> bool {
self.status == "success"
}
}
#[cfg(feature = "http")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct UploadInfo {
pub status: String,
#[serde(rename = "uploadURL")]
pub upload_url: String,
#[serde(rename = "requiredHeaders", default)]
pub required_headers: std::collections::HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i64>,
}
#[allow(dead_code)]
pub(crate) const STATUS_SUCCESS: &str = "success";
#[allow(dead_code)]
pub(crate) const STATUS_ERROR: &str = "error";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uploadable_file_new() {
let file = UploadableFile::new(b"content".to_vec(), "text/plain");
assert_eq!(file.data, b"content");
assert_eq!(file.mime_type, "text/plain");
assert_eq!(file.size(), 7);
}
#[test]
fn test_download_result_new() {
let result = DownloadResult::new(vec![1, 2, 3], "application/octet-stream");
assert_eq!(result.data, vec![1, 2, 3]);
assert_eq!(result.mime_type, "application/octet-stream");
}
#[test]
fn test_upload_file_result_new() {
let result = UploadFileResult::new("uhrp://abc123", true);
assert_eq!(result.uhrp_url, "uhrp://abc123");
assert!(result.published);
}
#[test]
fn test_renew_file_result_is_success() {
let success = RenewFileResult {
status: "success".to_string(),
previous_expiry: 1000,
new_expiry: 2000,
amount: 100,
};
assert!(success.is_success());
let failure = RenewFileResult {
status: "error".to_string(),
previous_expiry: 1000,
new_expiry: 1000,
amount: 0,
};
assert!(!failure.is_success());
}
}