| 1 |
|
| 2 |
|
| 3 |
use axum::{ |
| 4 |
Json, |
| 5 |
extract::{Path, State}, |
| 6 |
response::{IntoResponse, Redirect}, |
| 7 |
}; |
| 8 |
use serde::Serialize; |
| 9 |
use sqlx::PgPool; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
AppStorage, |
| 13 |
auth::MaybeUserVerified, |
| 14 |
db::{self, ContentData, ItemId, VersionId}, |
| 15 |
error::{AppError, Result, ResultExt}, |
| 16 |
pricing, |
| 17 |
}; |
| 18 |
|
| 19 |
|
| 20 |
#[derive(Debug, Serialize)] |
| 21 |
pub(super) struct StreamUrlResponse { |
| 22 |
pub stream_url: String, |
| 23 |
pub expires_in: u64, |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
async fn resolve_content_url( |
| 39 |
s3: &dyn crate::storage::StorageBackend, |
| 40 |
s3_key: &str, |
| 41 |
expiry_secs: u64, |
| 42 |
) -> Result<(String, u64)> { |
| 43 |
let url = s3 |
| 44 |
.presign_download( |
| 45 |
&crate::storage::S3Key::from_stored(s3_key), |
| 46 |
Some(expiry_secs), |
| 47 |
) |
| 48 |
.await |
| 49 |
.context("presign download for content")?; |
| 50 |
Ok((url, expiry_secs)) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[tracing::instrument(skip_all, name = "storage::stream_url", fields(item_id))] |
| 61 |
pub(super) async fn stream_url( |
| 62 |
State(db): State<PgPool>, |
| 63 |
State(storage): State<AppStorage>, |
| 64 |
MaybeUserVerified(maybe_user): MaybeUserVerified, |
| 65 |
Path(item_id): Path<ItemId>, |
| 66 |
) -> Result<impl IntoResponse> { |
| 67 |
tracing::Span::current().record("item_id", tracing::field::display(&item_id)); |
| 68 |
|
| 69 |
let s3 = storage.require_s3()?; |
| 70 |
|
| 71 |
let item = db::items::get_item_by_id(&db, item_id) |
| 72 |
.await? |
| 73 |
.ok_or(AppError::NotFound)?; |
| 74 |
|
| 75 |
|
| 76 |
let user_id = maybe_user.as_ref().map(|u| u.id); |
| 77 |
let access = db::items::check_item_access(&db, item_id, user_id) |
| 78 |
.await? |
| 79 |
.ok_or(AppError::NotFound)?; |
| 80 |
let is_creator = user_id.is_some_and(|uid| uid == access.owner_id); |
| 81 |
|
| 82 |
|
| 83 |
if !item.is_public && !is_creator { |
| 84 |
return Err(AppError::NotFound); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
if item.scan_status == db::FileScanStatus::Quarantined |
| 92 |
|| (item.scan_status != db::FileScanStatus::Clean && !is_creator) |
| 93 |
{ |
| 94 |
return Err(AppError::NotFound); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
let (s3_key, duration_seconds) = match item.content() { |
| 99 |
ContentData::Audio { |
| 100 |
audio_s3_key: Some(key), |
| 101 |
duration_seconds, |
| 102 |
.. |
| 103 |
} => (key, duration_seconds), |
| 104 |
ContentData::Video { |
| 105 |
video_s3_key: Some(key), |
| 106 |
duration_seconds, |
| 107 |
.. |
| 108 |
} => (key, duration_seconds), |
| 109 |
_ => return Err(AppError::NotFound), |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
let item_pricing = pricing::for_item(&item); |
| 114 |
let is_free = item_pricing.is_free(); |
| 115 |
|
| 116 |
if !is_free && !is_creator { |
| 117 |
if maybe_user.is_none() { |
| 118 |
return Err(AppError::Unauthorized); |
| 119 |
} |
| 120 |
let ctx = pricing::AccessContext { |
| 121 |
is_creator: false, |
| 122 |
has_purchased: access.has_purchased, |
| 123 |
subscription: access.subscription, |
| 124 |
}; |
| 125 |
if !item_pricing.can_access(&ctx) && !access.has_bundle_access { |
| 126 |
return Err(AppError::Forbidden); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
let expiry_secs = match duration_seconds { |
| 136 |
Some(duration) => { |
| 137 |
let nonneg = duration.max(0) as u64; |
| 138 |
nonneg.saturating_mul(2).clamp(3600, 86_400) |
| 139 |
} |
| 140 |
None => 3600, |
| 141 |
}; |
| 142 |
let (stream_url, expires_in) = resolve_content_url(s3.as_ref(), &s3_key, expiry_secs).await?; |
| 143 |
|
| 144 |
|
| 145 |
db::items::increment_play_count(&db, item_id).await?; |
| 146 |
|
| 147 |
|
| 148 |
if let Some(ref user) = maybe_user { |
| 149 |
let _ = db::items::record_unique_play(&db, user.id, item_id).await; |
| 150 |
} |
| 151 |
|
| 152 |
Ok(Json(StreamUrlResponse { |
| 153 |
stream_url, |
| 154 |
expires_in, |
| 155 |
})) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[tracing::instrument(skip_all, name = "storage::version_download", fields(version_id))] |
| 181 |
pub(super) async fn version_download( |
| 182 |
State(db): State<PgPool>, |
| 183 |
State(storage): State<AppStorage>, |
| 184 |
MaybeUserVerified(maybe_user): MaybeUserVerified, |
| 185 |
Path(version_id): Path<VersionId>, |
| 186 |
) -> Result<impl IntoResponse> { |
| 187 |
tracing::Span::current().record("version_id", tracing::field::display(&version_id)); |
| 188 |
let s3 = storage.require_s3()?; |
| 189 |
|
| 190 |
let version = db::versions::get_version_by_id(&db, version_id) |
| 191 |
.await? |
| 192 |
.ok_or(AppError::NotFound)?; |
| 193 |
|
| 194 |
let s3_key = version.s3_key.as_ref().ok_or(AppError::NotFound)?; |
| 195 |
|
| 196 |
let item = db::items::get_item_by_id(&db, version.item_id) |
| 197 |
.await? |
| 198 |
.ok_or(AppError::NotFound)?; |
| 199 |
|
| 200 |
|
| 201 |
let user_id = maybe_user.as_ref().map(|u| u.id); |
| 202 |
let access = db::items::check_item_access(&db, version.item_id, user_id) |
| 203 |
.await? |
| 204 |
.ok_or(AppError::NotFound)?; |
| 205 |
let is_creator = user_id.is_some_and(|uid| uid == access.owner_id); |
| 206 |
|
| 207 |
|
| 208 |
if !item.is_public && !is_creator { |
| 209 |
return Err(AppError::NotFound); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if version.scan_status == db::FileScanStatus::Quarantined |
| 216 |
|| (version.scan_status != db::FileScanStatus::Clean && !is_creator) |
| 217 |
{ |
| 218 |
return Err(AppError::NotFound); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
let item_pricing = pricing::for_item(&item); |
| 223 |
let is_free = item_pricing.is_free(); |
| 224 |
if !is_free && !is_creator { |
| 225 |
if maybe_user.is_none() { |
| 226 |
return Err(AppError::Unauthorized); |
| 227 |
} |
| 228 |
let ctx = pricing::AccessContext { |
| 229 |
is_creator: false, |
| 230 |
has_purchased: access.has_purchased, |
| 231 |
subscription: access.subscription, |
| 232 |
}; |
| 233 |
if !item_pricing.can_access(&ctx) && !access.has_bundle_access { |
| 234 |
return Err(AppError::Forbidden); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
let (download_url, _expires_in) = resolve_content_url(s3.as_ref(), s3_key, 3600).await?; |
| 239 |
|
| 240 |
|
| 241 |
db::versions::increment_download_count(&db, version_id).await?; |
| 242 |
db::items::increment_item_download_count(&db, version.item_id).await?; |
| 243 |
|
| 244 |
|
| 245 |
if let Some(ref user) = maybe_user { |
| 246 |
let _ = db::versions::record_user_download(&db, user.id, version.item_id, version_id).await; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
Ok(Redirect::to(&download_url)) |
| 253 |
} |
| 254 |
|