This commit is contained in:
parent
8cba92529e
commit
be8c03eb11
|
@ -1,6 +1,6 @@
|
|||
use actix_multipart::Multipart;
|
||||
use actix_web::{web, HttpRequest, HttpResponse, Result};
|
||||
use log::warn;
|
||||
use log::{error, warn};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::auth::{get_id_by_token, user_added_file};
|
||||
|
@ -50,31 +50,46 @@ pub async fn upload_handler(
|
|||
|
||||
// Проверяем, что добавление файла не превышает лимит квоты
|
||||
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);
|
||||
return Err(actix_web::error::ErrorUnauthorized("Quota exceeded"));
|
||||
// Квота превышена
|
||||
}
|
||||
|
||||
// инкрементируем квоту пользователя
|
||||
let _ = state.increment_uploaded_bytes(&user_id, file_size).await?;
|
||||
|
||||
// Определяем правильное расширение и ключ для S3
|
||||
body = generate_key_with_extension(name, content_type.to_owned());
|
||||
|
||||
// Загружаем файл в S3
|
||||
if let Err(e) = upload_to_s3(
|
||||
// Загружаем файл в S3 storj с использованием ключа в нижнем регистре
|
||||
let filekey = generate_key_with_extension(name.clone(), content_type.to_owned());
|
||||
let orig_path = name.clone();
|
||||
|
||||
match upload_to_s3(
|
||||
&state.storj_client,
|
||||
&state.bucket,
|
||||
&body,
|
||||
file_bytes,
|
||||
&filekey,
|
||||
file_bytes.clone(),
|
||||
&content_type,
|
||||
)
|
||||
.await {
|
||||
warn!("cannot upload to storj: {}", e);
|
||||
} else {
|
||||
warn!("file {} uploaded to storj", body);
|
||||
// Сохраняем информацию о загруженном файле для пользователя
|
||||
user_added_file(&mut state.redis.clone(), &user_id, &body).await?;
|
||||
state.set_path(&body, &body).await;
|
||||
).await {
|
||||
Ok(_) => {
|
||||
warn!("file {} uploaded to storj, incrementing quota by {} bytes", filekey, file_size);
|
||||
// Инкрементируем квоту только после успешной загрузки
|
||||
if let Err(e) = state.increment_uploaded_bytes(&user_id, file_size).await {
|
||||
error!("Failed to increment quota: {}", e);
|
||||
// Можно добавить откат загрузки файла здесь
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
// Сохраняем информацию о загруженном файле
|
||||
user_added_file(&mut state.redis.clone(), &user_id, &filekey).await?;
|
||||
state.set_path(&filekey, &orig_path).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 = filekey;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to upload to storj: {}", e);
|
||||
return Err(actix_web::error::ErrorInternalServerError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user