fedimovies/src/mastodon_api/uploads.rs

59 lines
1.7 KiB
Rust
Raw Normal View History

use std::path::Path;
2023-04-25 13:49:35 +00:00
use fedimovies_utils::files::sniff_media_type;
2023-02-18 22:25:49 +00:00
use super::errors::MastodonError;
2023-04-24 15:35:32 +00:00
use crate::media::{save_file, SUPPORTED_MEDIA_TYPES};
#[derive(thiserror::Error, Debug)]
pub enum UploadError {
#[error(transparent)]
WriteError(#[from] std::io::Error),
#[error("base64 decoding error")]
Base64DecodingError(#[from] base64::DecodeError),
#[error("file is too large")]
TooLarge,
#[error("invalid media type")]
InvalidMediaType,
}
impl From<UploadError> for MastodonError {
fn from(error: UploadError) -> Self {
match error {
UploadError::WriteError(_) => MastodonError::InternalError,
2023-04-24 15:35:32 +00:00
other_error => MastodonError::ValidationError(other_error.to_string()),
}
}
}
pub fn save_b64_file(
b64data: &str,
maybe_media_type: Option<String>,
output_dir: &Path,
2023-03-14 15:17:56 +00:00
file_size_limit: usize,
maybe_expected_prefix: Option<&str>,
) -> Result<(String, usize, String), UploadError> {
let file_data = base64::decode(b64data)?;
let file_size = file_data.len();
2023-03-14 15:17:56 +00:00
if file_size > file_size_limit {
return Err(UploadError::TooLarge);
};
2023-01-06 01:14:01 +00:00
// Sniff media type if not provided
2023-04-24 15:35:32 +00:00
let media_type = maybe_media_type
.or(sniff_media_type(&file_data))
.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-04-24 15:35:32 +00:00
let file_name = save_file(file_data, output_dir, Some(&media_type))?;
Ok((file_name, file_size, media_type))
}