docs
This commit is contained in:
@@ -4,10 +4,10 @@ use log::{error, warn};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::auth::{get_id_by_token, user_added_file};
|
||||
use crate::s3_utils::{self, upload_to_s3, generate_key_with_extension};
|
||||
use crate::handlers::MAX_USER_QUOTA_BYTES;
|
||||
use crate::lookup::store_file_info;
|
||||
use crate::s3_utils::{self, generate_key_with_extension, upload_to_s3};
|
||||
use futures::TryStreamExt;
|
||||
use crate::handlers::MAX_WEEK_BYTES;
|
||||
// use crate::thumbnail::convert_heic_to_jpeg;
|
||||
|
||||
/// Обработчик для аплоада файлов.
|
||||
@@ -28,7 +28,7 @@ pub async fn upload_handler(
|
||||
let user_id = get_id_by_token(token.unwrap()).await?;
|
||||
|
||||
// Получаем текущую квоту пользователя
|
||||
let this_week_amount: u64 = state.get_or_create_quota(&user_id).await.unwrap_or(0);
|
||||
let current_quota: u64 = state.get_or_create_quota(&user_id).await.unwrap_or(0);
|
||||
let mut body = "ok".to_string();
|
||||
while let Ok(Some(field)) = payload.try_next().await {
|
||||
let mut field = field;
|
||||
@@ -46,7 +46,9 @@ pub async fn upload_handler(
|
||||
Some(mime) => mime,
|
||||
None => {
|
||||
warn!("Неподдерживаемый формат файла");
|
||||
return Err(actix_web::error::ErrorUnsupportedMediaType("Неподдерживаемый формат файла"));
|
||||
return Err(actix_web::error::ErrorUnsupportedMediaType(
|
||||
"Неподдерживаемый формат файла",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -63,14 +65,18 @@ pub async fn upload_handler(
|
||||
Some(ext) => ext,
|
||||
None => {
|
||||
warn!("Неподдерживаемый тип содержимого: {}", content_type);
|
||||
return Err(actix_web::error::ErrorUnsupportedMediaType("Неподдерживаемый тип содержимого"));
|
||||
return Err(actix_web::error::ErrorUnsupportedMediaType(
|
||||
"Неподдерживаемый тип содержимого",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Проверяем, что добавление файла не превышает лимит квоты
|
||||
if this_week_amount + file_size > MAX_WEEK_BYTES {
|
||||
warn!("Quota would exceed limit: current={}, adding={}, limit={}",
|
||||
this_week_amount, file_size, MAX_WEEK_BYTES);
|
||||
if current_quota + file_size > MAX_USER_QUOTA_BYTES {
|
||||
warn!(
|
||||
"Quota would exceed limit: current={}, adding={}, limit={}",
|
||||
current_quota, file_size, MAX_USER_QUOTA_BYTES
|
||||
);
|
||||
return Err(actix_web::error::ErrorUnauthorized("Quota exceeded"));
|
||||
}
|
||||
|
||||
@@ -84,27 +90,33 @@ pub async fn upload_handler(
|
||||
&filename,
|
||||
file_bytes,
|
||||
&content_type,
|
||||
).await {
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
warn!("file {} uploaded to storj, incrementing quota by {} bytes", filename, file_size);
|
||||
warn!(
|
||||
"file {} uploaded to storj, incrementing quota by {} bytes",
|
||||
filename, file_size
|
||||
);
|
||||
if let Err(e) = state.increment_uploaded_bytes(&user_id, file_size).await {
|
||||
error!("Failed to increment quota: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
|
||||
// Сохраняем информацию о файле в Redis
|
||||
let mut redis = state.redis.clone();
|
||||
store_file_info(&mut redis, &filename, &content_type).await?;
|
||||
user_added_file(&mut redis, &user_id, &filename).await?;
|
||||
|
||||
|
||||
// Сохраняем маппинг пути
|
||||
let generated_key = generate_key_with_extension(filename.clone(), content_type.clone());
|
||||
let generated_key =
|
||||
generate_key_with_extension(filename.clone(), content_type.clone());
|
||||
state.set_path(&filename, &generated_key).await;
|
||||
|
||||
|
||||
if let Ok(new_quota) = state.get_or_create_quota(&user_id).await {
|
||||
warn!("New quota for user {}: {} bytes", user_id, new_quota);
|
||||
}
|
||||
|
||||
|
||||
body = filename;
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user