| 1 |
|
| 2 |
|
| 3 |
mod downloads; |
| 4 |
mod images; |
| 5 |
pub(crate) mod media; |
| 6 |
mod uploads; |
| 7 |
mod versions; |
| 8 |
|
| 9 |
use axum::routing::get; |
| 10 |
use serde::Serialize; |
| 11 |
use tower_governor::GovernorLayer; |
| 12 |
use uuid::Uuid; |
| 13 |
|
| 14 |
use crate::{ |
| 15 |
constants, |
| 16 |
csrf::{delete_csrf, post_csrf, CsrfRouter}, |
| 17 |
db, |
| 18 |
db::scan_jobs::ScanTargetKind, |
| 19 |
error::Result, |
| 20 |
storage::FileType, |
| 21 |
AppState, |
| 22 |
}; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub(crate) async fn enqueue_s3_orphan(pool: &sqlx::PgPool, s3_key: &str, source: &'static str) { |
| 37 |
if let Err(e) = db::pending_s3_deletions::enqueue_deletions( |
| 38 |
pool, |
| 39 |
&[(s3_key.to_string(), "main".to_string())], |
| 40 |
source, |
| 41 |
) |
| 42 |
.await |
| 43 |
{ |
| 44 |
tracing::warn!(error = ?e, key = %s3_key, source = %source, "failed to enqueue orphan S3 key"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub fn storage_routes() -> CsrfRouter<AppState> { |
| 53 |
let upload_rate_limit = crate::helpers::rate_limiter_ms(constants::UPLOAD_RATE_LIMIT_MS, constants::UPLOAD_RATE_LIMIT_BURST); |
| 54 |
|
| 55 |
let upload_routes = CsrfRouter::new() |
| 56 |
.route("/api/upload/presign", post_csrf(uploads::presign_upload)) |
| 57 |
.route("/api/upload/confirm", post_csrf(uploads::confirm_upload)) |
| 58 |
.route("/api/versions/{version_id}/upload/presign", post_csrf(versions::version_presign_upload)) |
| 59 |
.route("/api/versions/{version_id}/upload/confirm", post_csrf(versions::version_confirm_upload)) |
| 60 |
.route("/api/projects/image/presign", post_csrf(images::project_image_presign)) |
| 61 |
.route("/api/projects/image/confirm", post_csrf(images::project_image_confirm)) |
| 62 |
.route("/api/items/image/presign", post_csrf(images::item_image_presign)) |
| 63 |
.route("/api/items/image/confirm", post_csrf(images::item_image_confirm)) |
| 64 |
.route("/api/media/presign", post_csrf(media::media_presign)) |
| 65 |
.route("/api/media/confirm", post_csrf(media::media_confirm)) |
| 66 |
.route_get("/api/media", get(media::media_list)) |
| 67 |
.route_get("/api/media/folders", get(media::media_folders)) |
| 68 |
.route("/api/media/{id}", delete_csrf(media::media_delete)) |
| 69 |
.route_layer(GovernorLayer { |
| 70 |
config: upload_rate_limit, |
| 71 |
}); |
| 72 |
|
| 73 |
let stream_rate_limit = crate::helpers::rate_limiter_ms(constants::STREAM_RATE_LIMIT_MS, constants::STREAM_RATE_LIMIT_BURST); |
| 74 |
|
| 75 |
let stream_routes = CsrfRouter::new() |
| 76 |
.route_get("/api/stream/{item_id}", get(downloads::stream_url)) |
| 77 |
.route_get("/api/versions/{version_id}/download", get(downloads::version_download)) |
| 78 |
.route_layer(GovernorLayer { |
| 79 |
config: stream_rate_limit, |
| 80 |
}); |
| 81 |
|
| 82 |
upload_routes.merge(stream_routes) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#[derive(Debug, Serialize)] |
| 91 |
pub struct PresignUploadResponse { |
| 92 |
pub upload_url: String, |
| 93 |
pub s3_key: String, |
| 94 |
pub expires_in: u64, |
| 95 |
|
| 96 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 97 |
pub cache_control: Option<String>, |
| 98 |
|
| 99 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 100 |
pub max_file_bytes: Option<u64>, |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
#[derive(Debug, Serialize)] |
| 105 |
pub struct ConfirmUploadResponse { |
| 106 |
pub success: bool, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 111 |
pub pending_review: Option<bool>, |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
pub(crate) enum CommitTarget { |
| 125 |
|
| 126 |
Item(db::ItemId), |
| 127 |
|
| 128 |
Version(db::VersionId), |
| 129 |
|
| 130 |
Media(db::MediaFileId), |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
ProjectImage(db::ProjectId), |
| 135 |
|
| 136 |
ItemImage(db::ItemId), |
| 137 |
|
| 138 |
ContentInsertion(db::ContentInsertionId), |
| 139 |
} |
| 140 |
|
| 141 |
impl CommitTarget { |
| 142 |
fn kind(&self) -> ScanTargetKind { |
| 143 |
match self { |
| 144 |
CommitTarget::Item(_) => ScanTargetKind::Item, |
| 145 |
CommitTarget::ItemImage(_) => ScanTargetKind::ItemImage, |
| 146 |
CommitTarget::Version(_) => ScanTargetKind::Version, |
| 147 |
CommitTarget::Media(_) => ScanTargetKind::Media, |
| 148 |
CommitTarget::ProjectImage(_) => ScanTargetKind::ProjectImage, |
| 149 |
CommitTarget::ContentInsertion(_) => ScanTargetKind::ContentInsertion, |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
fn target_uuid(&self) -> Uuid { |
| 154 |
match self { |
| 155 |
CommitTarget::Item(id) | CommitTarget::ItemImage(id) => (*id).into(), |
| 156 |
CommitTarget::Version(id) => (*id).into(), |
| 157 |
CommitTarget::Media(id) => (*id).into(), |
| 158 |
CommitTarget::ProjectImage(id) => (*id).into(), |
| 159 |
CommitTarget::ContentInsertion(id) => (*id).into(), |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
pub(crate) async fn commit_upload( |
| 182 |
state: &AppState, |
| 183 |
target: CommitTarget, |
| 184 |
s3_key: &str, |
| 185 |
file_type: FileType, |
| 186 |
user_id: db::UserId, |
| 187 |
file_size_bytes: i64, |
| 188 |
) -> Result<db::FileScanStatus> { |
| 189 |
let scan_status = enqueue_scan_for( |
| 190 |
state, |
| 191 |
target.kind(), |
| 192 |
target.target_uuid(), |
| 193 |
s3_key, |
| 194 |
file_type, |
| 195 |
user_id, |
| 196 |
file_size_bytes, |
| 197 |
) |
| 198 |
.await?; |
| 199 |
match target { |
| 200 |
CommitTarget::Item(id) | CommitTarget::ItemImage(id) => { |
| 201 |
db::scanning::update_item_scan_status(&state.db, id, scan_status).await?; |
| 202 |
} |
| 203 |
CommitTarget::Version(id) => { |
| 204 |
db::scanning::update_version_scan_status(&state.db, id, scan_status).await?; |
| 205 |
} |
| 206 |
CommitTarget::Media(id) => { |
| 207 |
db::scanning::update_media_file_scan_status(&state.db, id, scan_status).await?; |
| 208 |
} |
| 209 |
CommitTarget::ProjectImage(_) | CommitTarget::ContentInsertion(_) => { |
| 210 |
|
| 211 |
} |
| 212 |
} |
| 213 |
Ok(scan_status) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
pub(crate) async fn commit_rescan( |
| 222 |
state: &AppState, |
| 223 |
target: CommitTarget, |
| 224 |
s3_key: &str, |
| 225 |
file_type: FileType, |
| 226 |
user_id: db::UserId, |
| 227 |
file_size_bytes: i64, |
| 228 |
) -> Result<db::FileScanStatus> { |
| 229 |
enqueue_scan_for( |
| 230 |
state, |
| 231 |
target.kind(), |
| 232 |
target.target_uuid(), |
| 233 |
s3_key, |
| 234 |
file_type, |
| 235 |
user_id, |
| 236 |
file_size_bytes, |
| 237 |
) |
| 238 |
.await?; |
| 239 |
let pending = db::FileScanStatus::Pending; |
| 240 |
match target { |
| 241 |
CommitTarget::Item(id) | CommitTarget::ItemImage(id) => { |
| 242 |
db::scanning::update_item_scan_status(&state.db, id, pending).await?; |
| 243 |
} |
| 244 |
CommitTarget::Version(id) => { |
| 245 |
db::scanning::update_version_scan_status(&state.db, id, pending).await?; |
| 246 |
} |
| 247 |
CommitTarget::Media(id) => { |
| 248 |
db::scanning::update_media_file_scan_status(&state.db, id, pending).await?; |
| 249 |
} |
| 250 |
CommitTarget::ProjectImage(_) | CommitTarget::ContentInsertion(_) => { |
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
Ok(pending) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
async fn enqueue_scan_for( |
| 266 |
state: &AppState, |
| 267 |
target_kind: ScanTargetKind, |
| 268 |
target_id: Uuid, |
| 269 |
s3_key: &str, |
| 270 |
file_type: FileType, |
| 271 |
user_id: db::UserId, |
| 272 |
file_size_bytes: i64, |
| 273 |
) -> Result<db::FileScanStatus> { |
| 274 |
if state.scanner.is_none() { |
| 275 |
let is_trusted = db::users::is_upload_trusted(&state.db, user_id).await?; |
| 276 |
return Ok(if is_trusted { |
| 277 |
db::FileScanStatus::Clean |
| 278 |
} else { |
| 279 |
db::FileScanStatus::HeldForReview |
| 280 |
}); |
| 281 |
} |
| 282 |
|
| 283 |
db::scan_jobs::enqueue( |
| 284 |
&state.db, |
| 285 |
target_kind, |
| 286 |
target_id, |
| 287 |
s3_key, |
| 288 |
file_type, |
| 289 |
user_id, |
| 290 |
file_size_bytes, |
| 291 |
) |
| 292 |
.await?; |
| 293 |
|
| 294 |
Ok(db::FileScanStatus::Pending) |
| 295 |
} |
| 296 |
|