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