2024-10-22 00:36:42 +03:00
|
|
|
|
use actix_multipart::Multipart;
|
2025-09-01 22:58:03 +03:00
|
|
|
|
use actix_web::{HttpRequest, HttpResponse, Result, web};
|
2025-09-01 20:36:15 +03:00
|
|
|
|
use log::{error, info, warn};
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
|
|
|
|
|
use crate::app_state::AppState;
|
2025-09-02 11:40:43 +03:00
|
|
|
|
use crate::auth::{extract_user_id_from_token, user_added_file};
|
2025-08-02 00:18:09 +03:00
|
|
|
|
use crate::handlers::MAX_USER_QUOTA_BYTES;
|
2024-11-13 11:14:53 +03:00
|
|
|
|
use crate::lookup::store_file_info;
|
2025-08-02 00:18:09 +03:00
|
|
|
|
use crate::s3_utils::{self, generate_key_with_extension, upload_to_s3};
|
2025-09-02 11:40:43 +03:00
|
|
|
|
use super::common::extract_and_validate_token;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
use futures::TryStreamExt;
|
2024-11-13 11:32:50 +03:00
|
|
|
|
// use crate::thumbnail::convert_heic_to_jpeg;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Максимальный размер одного файла: 500 МБ
|
|
|
|
|
|
const MAX_SINGLE_FILE_BYTES: u64 = 500 * 1024 * 1024;
|
|
|
|
|
|
|
|
|
|
|
|
/// Обработчик для аплоада файлов с улучшенной логикой квот и валидацией.
|
2024-10-22 00:36:42 +03:00
|
|
|
|
pub async fn upload_handler(
|
|
|
|
|
|
req: HttpRequest,
|
|
|
|
|
|
mut payload: Multipart,
|
|
|
|
|
|
state: web::Data<AppState>,
|
|
|
|
|
|
) -> Result<HttpResponse, actix_web::Error> {
|
2025-09-02 11:40:43 +03:00
|
|
|
|
// Извлекаем и валидируем токен
|
|
|
|
|
|
let token = extract_and_validate_token(&req)?;
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Затем извлекаем user_id
|
2025-09-01 22:58:03 +03:00
|
|
|
|
let user_id = extract_user_id_from_token(token).map_err(|e| {
|
|
|
|
|
|
warn!("Failed to extract user_id from token: {}", e);
|
|
|
|
|
|
actix_web::error::ErrorUnauthorized("Invalid authorization token")
|
|
|
|
|
|
})?;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
|
|
|
|
|
// Получаем текущую квоту пользователя
|
2025-08-02 00:18:09 +03:00
|
|
|
|
let current_quota: u64 = state.get_or_create_quota(&user_id).await.unwrap_or(0);
|
2025-09-01 20:36:15 +03:00
|
|
|
|
info!("Author {} current quota: {} bytes", user_id, current_quota);
|
|
|
|
|
|
|
|
|
|
|
|
// Предварительная проверка: есть ли вообще место для файлов
|
|
|
|
|
|
if current_quota >= MAX_USER_QUOTA_BYTES {
|
2025-09-01 22:58:03 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Author {} quota already at maximum: {}",
|
|
|
|
|
|
user_id, current_quota
|
|
|
|
|
|
);
|
|
|
|
|
|
return Err(actix_web::error::ErrorPayloadTooLarge(
|
|
|
|
|
|
"Author quota limit exceeded",
|
|
|
|
|
|
));
|
2025-09-01 20:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let mut uploaded_files = Vec::new();
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2024-10-22 00:36:42 +03:00
|
|
|
|
while let Ok(Some(field)) = payload.try_next().await {
|
|
|
|
|
|
let mut field = field;
|
2024-11-13 11:14:53 +03:00
|
|
|
|
let mut file_bytes = Vec::new();
|
|
|
|
|
|
let mut file_size: u64 = 0;
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Читаем данные файла с проверкой размера
|
2024-11-13 11:14:53 +03:00
|
|
|
|
while let Ok(Some(chunk)) = field.try_next().await {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
let chunk_size = chunk.len() as u64;
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Проверка лимита одного файла
|
|
|
|
|
|
if file_size + chunk_size > MAX_SINGLE_FILE_BYTES {
|
2025-09-01 22:58:03 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"File size exceeds single file limit: {} > {}",
|
|
|
|
|
|
file_size + chunk_size,
|
|
|
|
|
|
MAX_SINGLE_FILE_BYTES
|
|
|
|
|
|
);
|
|
|
|
|
|
return Err(actix_web::error::ErrorPayloadTooLarge(
|
|
|
|
|
|
"Single file size limit exceeded",
|
|
|
|
|
|
));
|
2025-09-01 20:36:15 +03:00
|
|
|
|
}
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Проверка общей квоты пользователя
|
|
|
|
|
|
if current_quota + file_size + chunk_size > MAX_USER_QUOTA_BYTES {
|
2025-09-01 22:58:03 +03:00
|
|
|
|
warn!(
|
|
|
|
|
|
"Upload would exceed user quota: current={}, adding={}, limit={}",
|
|
|
|
|
|
current_quota,
|
|
|
|
|
|
file_size + chunk_size,
|
|
|
|
|
|
MAX_USER_QUOTA_BYTES
|
|
|
|
|
|
);
|
|
|
|
|
|
return Err(actix_web::error::ErrorPayloadTooLarge(
|
|
|
|
|
|
"Author quota limit would be exceeded",
|
|
|
|
|
|
));
|
2025-09-01 20:36:15 +03:00
|
|
|
|
}
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
file_size += chunk_size;
|
2024-11-13 11:14:53 +03:00
|
|
|
|
file_bytes.extend_from_slice(&chunk);
|
|
|
|
|
|
}
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Пропускаем пустые файлы
|
|
|
|
|
|
if file_size == 0 {
|
|
|
|
|
|
warn!("Skipping empty file upload");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
info!("Processing file: {} bytes", file_size);
|
|
|
|
|
|
|
2024-11-13 11:14:53 +03:00
|
|
|
|
// Определяем MIME-тип из содержимого файла
|
|
|
|
|
|
let detected_mime_type = match s3_utils::detect_mime_type(&file_bytes) {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
Some(mime) => {
|
|
|
|
|
|
info!("Detected MIME type: {}", mime);
|
|
|
|
|
|
mime
|
|
|
|
|
|
}
|
2024-11-13 11:14:53 +03:00
|
|
|
|
None => {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
warn!("Unsupported file format detected");
|
2025-08-02 00:18:09 +03:00
|
|
|
|
return Err(actix_web::error::ErrorUnsupportedMediaType(
|
2025-09-01 20:36:15 +03:00
|
|
|
|
"Unsupported file format",
|
2025-08-02 00:18:09 +03:00
|
|
|
|
));
|
2024-11-13 11:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
|
// Для HEIC файлов просто сохраняем как есть
|
2024-11-13 11:14:53 +03:00
|
|
|
|
let (file_bytes, content_type) = if detected_mime_type == "image/heic" {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
info!("Processing HEIC file (saved as original)");
|
2024-11-13 11:32:50 +03:00
|
|
|
|
(file_bytes, detected_mime_type)
|
2024-11-13 11:14:53 +03:00
|
|
|
|
} else {
|
|
|
|
|
|
(file_bytes, detected_mime_type)
|
|
|
|
|
|
};
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2024-11-13 11:14:53 +03:00
|
|
|
|
// Получаем расширение из MIME-типа
|
|
|
|
|
|
let extension = match s3_utils::get_extension_from_mime(&content_type) {
|
|
|
|
|
|
Some(ext) => ext,
|
|
|
|
|
|
None => {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
warn!("No file extension found for MIME type: {}", content_type);
|
2025-08-02 00:18:09 +03:00
|
|
|
|
return Err(actix_web::error::ErrorUnsupportedMediaType(
|
2025-09-01 20:36:15 +03:00
|
|
|
|
"Unsupported content type",
|
2025-08-02 00:18:09 +03:00
|
|
|
|
));
|
2024-10-22 00:36:42 +03:00
|
|
|
|
}
|
2024-11-13 11:14:53 +03:00
|
|
|
|
};
|
2024-10-22 00:36:42 +03:00
|
|
|
|
|
2024-11-13 11:14:53 +03:00
|
|
|
|
// Генерируем имя файла с правильным расширением
|
|
|
|
|
|
let filename = format!("{}.{}", uuid::Uuid::new_v4(), extension);
|
2025-09-01 20:36:15 +03:00
|
|
|
|
info!("Generated filename: {}", filename);
|
2024-11-13 11:14:53 +03:00
|
|
|
|
|
|
|
|
|
|
// Загружаем файл в S3 storj
|
|
|
|
|
|
match upload_to_s3(
|
|
|
|
|
|
&state.storj_client,
|
|
|
|
|
|
&state.bucket,
|
|
|
|
|
|
&filename,
|
|
|
|
|
|
file_bytes,
|
|
|
|
|
|
&content_type,
|
2025-08-02 00:18:09 +03:00
|
|
|
|
)
|
|
|
|
|
|
.await
|
|
|
|
|
|
{
|
2024-11-13 11:14:53 +03:00
|
|
|
|
Ok(_) => {
|
2025-09-01 22:58:03 +03:00
|
|
|
|
info!(
|
|
|
|
|
|
"File {} successfully uploaded to S3 ({} bytes)",
|
|
|
|
|
|
filename, file_size
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Обновляем квоту пользователя
|
2024-11-13 11:14:53 +03:00
|
|
|
|
if let Err(e) = state.increment_uploaded_bytes(&user_id, file_size).await {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
error!("Failed to increment quota for user {}: {}", user_id, e);
|
|
|
|
|
|
return Err(actix_web::error::ErrorInternalServerError(
|
2025-09-01 22:58:03 +03:00
|
|
|
|
"Failed to update user quota",
|
2025-09-01 20:36:15 +03:00
|
|
|
|
));
|
2024-11-12 12:29:19 +03:00
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2024-11-13 11:14:53 +03:00
|
|
|
|
// Сохраняем информацию о файле в Redis
|
|
|
|
|
|
let mut redis = state.redis.clone();
|
2025-09-01 20:36:15 +03:00
|
|
|
|
if let Err(e) = store_file_info(&mut redis, &filename, &content_type).await {
|
|
|
|
|
|
error!("Failed to store file info in Redis: {}", e);
|
|
|
|
|
|
// Не прерываем процесс, файл уже загружен в S3
|
|
|
|
|
|
}
|
2025-09-01 22:58:03 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
if let Err(e) = user_added_file(&mut redis, &user_id, &filename).await {
|
|
|
|
|
|
error!("Failed to record user file association: {}", e);
|
|
|
|
|
|
// Не прерываем процесс
|
|
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2024-11-13 11:14:53 +03:00
|
|
|
|
// Сохраняем маппинг пути
|
2025-09-01 22:58:03 +03:00
|
|
|
|
let generated_key =
|
|
|
|
|
|
generate_key_with_extension(filename.clone(), content_type.clone());
|
2024-11-13 11:14:53 +03:00
|
|
|
|
state.set_path(&filename, &generated_key).await;
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
// Логируем новую квоту
|
2024-11-13 11:14:53 +03:00
|
|
|
|
if let Ok(new_quota) = state.get_or_create_quota(&user_id).await {
|
2025-09-01 22:58:03 +03:00
|
|
|
|
info!(
|
|
|
|
|
|
"Updated quota for user {}: {} bytes ({:.1}% used)",
|
|
|
|
|
|
user_id,
|
|
|
|
|
|
new_quota,
|
|
|
|
|
|
(new_quota as f64 / MAX_USER_QUOTA_BYTES as f64) * 100.0
|
|
|
|
|
|
);
|
2024-11-12 12:29:19 +03:00
|
|
|
|
}
|
2025-08-02 00:18:09 +03:00
|
|
|
|
|
2025-09-01 20:36:15 +03:00
|
|
|
|
uploaded_files.push(filename);
|
2024-11-13 11:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
Err(e) => {
|
2025-09-01 20:36:15 +03:00
|
|
|
|
error!("Failed to upload file to S3: {}", e);
|
|
|
|
|
|
return Err(actix_web::error::ErrorInternalServerError(
|
2025-09-01 22:58:03 +03:00
|
|
|
|
"File upload failed",
|
2025-09-01 20:36:15 +03:00
|
|
|
|
));
|
2024-10-22 21:23:34 +03:00
|
|
|
|
}
|
2024-10-22 00:36:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-01 20:36:15 +03:00
|
|
|
|
|
|
|
|
|
|
// Возвращаем результат
|
|
|
|
|
|
match uploaded_files.len() {
|
|
|
|
|
|
0 => {
|
|
|
|
|
|
warn!("No files were uploaded");
|
2025-09-01 22:58:03 +03:00
|
|
|
|
Err(actix_web::error::ErrorBadRequest(
|
|
|
|
|
|
"No files provided or all files were empty",
|
|
|
|
|
|
))
|
2025-09-01 20:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
1 => {
|
|
|
|
|
|
info!("Successfully uploaded 1 file: {}", uploaded_files[0]);
|
|
|
|
|
|
Ok(HttpResponse::Ok().body(uploaded_files[0].clone()))
|
|
|
|
|
|
}
|
|
|
|
|
|
n => {
|
|
|
|
|
|
info!("Successfully uploaded {} files", n);
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
|
|
|
|
|
"uploaded_files": uploaded_files,
|
|
|
|
|
|
"count": n
|
|
|
|
|
|
})))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-22 00:36:42 +03:00
|
|
|
|
}
|