2022-08-18 21:01:35 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2023-02-18 22:25:49 +00:00
|
|
|
use mitra_utils::files::sniff_media_type;
|
|
|
|
|
2023-02-12 18:54:29 +00:00
|
|
|
use crate::media::{save_file, SUPPORTED_MEDIA_TYPES};
|
2023-02-25 22:27:07 +00:00
|
|
|
use super::errors::MastodonError;
|
2022-08-18 21:01:35 +00:00
|
|
|
|
2022-08-18 20:15:40 +00:00
|
|
|
pub const UPLOAD_MAX_SIZE: usize = 1024 * 1024 * 5;
|
|
|
|
|
2022-08-18 21:01:35 +00:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum UploadError {
|
|
|
|
#[error(transparent)]
|
|
|
|
WriteError(#[from] std::io::Error),
|
|
|
|
|
|
|
|
#[error("base64 decoding error")]
|
|
|
|
Base64DecodingError(#[from] base64::DecodeError),
|
|
|
|
|
2022-08-18 20:15:40 +00:00
|
|
|
#[error("file is too large")]
|
|
|
|
TooLarge,
|
|
|
|
|
2022-08-18 21:01:35 +00:00
|
|
|
#[error("invalid media type")]
|
|
|
|
InvalidMediaType,
|
|
|
|
}
|
|
|
|
|
2023-02-25 22:27:07 +00:00
|
|
|
impl From<UploadError> for MastodonError {
|
2022-08-18 20:15:40 +00:00
|
|
|
fn from(error: UploadError) -> Self {
|
|
|
|
match error {
|
2023-02-25 22:27:07 +00:00
|
|
|
UploadError::WriteError(_) => MastodonError::InternalError,
|
2022-08-18 20:15:40 +00:00
|
|
|
other_error => {
|
2023-02-25 22:27:07 +00:00
|
|
|
MastodonError::ValidationError(other_error.to_string())
|
2022-08-18 20:15:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 21:01:35 +00:00
|
|
|
pub fn save_b64_file(
|
|
|
|
b64data: &str,
|
2023-01-06 01:50:30 +00:00
|
|
|
maybe_media_type: Option<String>,
|
2022-08-18 21:01:35 +00:00
|
|
|
output_dir: &Path,
|
2023-01-06 01:50:30 +00:00
|
|
|
maybe_expected_prefix: Option<&str>,
|
2023-01-19 21:57:29 +00:00
|
|
|
) -> Result<(String, usize, String), UploadError> {
|
|
|
|
let file_data = base64::decode(b64data)?;
|
|
|
|
let file_size = file_data.len();
|
|
|
|
if file_size > UPLOAD_MAX_SIZE {
|
2022-08-18 20:15:40 +00:00
|
|
|
return Err(UploadError::TooLarge);
|
|
|
|
};
|
2023-01-06 01:14:01 +00:00
|
|
|
// Sniff media type if not provided
|
2023-01-19 21:57:29 +00:00
|
|
|
let media_type = maybe_media_type.or(sniff_media_type(&file_data))
|
2023-01-06 01:50:30 +00:00
|
|
|
.ok_or(UploadError::InvalidMediaType)?;
|
|
|
|
if !SUPPORTED_MEDIA_TYPES.contains(&media_type.as_str()) {
|
|
|
|
return Err(UploadError::InvalidMediaType);
|
|
|
|
};
|
|
|
|
if let Some(expected_prefix) = maybe_expected_prefix {
|
|
|
|
if !media_type.starts_with(expected_prefix) {
|
|
|
|
return Err(UploadError::InvalidMediaType);
|
|
|
|
};
|
2022-12-22 21:42:14 +00:00
|
|
|
};
|
2023-01-06 01:14:01 +00:00
|
|
|
let file_name = save_file(
|
2023-01-19 21:57:29 +00:00
|
|
|
file_data,
|
2023-01-06 01:14:01 +00:00
|
|
|
output_dir,
|
2023-01-06 01:50:30 +00:00
|
|
|
Some(&media_type),
|
2023-01-06 01:14:01 +00:00
|
|
|
)?;
|
2023-01-19 21:57:29 +00:00
|
|
|
Ok((file_name, file_size, media_type))
|
2022-08-18 21:01:35 +00:00
|
|
|
}
|