1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00

feat(service/media): improve S3 error processing

This commit is contained in:
AndSDev 2025-06-06 12:59:27 +03:00
parent e7822f0332
commit 2c47045659
2 changed files with 22 additions and 7 deletions

View file

@ -3,6 +3,7 @@ use std::{io::Cursor, sync::Arc};
pub use data::Data; pub use data::Data;
use futures_util::{stream, StreamExt}; use futures_util::{stream, StreamExt};
use http::StatusCode;
use ruma::{ use ruma::{
api::client::{error::ErrorKind, media::is_safe_inline_content_type}, api::client::{error::ErrorKind, media::is_safe_inline_content_type},
http_headers::{ContentDisposition, ContentDispositionType}, http_headers::{ContentDisposition, ContentDispositionType},
@ -621,16 +622,23 @@ impl Service {
credentials, credentials,
duration, duration,
} => { } => {
let sha256_hex = hex::encode(sha256_digest);
let url = bucket let url = bucket
.get_object(Some(credentials), &hex::encode(sha256_digest)) .get_object(Some(credentials), &sha256_hex)
.sign(*duration); .sign(*duration);
let client = services().globals.default_client(); let client = services().globals.default_client();
let resp = client.get(url).send().await?; let resp = client.get(url).send().await?;
if resp.status() == StatusCode::NOT_FOUND {
return Err(Error::BadRequest(
ErrorKind::NotFound,
"File does not exist",
));
}
if !resp.status().is_success() { if !resp.status().is_success() {
warn!("S3 request error:\n{}", resp.text().await?); warn!("S3 request error ({}):\n{}", sha256_hex, resp.text().await?);
return Err(Error::bad_database("Cannot read media file")); return Err(Error::bad_s3_response("Cannot read media file"));
} }
resp.bytes().await?.to_vec() resp.bytes().await?.to_vec()
@ -681,8 +689,8 @@ pub async fn create_file(sha256_hex: &str, file: &[u8]) -> Result<()> {
let resp = client.put(url).body(file.to_vec()).send().await?; let resp = client.put(url).body(file.to_vec()).send().await?;
if !resp.status().is_success() { if !resp.status().is_success() {
warn!("S3 request error:\n{}", resp.text().await?); warn!("S3 request error ({}):\n{}", sha256_hex, resp.text().await?);
return Err(Error::bad_database("Cannot write media file")); return Err(Error::bad_s3_response("Cannot write media file"));
} }
} }
} }
@ -768,8 +776,8 @@ async fn delete_file(sha256_hex: &str) -> Result<()> {
let resp = client.delete(url).send().await?; let resp = client.delete(url).send().await?;
if !resp.status().is_success() { if !resp.status().is_success() {
warn!("S3 request error:\n{}", resp.text().await?); warn!("S3 request error ({}):\n{}", sha256_hex, resp.text().await?);
return Err(Error::bad_database("Cannot delete media file")); return Err(Error::bad_s3_response("Cannot delete media file"));
} }
} }
} }

View file

@ -52,6 +52,8 @@ pub enum Error {
source: std::io::Error, source: std::io::Error,
}, },
#[error("{0}")] #[error("{0}")]
BadS3Response(&'static str),
#[error("{0}")]
BadServerResponse(&'static str), BadServerResponse(&'static str),
#[error("{0}")] #[error("{0}")]
BadConfig(&'static str), BadConfig(&'static str),
@ -91,6 +93,11 @@ impl Error {
error!("BadConfig: {}", message); error!("BadConfig: {}", message);
Self::BadConfig(message) Self::BadConfig(message)
} }
pub fn bad_s3_response(message: &'static str) -> Self {
info!("BadS3Response: {}", message);
Self::BadS3Response(message)
}
} }
impl Error { impl Error {