| 1 |
|
| 2 |
|
| 3 |
use axum::{Json, extract::State, response::IntoResponse}; |
| 4 |
use serde::Deserialize; |
| 5 |
use sqlx::PgPool; |
| 6 |
use std::str::FromStr; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
AppStorage, Scanning, |
| 10 |
auth::AuthUser, |
| 11 |
db::{self, ItemId}, |
| 12 |
error::{AppError, Result, ResultExt}, |
| 13 |
storage::{CACHE_CONTROL_IMMUTABLE, FileType, S3Client}, |
| 14 |
}; |
| 15 |
|
| 16 |
use super::{CommitTarget, ConfirmUploadResponse, PresignUploadResponse, commit_upload}; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#[derive(Debug, Deserialize)] |
| 27 |
pub(super) struct PresignUploadRequest { |
| 28 |
pub item_id: ItemId, |
| 29 |
pub file_type: String, |
| 30 |
pub file_name: String, |
| 31 |
pub content_type: String, |
| 32 |
#[serde(default)] |
| 33 |
pub file_size_bytes: Option<i64>, |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
#[derive(Debug, Deserialize)] |
| 38 |
pub(super) struct ConfirmUploadRequest { |
| 39 |
pub item_id: ItemId, |
| 40 |
pub file_type: String, |
| 41 |
pub s3_key: String, |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#[tracing::instrument(skip_all, name = "storage::presign_upload", fields(user_id = %user.id))] |
| 50 |
pub(super) async fn presign_upload( |
| 51 |
State(db): State<PgPool>, |
| 52 |
State(storage): State<AppStorage>, |
| 53 |
AuthUser(user): AuthUser, |
| 54 |
Json(req): Json<PresignUploadRequest>, |
| 55 |
) -> Result<impl IntoResponse> { |
| 56 |
user.check_not_suspended()?; |
| 57 |
|
| 58 |
let s3 = storage.require_s3()?; |
| 59 |
|
| 60 |
|
| 61 |
let file_type = FileType::from_str(&req.file_type) |
| 62 |
.map_err(|_| AppError::BadRequest(format!("Invalid file type: {}", req.file_type)))?; |
| 63 |
|
| 64 |
S3Client::validate_content_type(file_type, &req.content_type)?; |
| 65 |
|
| 66 |
S3Client::validate_extension(file_type, &req.file_name)?; |
| 67 |
|
| 68 |
|
| 69 |
let owner = db::items::get_item_owner(&db, req.item_id) |
| 70 |
.await? |
| 71 |
.ok_or(AppError::NotFound)?; |
| 72 |
|
| 73 |
if owner != user.id { |
| 74 |
return Err(AppError::Forbidden); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
db::creator_tiers::check_presign_allowed(&db, user.id, file_type).await?; |
| 79 |
|
| 80 |
|
| 81 |
let max_file_bytes = |
| 82 |
db::creator_tiers::get_effective_max_file_bytes(&db, user.id, file_type).await?; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
super::validate_declared_upload_size(req.file_size_bytes, file_type, max_file_bytes)?; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
let s3_key = S3Client::generate_staging_key(&req.file_name); |
| 96 |
|
| 97 |
|
| 98 |
db::pending_uploads::record_pending_upload(&db, user.id, &s3_key, "main").await?; |
| 99 |
|
| 100 |
|
| 101 |
let expires_in = 3600; |
| 102 |
let upload_url = s3 |
| 103 |
.presign_upload( |
| 104 |
&s3_key, |
| 105 |
&req.content_type, |
| 106 |
Some(expires_in), |
| 107 |
Some(CACHE_CONTROL_IMMUTABLE), |
| 108 |
req.file_size_bytes, |
| 109 |
) |
| 110 |
.await |
| 111 |
.context("presign upload for item content")?; |
| 112 |
|
| 113 |
Ok(Json(PresignUploadResponse { |
| 114 |
upload_url, |
| 115 |
s3_key: s3_key.into_string(), |
| 116 |
expires_in, |
| 117 |
cache_control: Some(CACHE_CONTROL_IMMUTABLE.to_string()), |
| 118 |
max_file_bytes, |
| 119 |
})) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[tracing::instrument(skip_all, name = "storage::confirm_upload", fields(user_id = %user.id))] |
| 128 |
pub(super) async fn confirm_upload( |
| 129 |
State(db): State<PgPool>, |
| 130 |
State(storage): State<AppStorage>, |
| 131 |
State(scanning): State<Scanning>, |
| 132 |
AuthUser(user): AuthUser, |
| 133 |
Json(req): Json<ConfirmUploadRequest>, |
| 134 |
) -> Result<impl IntoResponse> { |
| 135 |
user.check_not_suspended()?; |
| 136 |
|
| 137 |
let s3 = storage.require_s3()?; |
| 138 |
|
| 139 |
|
| 140 |
let file_type = FileType::from_str(&req.file_type) |
| 141 |
.map_err(|_| AppError::BadRequest(format!("Invalid file type: {}", req.file_type)))?; |
| 142 |
|
| 143 |
|
| 144 |
let owner = db::items::get_item_owner(&db, req.item_id) |
| 145 |
.await? |
| 146 |
.ok_or(AppError::NotFound)?; |
| 147 |
|
| 148 |
if owner != user.id { |
| 149 |
return Err(AppError::Forbidden); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if !s3.object_exists(&req.s3_key).await? { |
| 158 |
return Err(AppError::BadRequest( |
| 159 |
"Upload not found. Please try uploading again.".to_string(), |
| 160 |
)); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
let file_size_bytes = s3.object_size(&req.s3_key).await?.ok_or_else(|| { |
| 165 |
AppError::BadRequest( |
| 166 |
"Could not determine file size. Please try uploading again.".to_string(), |
| 167 |
) |
| 168 |
})?; |
| 169 |
if file_size_bytes as u64 > file_type.max_size() { |
| 170 |
super::enqueue_s3_orphan( |
| 171 |
&db, |
| 172 |
&req.s3_key, |
| 173 |
crate::storage::S3Bucket::Main, |
| 174 |
"item_upload_rejected", |
| 175 |
) |
| 176 |
.await; |
| 177 |
let limit_mb = file_type.max_size() / (1024 * 1024); |
| 178 |
let file_mb = file_size_bytes as u64 / (1024 * 1024); |
| 179 |
return Err(AppError::FileTooLarge(format!( |
| 180 |
"File is {} MB but the maximum for {} files is {} MB.", |
| 181 |
file_mb, |
| 182 |
file_type.as_str(), |
| 183 |
limit_mb |
| 184 |
))); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
let max_storage = |
| 189 |
match db::creator_tiers::check_upload_allowed(&db, user.id, file_type, file_size_bytes) |
| 190 |
.await |
| 191 |
{ |
| 192 |
Ok(max) => max, |
| 193 |
Err(e) => { |
| 194 |
super::enqueue_s3_orphan( |
| 195 |
&db, |
| 196 |
&req.s3_key, |
| 197 |
crate::storage::S3Bucket::Main, |
| 198 |
"item_upload_rejected", |
| 199 |
) |
| 200 |
.await; |
| 201 |
return Err(e); |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if let crate::storage::GenericItemConfirm::UseRoute(route) = file_type.generic_item_confirm() { |
| 214 |
super::enqueue_s3_orphan( |
| 215 |
&db, |
| 216 |
&req.s3_key, |
| 217 |
crate::storage::S3Bucket::Main, |
| 218 |
"item_upload_rejected", |
| 219 |
) |
| 220 |
.await; |
| 221 |
return Err(AppError::BadRequest(format!( |
| 222 |
"This file type isn't confirmed here. Use {route}." |
| 223 |
))); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
let mut old_s3_key: Option<String> = None; |
| 229 |
let mut replaced_old_size: i64 = 0; |
| 230 |
let mut item_project_id: Option<db::ProjectId> = None; |
| 231 |
if let Some(item) = db::items::get_item_by_id(&db, req.item_id).await? { |
| 232 |
item_project_id = Some(item.project_id); |
| 233 |
let existing_key = match file_type { |
| 234 |
FileType::Audio => item.audio_s3_key.as_deref(), |
| 235 |
FileType::Cover => item.cover_s3_key.as_deref(), |
| 236 |
FileType::Video => item.video_s3_key.as_deref(), |
| 237 |
_ => None, |
| 238 |
}; |
| 239 |
if existing_key == Some(&req.s3_key) { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
if let Err(e) = |
| 245 |
db::pending_uploads::remove_pending_upload(&db, user.id, &req.s3_key, "main").await |
| 246 |
{ |
| 247 |
tracing::warn!(error = ?e, key = %req.s3_key, "remove_pending_upload failed on idempotent re-confirm"); |
| 248 |
} |
| 249 |
return Ok(Json(ConfirmUploadResponse { |
| 250 |
success: true, |
| 251 |
pending_review: None, |
| 252 |
})); |
| 253 |
} |
| 254 |
if let Some(old_key) = existing_key { |
| 255 |
old_s3_key = Some(old_key.to_string()); |
| 256 |
replaced_old_size = match file_type { |
| 257 |
FileType::Audio => item.audio_file_size_bytes.unwrap_or(0), |
| 258 |
FileType::Cover => item.cover_file_size_bytes.unwrap_or(0), |
| 259 |
FileType::Video => item.video_file_size_bytes.unwrap_or(0), |
| 260 |
_ => 0, |
| 261 |
}; |
| 262 |
} |
| 263 |
} |
| 264 |
let is_replace = old_s3_key.is_some(); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
if !db::pending_uploads::is_owned(&db, user.id, &req.s3_key, "main").await? { |
| 279 |
return Err(AppError::BadRequest("Invalid upload key".to_string())); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
let tx_result: Result<u64> = async { |
| 294 |
let mut tx = db.begin().await?; |
| 295 |
db::creator_tiers::try_apply_storage_on( |
| 296 |
&mut tx, |
| 297 |
user.id, |
| 298 |
is_replace.then_some(replaced_old_size), |
| 299 |
file_size_bytes, |
| 300 |
max_storage, |
| 301 |
) |
| 302 |
.await?; |
| 303 |
match db::items::update_item_file_cas( |
| 304 |
&mut *tx, |
| 305 |
req.item_id, |
| 306 |
user.id, |
| 307 |
file_type, |
| 308 |
old_s3_key.as_deref(), |
| 309 |
&req.s3_key, |
| 310 |
file_size_bytes, |
| 311 |
) |
| 312 |
.await? |
| 313 |
{ |
| 314 |
db::items::FileConfirmOutcome::LostRace => { |
| 315 |
|
| 316 |
|
| 317 |
Ok(0) |
| 318 |
} |
| 319 |
db::items::FileConfirmOutcome::Committed => { |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if let Some(old_key) = old_s3_key.as_deref() { |
| 328 |
db::pending_s3_deletions::enqueue_deletions( |
| 329 |
&mut *tx, |
| 330 |
&[(old_key.to_string(), "main".to_string())], |
| 331 |
"item_upload_replace", |
| 332 |
) |
| 333 |
.await?; |
| 334 |
} |
| 335 |
tx.commit().await?; |
| 336 |
Ok(1) |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
.await; |
| 341 |
|
| 342 |
match tx_result { |
| 343 |
Err(e) => { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
super::enqueue_s3_orphan( |
| 351 |
&db, |
| 352 |
&req.s3_key, |
| 353 |
crate::storage::S3Bucket::Main, |
| 354 |
"item_confirm_failed", |
| 355 |
) |
| 356 |
.await; |
| 357 |
return Err(e); |
| 358 |
} |
| 359 |
Ok(0) => { |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
super::enqueue_s3_orphan( |
| 367 |
&db, |
| 368 |
&req.s3_key, |
| 369 |
crate::storage::S3Bucket::Main, |
| 370 |
"item_upload_target_missing", |
| 371 |
) |
| 372 |
.await; |
| 373 |
return Err(AppError::BadRequest( |
| 374 |
"Item was modified concurrently. Please try uploading again.".to_string(), |
| 375 |
)); |
| 376 |
} |
| 377 |
Ok(_) => {} |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
let scan_status = commit_upload( |
| 385 |
&db, |
| 386 |
scanning.scanner.as_ref(), |
| 387 |
CommitTarget::Item(req.item_id), |
| 388 |
&req.s3_key, |
| 389 |
file_type, |
| 390 |
user.id, |
| 391 |
file_size_bytes, |
| 392 |
) |
| 393 |
.await?; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
db::pending_uploads::remove_pending_upload(&db, user.id, &req.s3_key, "main").await?; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
if let Some(project_id) = item_project_id |
| 404 |
&& let Err(e) = db::projects::bump_cache_generation(&db, project_id).await |
| 405 |
{ |
| 406 |
tracing::warn!(%project_id, error = ?e, "failed to bump cache generation after upload"); |
| 407 |
} |
| 408 |
|
| 409 |
tracing::info!( |
| 410 |
"Upload confirmed: item={}, type={:?}, key={}, size={}", |
| 411 |
req.item_id, |
| 412 |
file_type, |
| 413 |
req.s3_key, |
| 414 |
file_size_bytes |
| 415 |
); |
| 416 |
|
| 417 |
let pending_review = if scan_status == db::FileScanStatus::HeldForReview { |
| 418 |
Some(true) |
| 419 |
} else { |
| 420 |
None |
| 421 |
}; |
| 422 |
Ok(Json(ConfirmUploadResponse { |
| 423 |
success: true, |
| 424 |
pending_review, |
| 425 |
})) |
| 426 |
} |
| 427 |
|