serve-file-fix
Some checks failed
deploy / deploy (push) Failing after 49s

This commit is contained in:
2024-10-22 11:38:28 +03:00
parent 1385f64824
commit ca97ecf128
2 changed files with 18 additions and 18 deletions

View File

@@ -1,6 +1,5 @@
use actix_web::{error::ErrorInternalServerError, HttpResponse, Result};
use mime_guess::MimeGuess;
use log::warn;
use crate::app_state::AppState;
use crate::s3_utils::check_file_exists;
@@ -9,21 +8,20 @@ use crate::s3_utils::check_file_exists;
pub async fn serve_file(file_key: &str, state: &AppState) -> Result<HttpResponse, actix_web::Error> {
// Проверяем наличие файла в Storj S3
if !check_file_exists(&state.storj_client, &state.storj_bucket, &file_key).await? {
warn!("{}", file_key);
return Err(ErrorInternalServerError("File not found in Storj"));
return Err(ErrorInternalServerError(format!("File {} not found in Storj", file_key)));
}
let file_path = state.get_path(file_key).await.unwrap().unwrap();
let file_path_in_storj = state.get_path(file_key).await.unwrap().unwrap();
// Получаем объект из Storj S3
let get_object_output = state
.storj_client
.get_object()
.bucket(&state.storj_bucket)
.key(file_path.clone())
.key(file_path_in_storj.clone())
.send()
.await
.map_err(|_| ErrorInternalServerError("Failed to get object from Storj"))?;
.map_err(|_| ErrorInternalServerError(format!("Failed to get {} object from Storj", file_path_in_storj)))?;
let data: aws_sdk_s3::primitives::AggregatedBytes = get_object_output
.body
@@ -32,7 +30,7 @@ pub async fn serve_file(file_key: &str, state: &AppState) -> Result<HttpResponse
.map_err(|_| ErrorInternalServerError("Failed to read object body"))?;
let data_bytes = data.into_bytes();
let mime_type = MimeGuess::from_path(&file_path).first_or_octet_stream();
let mime_type = MimeGuess::from_path(&file_path_in_storj).first_or_octet_stream();
Ok(HttpResponse::Ok()
.content_type(mime_type.as_ref())