Make media_type property on Attachment type optional
This commit is contained in:
parent
e71bb9585a
commit
f9fca604a9
6 changed files with 27 additions and 20 deletions
|
@ -22,7 +22,7 @@ pub struct Attachment {
|
|||
#[serde(rename = "type")]
|
||||
pub attachment_type: String,
|
||||
|
||||
pub media_type: String,
|
||||
pub media_type: Option<String>,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ pub fn create_note(
|
|||
);
|
||||
let attachments: Vec<Attachment> = post.attachments.iter().map(|db_item| {
|
||||
let url = get_file_url(instance_url, &db_item.file_name);
|
||||
let media_type = db_item.media_type.clone().unwrap_or("".to_string());
|
||||
let media_type = db_item.media_type.clone();
|
||||
Attachment {
|
||||
name: None,
|
||||
attachment_type: DOCUMENT.to_string(),
|
||||
|
|
|
@ -74,7 +74,7 @@ pub async fn fetch_avatar_and_banner(
|
|||
) -> Result<(Option<String>, Option<String>), FetchError> {
|
||||
let avatar = match &actor.icon {
|
||||
Some(icon) => {
|
||||
let file_name = fetch_attachment(
|
||||
let (file_name, _) = fetch_attachment(
|
||||
&icon.url,
|
||||
media_dir,
|
||||
).await?;
|
||||
|
@ -84,7 +84,7 @@ pub async fn fetch_avatar_and_banner(
|
|||
};
|
||||
let banner = match &actor.image {
|
||||
Some(image) => {
|
||||
let file_name = fetch_attachment(
|
||||
let (file_name, _) = fetch_attachment(
|
||||
&image.url,
|
||||
media_dir,
|
||||
).await?;
|
||||
|
@ -158,11 +158,11 @@ pub async fn fetch_profile_by_actor_id(
|
|||
pub async fn fetch_attachment(
|
||||
url: &str,
|
||||
output_dir: &Path,
|
||||
) -> Result<String, FetchError> {
|
||||
) -> Result<(String, Option<String>), FetchError> {
|
||||
let response = reqwest::get(url).await?;
|
||||
let file_data = response.bytes().await?;
|
||||
let file_name = save_file(file_data.to_vec(), output_dir)?;
|
||||
Ok(file_name)
|
||||
let (file_name, media_type) = save_file(file_data.to_vec(), output_dir)?;
|
||||
Ok((file_name, media_type))
|
||||
}
|
||||
|
||||
pub async fn fetch_object(
|
||||
|
|
|
@ -221,20 +221,23 @@ pub async fn process_note(
|
|||
.ok_or(ValidationError("no content"))?;
|
||||
let mut attachments: Vec<Uuid> = Vec::new();
|
||||
if let Some(list) = object.attachment {
|
||||
let mut downloaded: Vec<(String, String)> = Vec::new();
|
||||
let mut downloaded = vec![];
|
||||
let output_dir = config.media_dir();
|
||||
for attachment in list {
|
||||
let file_name = fetch_attachment(&attachment.url, &output_dir).await
|
||||
let (file_name, media_type) = fetch_attachment(&attachment.url, &output_dir).await
|
||||
.map_err(|_| ValidationError("failed to fetch attachment"))?;
|
||||
log::info!("downloaded attachment {}", attachment.url);
|
||||
downloaded.push((file_name, attachment.media_type));
|
||||
downloaded.push((
|
||||
file_name,
|
||||
attachment.media_type.or(media_type),
|
||||
));
|
||||
}
|
||||
for (file_name, media_type) in downloaded {
|
||||
let db_attachment = create_attachment(
|
||||
db_client,
|
||||
&author.id,
|
||||
Some(media_type),
|
||||
file_name,
|
||||
media_type,
|
||||
).await?;
|
||||
attachments.push(db_attachment.id);
|
||||
}
|
||||
|
|
|
@ -28,8 +28,8 @@ async fn create_attachment_view(
|
|||
let db_attachment = create_attachment(
|
||||
db_client,
|
||||
¤t_user.id,
|
||||
media_type,
|
||||
file_name,
|
||||
media_type,
|
||||
).await?;
|
||||
let attachment = Attachment::from_db(
|
||||
db_attachment,
|
||||
|
|
|
@ -8,8 +8,8 @@ use super::types::DbMediaAttachment;
|
|||
pub async fn create_attachment(
|
||||
db_client: &impl GenericClient,
|
||||
owner_id: &Uuid,
|
||||
media_type: Option<String>,
|
||||
file_name: String,
|
||||
media_type: Option<String>,
|
||||
) -> Result<DbMediaAttachment, DatabaseError> {
|
||||
let attachment_id = new_uuid();
|
||||
let inserted_row = db_client.query_one(
|
||||
|
|
|
@ -18,6 +18,10 @@ pub enum FileError {
|
|||
InvalidMediaType,
|
||||
}
|
||||
|
||||
fn sniff_media_type(data: &[u8]) -> Option<String> {
|
||||
data.sniff_mime_type().map(|val| val.to_string())
|
||||
}
|
||||
|
||||
/// Generates unique file name based on file contents
|
||||
fn get_file_name(data: &[u8], media_type: Option<&str>) -> String {
|
||||
let digest = Sha256::digest(data);
|
||||
|
@ -38,15 +42,15 @@ fn write_file(data: Vec<u8>, file_path: &Path) -> Result<(), FileError> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_file(data: Vec<u8>, output_dir: &Path) -> Result<String, FileError> {
|
||||
let file_name = get_file_name(&data, data.sniff_mime_type());
|
||||
pub fn save_file(
|
||||
data: Vec<u8>,
|
||||
output_dir: &Path,
|
||||
) -> Result<(String, Option<String>), FileError> {
|
||||
let media_type = sniff_media_type(&data);
|
||||
let file_name = get_file_name(&data, media_type.as_deref());
|
||||
let file_path = output_dir.join(&file_name);
|
||||
write_file(data, &file_path)?;
|
||||
Ok(file_name)
|
||||
}
|
||||
|
||||
fn sniff_media_type(data: &[u8]) -> Option<String> {
|
||||
data.sniff_mime_type().map(|val| val.to_string())
|
||||
Ok((file_name, media_type))
|
||||
}
|
||||
|
||||
pub fn save_b64_file(
|
||||
|
|
Loading…
Reference in a new issue