2025-06-19 14:46:12 -05:00
|
|
|
use super::{
|
|
|
|
|
DeleteFileData, FileHost, FileHostPublicity, FileHostingError,
|
|
|
|
|
UploadFileData,
|
|
|
|
|
};
|
2020-07-23 22:46:33 +02:00
|
|
|
use async_trait::async_trait;
|
2022-02-05 23:08:30 -07:00
|
|
|
use bytes::Bytes;
|
2022-07-31 13:29:20 -07:00
|
|
|
use chrono::Utc;
|
2025-05-09 07:27:55 -05:00
|
|
|
use hex::ToHex;
|
2021-01-14 10:08:38 -07:00
|
|
|
use sha2::Digest;
|
2025-06-19 14:46:12 -05:00
|
|
|
use std::path::PathBuf;
|
2020-07-23 22:46:33 +02:00
|
|
|
|
2026-02-19 17:33:41 +00:00
|
|
|
use crate::env::ENV;
|
|
|
|
|
|
2023-10-11 15:55:01 -07:00
|
|
|
#[derive(Default)]
|
2020-07-23 22:46:33 +02:00
|
|
|
pub struct MockHost(());
|
|
|
|
|
|
|
|
|
|
impl MockHost {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
MockHost(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl FileHost for MockHost {
|
|
|
|
|
async fn upload_file(
|
|
|
|
|
&self,
|
|
|
|
|
content_type: &str,
|
|
|
|
|
file_name: &str,
|
2025-06-19 14:46:12 -05:00
|
|
|
file_publicity: FileHostPublicity,
|
2021-10-12 11:26:59 +08:00
|
|
|
file_bytes: Bytes,
|
2020-07-23 22:46:33 +02:00
|
|
|
) -> Result<UploadFileData, FileHostingError> {
|
2025-12-20 11:43:04 +00:00
|
|
|
let file_name = urlencoding::decode(file_name)
|
|
|
|
|
.map_err(|_| FileHostingError::InvalidFilename)?;
|
|
|
|
|
let path = get_file_path(&file_name, file_publicity);
|
2024-10-18 16:07:35 -07:00
|
|
|
std::fs::create_dir_all(
|
|
|
|
|
path.parent().ok_or(FileHostingError::InvalidFilename)?,
|
|
|
|
|
)?;
|
2025-05-09 07:27:55 -05:00
|
|
|
let content_sha1 = sha1::Sha1::digest(&file_bytes).encode_hex();
|
2022-11-17 17:09:53 +00:00
|
|
|
let content_sha512 = format!("{:x}", sha2::Sha512::digest(&file_bytes));
|
2020-07-23 22:46:33 +02:00
|
|
|
|
2022-02-05 23:08:30 -07:00
|
|
|
std::fs::write(path, &*file_bytes)?;
|
2020-07-23 22:46:33 +02:00
|
|
|
Ok(UploadFileData {
|
|
|
|
|
file_name: file_name.to_string(),
|
2025-06-19 14:46:12 -05:00
|
|
|
file_publicity,
|
2020-07-23 22:46:33 +02:00
|
|
|
content_length: file_bytes.len() as u32,
|
2021-01-14 10:08:38 -07:00
|
|
|
content_sha512,
|
2020-07-23 22:46:33 +02:00
|
|
|
content_sha1,
|
|
|
|
|
content_md5: None,
|
|
|
|
|
content_type: content_type.to_string(),
|
2022-07-31 13:29:20 -07:00
|
|
|
upload_timestamp: Utc::now().timestamp() as u64,
|
2020-07-23 22:46:33 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 14:46:12 -05:00
|
|
|
async fn get_url_for_private_file(
|
2020-07-23 22:46:33 +02:00
|
|
|
&self,
|
|
|
|
|
file_name: &str,
|
2025-06-19 14:46:12 -05:00
|
|
|
_expiry_secs: u32,
|
|
|
|
|
) -> Result<String, FileHostingError> {
|
2026-02-19 17:33:41 +00:00
|
|
|
Ok(format!("{}/private/{file_name}", ENV.CDN_URL))
|
2025-06-19 14:46:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn delete_file(
|
|
|
|
|
&self,
|
|
|
|
|
file_name: &str,
|
|
|
|
|
file_publicity: FileHostPublicity,
|
2020-07-23 22:46:33 +02:00
|
|
|
) -> Result<DeleteFileData, FileHostingError> {
|
2025-06-19 14:46:12 -05:00
|
|
|
let path = get_file_path(file_name, file_publicity);
|
2024-01-05 08:20:56 -08:00
|
|
|
if path.exists() {
|
|
|
|
|
std::fs::remove_file(path)?;
|
|
|
|
|
}
|
2020-07-23 22:46:33 +02:00
|
|
|
Ok(DeleteFileData {
|
|
|
|
|
file_name: file_name.to_string(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-19 14:46:12 -05:00
|
|
|
|
|
|
|
|
fn get_file_path(
|
|
|
|
|
file_name: &str,
|
|
|
|
|
file_publicity: FileHostPublicity,
|
|
|
|
|
) -> PathBuf {
|
2026-02-19 17:33:41 +00:00
|
|
|
let mut path = PathBuf::from(ENV.MOCK_FILE_PATH.clone());
|
2025-06-19 14:46:12 -05:00
|
|
|
|
|
|
|
|
if matches!(file_publicity, FileHostPublicity::Private) {
|
|
|
|
|
path.push("private");
|
|
|
|
|
}
|
|
|
|
|
path.push(file_name.replace("../", ""));
|
|
|
|
|
|
|
|
|
|
path
|
|
|
|
|
}
|