2022-12-23 18:56:15 +00:00
|
|
|
mod backgrounded;
|
2024-02-24 04:12:19 +00:00
|
|
|
mod blurhash;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod bytes_stream;
|
|
|
|
mod config;
|
|
|
|
mod details;
|
2023-07-13 03:12:21 +00:00
|
|
|
mod discover;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod either;
|
|
|
|
mod error;
|
2023-09-02 01:50:10 +00:00
|
|
|
mod error_code;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod exiftool;
|
|
|
|
mod ffmpeg;
|
|
|
|
mod file;
|
2024-02-26 21:43:30 +00:00
|
|
|
mod file_path;
|
2023-07-12 04:11:23 +00:00
|
|
|
mod formats;
|
2023-09-05 02:51:27 +00:00
|
|
|
mod future;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod generate;
|
2024-05-01 19:46:29 +00:00
|
|
|
mod http1;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod ingest;
|
2024-02-04 20:26:18 +00:00
|
|
|
mod init_metrics;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod init_tracing;
|
|
|
|
mod magick;
|
|
|
|
mod middleware;
|
2023-07-17 03:07:42 +00:00
|
|
|
mod migrate_store;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod process;
|
|
|
|
mod processor;
|
|
|
|
mod queue;
|
|
|
|
mod range;
|
2023-09-24 16:54:16 +00:00
|
|
|
mod read;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod repo;
|
2023-08-13 19:12:38 +00:00
|
|
|
mod repo_04;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod serde_str;
|
2024-02-03 19:31:54 +00:00
|
|
|
mod state;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod store;
|
|
|
|
mod stream;
|
2023-09-04 02:30:47 +00:00
|
|
|
mod sync;
|
2024-01-31 23:47:42 +00:00
|
|
|
mod tls;
|
2022-12-23 18:56:15 +00:00
|
|
|
mod tmp_file;
|
|
|
|
|
|
|
|
use actix_form_data::{Field, Form, FormData, Multipart, Value};
|
|
|
|
use actix_web::{
|
|
|
|
guard,
|
|
|
|
http::header::{CacheControl, CacheDirective, LastModified, Range, ACCEPT_RANGES},
|
|
|
|
web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer,
|
|
|
|
};
|
2023-08-23 16:59:42 +00:00
|
|
|
use futures_core::Stream;
|
2023-07-22 21:47:59 +00:00
|
|
|
use metrics_exporter_prometheus::PrometheusBuilder;
|
2023-07-21 21:58:31 +00:00
|
|
|
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
|
|
|
|
use reqwest_tracing::TracingMiddleware;
|
2024-02-04 00:42:34 +00:00
|
|
|
use rustls_channel_resolver::ChannelSender;
|
2022-12-23 18:56:15 +00:00
|
|
|
use rusty_s3::UrlStyle;
|
|
|
|
use std::{
|
2023-09-02 23:30:45 +00:00
|
|
|
marker::PhantomData,
|
2022-12-23 18:56:15 +00:00
|
|
|
path::Path,
|
2024-03-27 21:57:22 +00:00
|
|
|
rc::Rc,
|
2023-09-24 20:32:00 +00:00
|
|
|
sync::{Arc, OnceLock},
|
2023-07-17 03:07:42 +00:00
|
|
|
time::{Duration, SystemTime},
|
2022-12-23 18:56:15 +00:00
|
|
|
};
|
2023-09-10 22:55:13 +00:00
|
|
|
use streem::IntoStreamer;
|
2022-12-23 18:56:15 +00:00
|
|
|
use tokio::sync::Semaphore;
|
2023-09-05 02:51:27 +00:00
|
|
|
use tracing::Instrument;
|
2022-12-23 18:56:15 +00:00
|
|
|
use tracing_actix_web::TracingLogger;
|
|
|
|
|
|
|
|
use self::{
|
|
|
|
backgrounded::Backgrounded,
|
2023-07-13 18:48:59 +00:00
|
|
|
config::{Configuration, Operation},
|
2024-06-04 02:38:41 +00:00
|
|
|
details::{ApiDetails, Details, HumanDate},
|
2022-12-23 18:56:15 +00:00
|
|
|
either::Either,
|
|
|
|
error::{Error, UploadError},
|
2023-07-22 17:47:32 +00:00
|
|
|
formats::InputProcessableFormat,
|
2024-06-04 02:38:41 +00:00
|
|
|
future::{WithPollTimer, WithTimeout},
|
2022-12-23 18:56:15 +00:00
|
|
|
ingest::Session,
|
|
|
|
init_tracing::init_tracing,
|
2024-06-04 02:38:41 +00:00
|
|
|
magick::ArcPolicyDir,
|
|
|
|
middleware::{Deadline, Internal, Log, Metrics, Payload},
|
2023-07-17 03:07:42 +00:00
|
|
|
migrate_store::migrate_store,
|
2022-12-23 18:56:15 +00:00
|
|
|
queue::queue_generate,
|
2024-06-09 19:44:18 +00:00
|
|
|
repo::{
|
|
|
|
sled::SledRepo, Alias, ArcRepo, DeleteToken, Hash, ProxyAlreadyExists, Repo, UploadId,
|
|
|
|
UploadResult,
|
|
|
|
},
|
2022-12-23 18:56:15 +00:00
|
|
|
serde_str::Serde,
|
2024-06-04 02:38:41 +00:00
|
|
|
state::State,
|
2023-09-02 23:30:45 +00:00
|
|
|
store::{file_store::FileStore, object_store::ObjectStore, Store},
|
2024-02-22 22:02:33 +00:00
|
|
|
stream::empty,
|
2024-06-04 02:38:41 +00:00
|
|
|
sync::DropHandle,
|
2024-01-31 23:47:42 +00:00
|
|
|
tls::Tls,
|
2024-06-04 02:38:41 +00:00
|
|
|
tmp_file::{ArcTmpDir, TmpDir},
|
2022-12-23 18:56:15 +00:00
|
|
|
};
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
pub use self::config::{ConfigSource, PictRsConfiguration};
|
2022-12-26 22:35:25 +00:00
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
const MEGABYTES: usize = 1024 * 1024;
|
|
|
|
const MINUTES: u32 = 60;
|
|
|
|
const HOURS: u32 = 60 * MINUTES;
|
|
|
|
const DAYS: u32 = 24 * HOURS;
|
|
|
|
|
2023-07-06 00:06:48 +00:00
|
|
|
const NOT_FOUND_KEY: &str = "404-alias";
|
|
|
|
|
2023-09-24 20:32:00 +00:00
|
|
|
static PROCESS_SEMAPHORE: OnceLock<Semaphore> = OnceLock::new();
|
2023-09-04 02:30:47 +00:00
|
|
|
|
2023-09-24 20:32:00 +00:00
|
|
|
fn process_semaphore() -> &'static Semaphore {
|
|
|
|
PROCESS_SEMAPHORE.get_or_init(|| {
|
|
|
|
let permits = std::thread::available_parallelism()
|
|
|
|
.map(usize::from)
|
|
|
|
.unwrap_or(1)
|
|
|
|
.saturating_sub(1)
|
|
|
|
.max(1);
|
|
|
|
|
|
|
|
crate::sync::bare_semaphore(permits)
|
|
|
|
})
|
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn ensure_details<S: Store + 'static>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2022-12-23 18:56:15 +00:00
|
|
|
alias: &Alias,
|
|
|
|
) -> Result<Details, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(identifier) = state.repo.identifier_from_alias(alias).await? else {
|
2023-07-05 21:46:44 +00:00
|
|
|
return Err(UploadError::MissingAlias.into());
|
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
ensure_details_identifier(state, &identifier).await
|
2023-11-11 20:22:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 19:11:12 +00:00
|
|
|
#[tracing::instrument(skip(state))]
|
2023-11-11 20:22:12 +00:00
|
|
|
async fn ensure_details_identifier<S: Store + 'static>(
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2023-11-11 20:22:12 +00:00
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<Details, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = state.repo.details(identifier).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
if let Some(details) = details {
|
|
|
|
Ok(details)
|
|
|
|
} else {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
2024-02-03 19:31:54 +00:00
|
|
|
} else if state.config.server.danger_dummy_mode {
|
2023-11-11 20:22:12 +00:00
|
|
|
return Ok(Details::danger_dummy(formats::InternalFormat::Image(
|
|
|
|
formats::ImageFormat::Png,
|
|
|
|
)));
|
2023-07-17 19:24:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let bytes_stream = state.store.to_bytes(identifier, None, None).await?;
|
2024-02-22 22:02:33 +00:00
|
|
|
let new_details = Details::from_bytes_stream(state, bytes_stream).await?;
|
2024-02-03 19:31:54 +00:00
|
|
|
state.repo.relate_details(identifier, &new_details).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
Ok(new_details)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
struct UploadLimits {
|
2024-03-28 00:00:54 +00:00
|
|
|
max_width: Option<Serde<u16>>,
|
|
|
|
max_height: Option<Serde<u16>>,
|
|
|
|
max_area: Option<Serde<u32>>,
|
|
|
|
max_frame_count: Option<Serde<u32>>,
|
|
|
|
max_file_size: Option<Serde<usize>>,
|
|
|
|
allow_image: Serde<bool>,
|
|
|
|
allow_animation: Serde<bool>,
|
|
|
|
allow_video: Serde<bool>,
|
2024-03-27 21:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UploadLimits {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
max_width: None,
|
|
|
|
max_height: None,
|
|
|
|
max_area: None,
|
|
|
|
max_frame_count: None,
|
|
|
|
max_file_size: None,
|
2024-03-28 00:00:54 +00:00
|
|
|
allow_image: Serde::new(true),
|
|
|
|
allow_animation: Serde::new(true),
|
|
|
|
allow_video: Serde::new(true),
|
2024-03-27 21:57:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Default, Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
struct UploadQuery {
|
|
|
|
#[serde(flatten)]
|
|
|
|
limits: UploadLimits,
|
|
|
|
|
|
|
|
#[serde(with = "tuple_vec_map", flatten)]
|
|
|
|
operations: Vec<(String, String)>,
|
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
struct Upload<S>(Value<Session>, PhantomData<S>);
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<S: Store + 'static> FormData for Upload<S> {
|
2023-09-02 23:30:45 +00:00
|
|
|
type Item = Session;
|
2022-12-23 18:56:15 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
fn form(req: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = req
|
|
|
|
.app_data::<web::Data<State<S>>>()
|
|
|
|
.expect("No state in request")
|
2023-07-22 17:31:01 +00:00
|
|
|
.clone();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
let web::Query(upload_query) = web::Query::<UploadQuery>::from_query(req.query_string())
|
2024-03-28 00:00:54 +00:00
|
|
|
.map_err(UploadError::InvalidQuery)?;
|
2024-03-27 21:57:22 +00:00
|
|
|
|
|
|
|
let upload_query = Rc::new(upload_query);
|
|
|
|
|
2023-10-07 00:42:24 +00:00
|
|
|
// Create a new Multipart Form validator
|
|
|
|
//
|
|
|
|
// This form is expecting a single array field, 'images' with at most 10 files in it
|
2024-03-27 21:57:22 +00:00
|
|
|
Ok(Form::new()
|
2024-02-03 19:31:54 +00:00
|
|
|
.max_files(state.config.server.max_file_count)
|
|
|
|
.max_file_size(state.config.media.max_file_size * MEGABYTES)
|
2022-12-23 18:56:15 +00:00
|
|
|
.transform_error(transform_error)
|
|
|
|
.field(
|
|
|
|
"images",
|
|
|
|
Field::array(Field::file(move |filename, _, stream| {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = state.clone();
|
2024-03-27 21:57:22 +00:00
|
|
|
let upload_query = upload_query.clone();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::FILES, "upload" => "inline")
|
|
|
|
.increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
let span = tracing::info_span!("file-upload", ?filename);
|
|
|
|
|
|
|
|
Box::pin(
|
2023-07-13 22:42:21 +00:00
|
|
|
async move {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::from_err(stream);
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
ingest::ingest(&state, stream, None, &upload_query).await
|
2023-07-13 22:42:21 +00:00
|
|
|
}
|
2024-03-09 18:15:23 +00:00
|
|
|
.with_poll_timer("file-upload")
|
2023-07-13 22:42:21 +00:00
|
|
|
.instrument(span),
|
2022-12-23 18:56:15 +00:00
|
|
|
)
|
|
|
|
})),
|
2024-03-27 21:57:22 +00:00
|
|
|
))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error> {
|
2023-09-02 23:30:45 +00:00
|
|
|
Ok(Upload(value, PhantomData))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
struct Import<S: Store + 'static>(Value<Session>, PhantomData<S>);
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<S: Store + 'static> FormData for Import<S> {
|
2023-09-02 23:30:45 +00:00
|
|
|
type Item = Session;
|
2022-12-23 18:56:15 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
fn form(req: &actix_web::HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = req
|
|
|
|
.app_data::<web::Data<State<S>>>()
|
|
|
|
.expect("No state in request")
|
2023-07-22 17:31:01 +00:00
|
|
|
.clone();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
let web::Query(upload_query) = web::Query::<UploadQuery>::from_query(req.query_string())
|
2024-03-28 00:00:54 +00:00
|
|
|
.map_err(UploadError::InvalidQuery)?;
|
2024-03-27 21:57:22 +00:00
|
|
|
|
|
|
|
let upload_query = Rc::new(upload_query);
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
// Create a new Multipart Form validator for internal imports
|
|
|
|
//
|
|
|
|
// This form is expecting a single array field, 'images' with at most 10 files in it
|
2024-03-27 21:57:22 +00:00
|
|
|
Ok(Form::new()
|
2024-02-03 19:31:54 +00:00
|
|
|
.max_files(state.config.server.max_file_count)
|
|
|
|
.max_file_size(state.config.media.max_file_size * MEGABYTES)
|
2022-12-23 18:56:15 +00:00
|
|
|
.transform_error(transform_error)
|
|
|
|
.field(
|
|
|
|
"images",
|
|
|
|
Field::array(Field::file(move |filename, _, stream| {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = state.clone();
|
2024-03-27 21:57:22 +00:00
|
|
|
let upload_query = upload_query.clone();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::FILES, "import" => "inline")
|
|
|
|
.increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
let span = tracing::info_span!("file-import", ?filename);
|
|
|
|
|
|
|
|
Box::pin(
|
|
|
|
async move {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::from_err(stream);
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
ingest::ingest(
|
|
|
|
&state,
|
|
|
|
stream,
|
|
|
|
Some(Alias::from_existing(&filename)),
|
|
|
|
&upload_query,
|
|
|
|
)
|
|
|
|
.await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
2024-03-09 18:15:23 +00:00
|
|
|
.with_poll_timer("file-import")
|
2022-12-23 18:56:15 +00:00
|
|
|
.instrument(span),
|
|
|
|
)
|
|
|
|
})),
|
2024-03-27 21:57:22 +00:00
|
|
|
))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-09-02 23:30:45 +00:00
|
|
|
Ok(Import(value, PhantomData))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 16:45:06 +00:00
|
|
|
/// Handle responding to successful uploads
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Uploaded files", skip(value, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn upload<S: Store + 'static>(
|
2023-09-02 23:30:45 +00:00
|
|
|
Multipart(Upload(value, _)): Multipart<Upload<S>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
handle_upload(value, state).await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 16:45:06 +00:00
|
|
|
/// Handle responding to successful uploads
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Imported files", skip(value, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn import<S: Store + 'static>(
|
2023-09-02 23:30:45 +00:00
|
|
|
Multipart(Import(value, _)): Multipart<Import<S>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
handle_upload(value, state).await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 16:45:06 +00:00
|
|
|
/// Handle responding to successful uploads
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Uploaded files", skip(value, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn handle_upload<S: Store + 'static>(
|
2023-09-02 23:30:45 +00:00
|
|
|
value: Value<Session>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let images = value
|
|
|
|
.map()
|
|
|
|
.and_then(|mut m| m.remove("images"))
|
|
|
|
.and_then(|images| images.array())
|
|
|
|
.ok_or(UploadError::NoFiles)?;
|
|
|
|
|
|
|
|
let mut files = Vec::new();
|
|
|
|
let images = images
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|i| i.file())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for image in &images {
|
|
|
|
if let Some(alias) = image.result.alias() {
|
|
|
|
tracing::debug!("Uploaded {} as {:?}", image.filename, alias);
|
2023-07-26 01:08:18 +00:00
|
|
|
let delete_token = image.result.delete_token();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
files.push(serde_json::json!({
|
|
|
|
"file": alias.to_string(),
|
|
|
|
"delete_token": delete_token.to_string(),
|
2023-08-31 21:26:45 +00:00
|
|
|
"details": details.into_api_details(),
|
2022-12-23 18:56:15 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 01:08:18 +00:00
|
|
|
for image in images {
|
2022-12-23 18:56:15 +00:00
|
|
|
image.result.disarm();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Created().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"files": files
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
struct BackgroundedUpload<S: Store + 'static>(Value<Backgrounded>, PhantomData<S>);
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<S: Store + 'static> FormData for BackgroundedUpload<S> {
|
2023-09-02 23:30:45 +00:00
|
|
|
type Item = Backgrounded;
|
2022-12-23 18:56:15 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
fn form(req: &actix_web::HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = req
|
|
|
|
.app_data::<web::Data<State<S>>>()
|
|
|
|
.expect("No state in request")
|
2023-07-22 17:31:01 +00:00
|
|
|
.clone();
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
// Create a new Multipart Form validator for backgrounded uploads
|
|
|
|
//
|
|
|
|
// This form is expecting a single array field, 'images' with at most 10 files in it
|
|
|
|
Ok(Form::new()
|
2024-02-03 19:31:54 +00:00
|
|
|
.max_files(state.config.server.max_file_count)
|
|
|
|
.max_file_size(state.config.media.max_file_size * MEGABYTES)
|
2022-12-23 18:56:15 +00:00
|
|
|
.transform_error(transform_error)
|
|
|
|
.field(
|
|
|
|
"images",
|
|
|
|
Field::array(Field::file(move |filename, _, stream| {
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = state.clone();
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::FILES, "upload" => "background")
|
|
|
|
.increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
let span = tracing::info_span!("file-proxy", ?filename);
|
|
|
|
|
|
|
|
Box::pin(
|
2023-07-17 19:24:49 +00:00
|
|
|
async move {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::from_err(stream);
|
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
Backgrounded::proxy(&state, stream).await
|
2023-07-17 19:24:49 +00:00
|
|
|
}
|
2024-03-09 18:15:23 +00:00
|
|
|
.with_poll_timer("file-proxy")
|
2023-07-17 19:24:49 +00:00
|
|
|
.instrument(span),
|
2022-12-23 18:56:15 +00:00
|
|
|
)
|
|
|
|
})),
|
2024-03-27 21:57:22 +00:00
|
|
|
))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-09-02 23:30:45 +00:00
|
|
|
Ok(BackgroundedUpload(value, PhantomData))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Uploaded files", skip(value, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn upload_backgrounded<S: Store>(
|
2023-09-02 23:30:45 +00:00
|
|
|
Multipart(BackgroundedUpload(value, _)): Multipart<BackgroundedUpload<S>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2024-03-28 00:00:54 +00:00
|
|
|
upload_query: web::Query<UploadQuery>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-28 00:00:54 +00:00
|
|
|
let upload_query = upload_query.into_inner();
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
let images = value
|
|
|
|
.map()
|
|
|
|
.and_then(|mut m| m.remove("images"))
|
|
|
|
.and_then(|images| images.array())
|
|
|
|
.ok_or(UploadError::NoFiles)?;
|
|
|
|
|
|
|
|
let mut files = Vec::new();
|
|
|
|
let images = images
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|i| i.file())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
for image in &images {
|
|
|
|
let upload_id = image.result.upload_id().expect("Upload ID exists");
|
2023-09-02 23:30:45 +00:00
|
|
|
let identifier = image.result.identifier().expect("Identifier exists");
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
queue::queue_ingest(
|
|
|
|
&state.repo,
|
|
|
|
identifier,
|
|
|
|
upload_id,
|
|
|
|
None,
|
|
|
|
upload_query.clone(),
|
|
|
|
)
|
|
|
|
.await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
files.push(serde_json::json!({
|
|
|
|
"upload_id": upload_id.to_string(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
for image in images {
|
|
|
|
image.result.disarm();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"uploads": files
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct ClaimQuery {
|
|
|
|
upload_id: Serde<UploadId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Claim a backgrounded upload
|
|
|
|
#[tracing::instrument(name = "Waiting on upload", skip_all)]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn claim_upload<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
query: web::Query<ClaimQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let upload_id = Serde::into_inner(query.into_inner().upload_id);
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
match state
|
|
|
|
.repo
|
2023-09-05 02:58:57 +00:00
|
|
|
.wait(upload_id)
|
|
|
|
.with_timeout(Duration::from_secs(10))
|
|
|
|
.await
|
|
|
|
{
|
2022-12-23 18:56:15 +00:00
|
|
|
Ok(wait_res) => {
|
|
|
|
let upload_result = wait_res?;
|
2024-02-03 19:31:54 +00:00
|
|
|
state.repo.claim(upload_id).await?;
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::BACKGROUND_UPLOAD_CLAIM).increment(1);
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
match upload_result {
|
|
|
|
UploadResult::Success { alias, token } => {
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, &alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"files": [{
|
|
|
|
"file": alias.to_string(),
|
|
|
|
"delete_token": token.to_string(),
|
2023-08-31 21:26:45 +00:00
|
|
|
"details": details.into_api_details(),
|
2022-12-23 18:56:15 +00:00
|
|
|
}]
|
|
|
|
})))
|
|
|
|
}
|
2023-09-02 01:50:10 +00:00
|
|
|
UploadResult::Failure { message, code } => Ok(HttpResponse::UnprocessableEntity()
|
|
|
|
.json(&serde_json::json!({
|
2022-12-23 18:56:15 +00:00
|
|
|
"msg": message,
|
2023-09-02 01:50:10 +00:00
|
|
|
"code": code,
|
|
|
|
}))),
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => Ok(HttpResponse::NoContent().finish()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct UrlQuery {
|
|
|
|
url: String,
|
|
|
|
|
|
|
|
#[serde(default)]
|
2024-06-05 17:54:05 +00:00
|
|
|
backgrounded: Serde<bool>,
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct DownloadQuery {
|
|
|
|
#[serde(flatten)]
|
|
|
|
url_query: UrlQuery,
|
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
upload_query: UploadQuery,
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn ingest_inline<S: Store + 'static>(
|
2024-02-28 02:46:04 +00:00
|
|
|
stream: impl Stream<Item = Result<web::Bytes, Error>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2024-03-27 21:57:22 +00:00
|
|
|
upload_query: &UploadQuery,
|
2023-07-23 20:45:52 +00:00
|
|
|
) -> Result<(Alias, DeleteToken, Details), Error> {
|
2024-03-27 21:57:22 +00:00
|
|
|
let session = ingest::ingest(state, stream, None, upload_query).await?;
|
2023-07-23 20:45:52 +00:00
|
|
|
|
|
|
|
let alias = session.alias().expect("alias should exist").to_owned();
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(state, &alias).await?;
|
2023-07-23 20:45:52 +00:00
|
|
|
|
2023-07-26 01:08:18 +00:00
|
|
|
let delete_token = session.disarm();
|
2023-07-23 20:45:52 +00:00
|
|
|
|
|
|
|
Ok((alias, delete_token, details))
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
/// download an image from a URL
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Downloading file", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn download<S: Store + 'static>(
|
2024-03-28 00:00:54 +00:00
|
|
|
download_query: web::Query<DownloadQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-28 00:00:54 +00:00
|
|
|
let DownloadQuery {
|
|
|
|
url_query,
|
|
|
|
upload_query,
|
|
|
|
} = download_query.into_inner();
|
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
let stream = download_stream(&url_query.url, &state).await?;
|
2023-07-23 20:45:52 +00:00
|
|
|
|
2024-06-05 17:54:05 +00:00
|
|
|
if *url_query.backgrounded {
|
2024-03-27 21:57:22 +00:00
|
|
|
do_download_backgrounded(stream, state, upload_query).await
|
2023-07-23 20:45:52 +00:00
|
|
|
} else {
|
2024-03-27 21:57:22 +00:00
|
|
|
do_download_inline(stream, &state, &upload_query).await
|
2023-07-23 20:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
async fn download_stream<S>(
|
2023-07-23 20:45:52 +00:00
|
|
|
url: &str,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2023-09-10 22:55:13 +00:00
|
|
|
) -> Result<impl Stream<Item = Result<web::Bytes, Error>> + 'static, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let res = state.client.get(url).send().await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
if !res.status().is_success() {
|
2024-05-01 19:46:29 +00:00
|
|
|
return Err(UploadError::Download(http1::to_actix_status(res.status())).into());
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::limit(
|
2024-02-03 19:31:54 +00:00
|
|
|
state.config.media.max_file_size * MEGABYTES,
|
2023-09-10 22:55:13 +00:00
|
|
|
crate::stream::from_err(res.bytes_stream()),
|
|
|
|
);
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-07-23 20:45:52 +00:00
|
|
|
Ok(stream)
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Downloading file inline", skip(stream, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn do_download_inline<S: Store + 'static>(
|
2024-02-28 02:46:04 +00:00
|
|
|
stream: impl Stream<Item = Result<web::Bytes, Error>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: &State<S>,
|
2024-03-27 21:57:22 +00:00
|
|
|
upload_query: &UploadQuery,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::FILES, "download" => "inline").increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
let (alias, delete_token, details) = ingest_inline(stream, state, upload_query).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Created().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"files": [{
|
|
|
|
"file": alias.to_string(),
|
|
|
|
"delete_token": delete_token.to_string(),
|
2023-08-31 21:26:45 +00:00
|
|
|
"details": details.into_api_details(),
|
2022-12-23 18:56:15 +00:00
|
|
|
}]
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Downloading file in background", skip(stream, state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn do_download_backgrounded<S: Store + 'static>(
|
2024-02-28 02:41:39 +00:00
|
|
|
stream: impl Stream<Item = Result<web::Bytes, Error>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2024-03-27 21:57:22 +00:00
|
|
|
upload_query: UploadQuery,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-04 21:45:47 +00:00
|
|
|
metrics::counter!(crate::init_metrics::FILES, "download" => "background").increment(1);
|
2023-07-22 21:47:59 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let backgrounded = Backgrounded::proxy(&state, stream).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
let upload_id = backgrounded.upload_id().expect("Upload ID exists");
|
2023-09-02 23:30:45 +00:00
|
|
|
let identifier = backgrounded.identifier().expect("Identifier exists");
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-27 21:57:22 +00:00
|
|
|
queue::queue_ingest(&state.repo, identifier, upload_id, None, upload_query).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
backgrounded.disarm();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"uploads": [{
|
|
|
|
"upload_id": upload_id.to_string(),
|
|
|
|
}]
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct PageQuery {
|
|
|
|
slug: Option<String>,
|
2023-08-29 18:27:18 +00:00
|
|
|
timestamp: Option<HumanDate>,
|
2023-08-28 21:02:11 +00:00
|
|
|
limit: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct PageJson {
|
|
|
|
limit: usize,
|
|
|
|
|
2023-08-28 23:43:24 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
current: Option<String>,
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
prev: Option<String>,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
next: Option<String>,
|
|
|
|
|
|
|
|
hashes: Vec<HashJson>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct HashJson {
|
|
|
|
hex: String,
|
|
|
|
aliases: Vec<String>,
|
2023-08-31 21:26:45 +00:00
|
|
|
details: Option<ApiDetails>,
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a page of hashes
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Hash Page", skip(state))]
|
|
|
|
async fn page<S>(
|
|
|
|
state: web::Data<State<S>>,
|
2023-08-29 18:27:18 +00:00
|
|
|
web::Query(PageQuery {
|
|
|
|
slug,
|
|
|
|
timestamp,
|
|
|
|
limit,
|
|
|
|
}): web::Query<PageQuery>,
|
2023-08-28 21:02:11 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let limit = limit.unwrap_or(20);
|
|
|
|
|
2023-08-29 18:27:18 +00:00
|
|
|
let page = if let Some(timestamp) = timestamp {
|
2024-02-03 19:31:54 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.hash_page_by_date(timestamp.timestamp, limit)
|
|
|
|
.await?
|
2023-08-29 18:27:18 +00:00
|
|
|
} else {
|
2024-02-03 19:31:54 +00:00
|
|
|
state.repo.hash_page(slug, limit).await?
|
2023-08-29 18:27:18 +00:00
|
|
|
};
|
2023-08-28 21:02:11 +00:00
|
|
|
|
|
|
|
let mut hashes = Vec::with_capacity(page.hashes.len());
|
|
|
|
|
|
|
|
for hash in &page.hashes {
|
|
|
|
let hex = hash.to_hex();
|
2024-02-04 00:18:13 +00:00
|
|
|
let aliases = state
|
|
|
|
.repo
|
2023-09-01 23:41:04 +00:00
|
|
|
.aliases_for_hash(hash.clone())
|
2023-08-28 21:02:11 +00:00
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| a.to_string())
|
|
|
|
.collect();
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier = state.repo.identifier(hash.clone()).await?;
|
2023-08-28 21:02:11 +00:00
|
|
|
let details = if let Some(identifier) = identifier {
|
2024-02-03 19:31:54 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.details(&identifier)
|
2023-08-31 21:26:45 +00:00
|
|
|
.await?
|
|
|
|
.map(|d| d.into_api_details())
|
2023-08-28 21:02:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
hashes.push(HashJson {
|
|
|
|
hex,
|
|
|
|
aliases,
|
|
|
|
details,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let page = PageJson {
|
|
|
|
limit: page.limit,
|
2023-08-29 17:53:29 +00:00
|
|
|
current: page.current(),
|
2023-08-28 21:02:11 +00:00
|
|
|
prev: page.prev(),
|
|
|
|
next: page.next(),
|
|
|
|
hashes,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"page": page,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
/// Delete aliases and files
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Deleting file", skip(state))]
|
|
|
|
async fn delete<S>(
|
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
path_entries: web::Path<(String, String)>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
let (token, alias) = path_entries.into_inner();
|
|
|
|
|
|
|
|
let token = DeleteToken::from_existing(&token);
|
|
|
|
let alias = Alias::from_existing(&alias);
|
|
|
|
|
2023-10-04 17:11:29 +00:00
|
|
|
// delete alias inline
|
2024-02-03 19:31:54 +00:00
|
|
|
queue::cleanup::alias(&state.repo, alias, token).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
}
|
|
|
|
|
2023-07-23 16:44:41 +00:00
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum ProcessSource {
|
|
|
|
Source { src: Serde<Alias> },
|
|
|
|
Alias { alias: Serde<Alias> },
|
|
|
|
Proxy { proxy: url::Url },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
struct ProcessQuery {
|
|
|
|
#[serde(flatten)]
|
|
|
|
source: ProcessSource,
|
|
|
|
|
|
|
|
#[serde(with = "tuple_vec_map", flatten)]
|
|
|
|
operations: Vec<(String, String)>,
|
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
fn prepare_process(
|
2023-07-22 17:31:01 +00:00
|
|
|
config: &Configuration,
|
2023-07-23 16:44:41 +00:00
|
|
|
operations: Vec<(String, String)>,
|
2022-12-23 18:56:15 +00:00
|
|
|
ext: &str,
|
2024-03-31 21:00:23 +00:00
|
|
|
) -> Result<(InputProcessableFormat, String, Vec<String>), Error> {
|
2022-12-23 18:56:15 +00:00
|
|
|
let operations = operations
|
|
|
|
.into_iter()
|
2023-07-22 17:31:01 +00:00
|
|
|
.filter(|(k, _)| config.media.filters.contains(&k.to_lowercase()))
|
2022-12-23 18:56:15 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let format = ext
|
2023-07-13 03:12:21 +00:00
|
|
|
.parse::<InputProcessableFormat>()
|
2023-07-09 20:07:49 +00:00
|
|
|
.map_err(|_| UploadError::UnsupportedProcessExtension)?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-31 21:23:34 +00:00
|
|
|
let (variant, variant_args) = self::processor::build_chain(&operations, &format.to_string())?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-31 21:23:34 +00:00
|
|
|
Ok((format, variant, variant_args))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Fetching derived details", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn process_details<S: Store>(
|
2023-07-23 16:44:41 +00:00
|
|
|
web::Query(ProcessQuery { source, operations }): web::Query<ProcessQuery>,
|
2022-12-23 18:56:15 +00:00
|
|
|
ext: web::Path<String>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(source.into(), &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-03-31 21:00:23 +00:00
|
|
|
let (_, variant, _) = prepare_process(&state.config, operations, ext.as_str())?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
let hash = state
|
|
|
|
.repo
|
|
|
|
.hash(&alias)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
2023-07-05 21:46:44 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if !state.config.server.read_only {
|
|
|
|
state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.accessed_variant(hash.clone(), variant.clone())
|
2023-08-16 00:19:03 +00:00
|
|
|
.await?;
|
2023-07-22 23:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier = state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.variant_identifier(hash, variant)
|
2022-12-23 18:56:15 +00:00
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = state.repo.details(&identifier).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
let details = details.ok_or(UploadError::NoFiles)?;
|
|
|
|
|
2023-08-31 21:26:45 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&details.into_api_details()))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn not_found_hash(repo: &ArcRepo) -> Result<Option<(Alias, Hash)>, Error> {
|
2023-07-06 00:06:48 +00:00
|
|
|
let Some(not_found) = repo.get(NOT_FOUND_KEY).await? else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
2023-07-07 18:33:27 +00:00
|
|
|
let Some(alias) = Alias::from_slice(not_found.as_ref()) else {
|
|
|
|
tracing::warn!("Couldn't parse not-found alias");
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-07-06 00:06:48 +00:00
|
|
|
|
2023-07-07 18:33:27 +00:00
|
|
|
let Some(hash) = repo.hash(&alias).await? else {
|
|
|
|
tracing::warn!("No hash found for not-found alias");
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some((alias, hash)))
|
2023-07-06 00:06:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
/// Process files
|
2024-03-30 14:36:31 +00:00
|
|
|
#[tracing::instrument(name = "Serving processed image", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn process<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
web::Query(ProcessQuery { source, operations }): web::Query<ProcessQuery>,
|
2022-12-23 18:56:15 +00:00
|
|
|
ext: web::Path<String>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = proxy_alias_from_query(source.into(), &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-03-31 21:23:34 +00:00
|
|
|
let (format, variant, variant_args) = prepare_process(&state.config, operations, ext.as_str())?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let (hash, alias, not_found) = if let Some(hash) = state.repo.hash(&alias).await? {
|
2023-07-06 00:06:48 +00:00
|
|
|
(hash, alias, false)
|
|
|
|
} else {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some((alias, hash)) = not_found_hash(&state.repo).await? else {
|
2023-07-06 00:06:48 +00:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
|
|
|
|
|
|
|
(hash, alias, true)
|
2023-07-05 21:46:44 +00:00
|
|
|
};
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if !state.config.server.read_only {
|
|
|
|
state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.accessed_variant(hash.clone(), variant.clone())
|
2023-08-16 21:09:40 +00:00
|
|
|
.await?;
|
2023-07-22 23:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let identifier_opt = state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.variant_identifier(hash.clone(), variant.clone())
|
2024-02-04 00:18:13 +00:00
|
|
|
.await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-22 22:02:33 +00:00
|
|
|
let (details, identifier) = if let Some(identifier) = identifier_opt {
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details_identifier(&state, &identifier).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-22 22:02:33 +00:00
|
|
|
(details, identifier)
|
|
|
|
} else {
|
|
|
|
if state.config.server.read_only {
|
|
|
|
return Err(UploadError::ReadOnly.into());
|
2023-07-14 19:53:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 01:26:15 +00:00
|
|
|
queue_generate(&state.repo, format, alias, variant.clone(), variant_args).await?;
|
|
|
|
|
|
|
|
let mut attempts = 0;
|
|
|
|
loop {
|
|
|
|
if attempts > 6 {
|
|
|
|
return Err(UploadError::ProcessTimeout.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = state
|
|
|
|
.repo
|
|
|
|
.variant_waiter(hash.clone(), variant.clone())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let opt = generate::wait_timeout(
|
|
|
|
hash.clone(),
|
|
|
|
variant.clone(),
|
|
|
|
entry,
|
|
|
|
&state,
|
|
|
|
Duration::from_secs(5),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if let Some(tuple) = opt {
|
|
|
|
break tuple;
|
|
|
|
}
|
|
|
|
|
|
|
|
attempts += 1;
|
|
|
|
}
|
2024-02-22 22:02:33 +00:00
|
|
|
};
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-22 22:02:33 +00:00
|
|
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
|
|
|
return Ok(HttpResponse::SeeOther()
|
|
|
|
.insert_header((actix_web::http::header::LOCATION, public_url.as_str()))
|
|
|
|
.finish());
|
2023-07-17 19:24:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 22:02:33 +00:00
|
|
|
ranged_file_resp(&state.store, identifier, range, details, not_found).await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Serving processed image headers", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn process_head<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
web::Query(ProcessQuery { source, operations }): web::Query<ProcessQuery>,
|
2022-12-23 18:56:15 +00:00
|
|
|
ext: web::Path<String>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2023-07-23 16:44:41 +00:00
|
|
|
let alias = match source {
|
|
|
|
ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => {
|
|
|
|
Serde::into_inner(alias)
|
|
|
|
}
|
2023-07-23 17:10:36 +00:00
|
|
|
ProcessSource::Proxy { proxy } => {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(alias) = state.repo.related(proxy).await? else {
|
2023-07-23 17:10:36 +00:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
|
|
|
alias
|
|
|
|
}
|
2023-07-23 16:44:41 +00:00
|
|
|
};
|
|
|
|
|
2024-03-31 21:00:23 +00:00
|
|
|
let (_, variant, _) = prepare_process(&state.config, operations, ext.as_str())?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(hash) = state.repo.hash(&alias).await? else {
|
2023-07-05 21:46:44 +00:00
|
|
|
// Invalid alias
|
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if !state.config.server.read_only {
|
2024-02-04 00:18:13 +00:00
|
|
|
state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.accessed_variant(hash.clone(), variant.clone())
|
2023-08-16 21:09:40 +00:00
|
|
|
.await?;
|
2023-07-22 23:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-31 21:00:23 +00:00
|
|
|
let identifier_opt = state.repo.variant_identifier(hash.clone(), variant).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
if let Some(identifier) = identifier_opt {
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details_identifier(&state, &identifier).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
2023-07-14 19:53:37 +00:00
|
|
|
return Ok(HttpResponse::SeeOther()
|
|
|
|
.insert_header((actix_web::http::header::LOCATION, public_url.as_str()))
|
|
|
|
.finish());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
return ranged_file_head_resp(&state.store, identifier, range, details).await;
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::NotFound().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process files
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Spawning image process", skip(state))]
|
2024-04-01 01:26:15 +00:00
|
|
|
async fn process_backgrounded<S: Store + 'static>(
|
2023-07-23 16:44:41 +00:00
|
|
|
web::Query(ProcessQuery { source, operations }): web::Query<ProcessQuery>,
|
2022-12-23 18:56:15 +00:00
|
|
|
ext: web::Path<String>,
|
2024-02-04 00:18:13 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2023-07-23 16:44:41 +00:00
|
|
|
let source = match source {
|
|
|
|
ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => {
|
|
|
|
Serde::into_inner(alias)
|
|
|
|
}
|
2023-07-23 17:10:36 +00:00
|
|
|
ProcessSource::Proxy { proxy } => {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(alias) = state.repo.related(proxy).await? else {
|
2023-07-23 17:10:36 +00:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
|
|
|
alias
|
|
|
|
}
|
2023-07-23 16:44:41 +00:00
|
|
|
};
|
|
|
|
|
2024-04-01 01:26:15 +00:00
|
|
|
let (target_format, variant, variant_args) =
|
2024-02-03 19:31:54 +00:00
|
|
|
prepare_process(&state.config, operations, ext.as_str())?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(hash) = state.repo.hash(&source).await? else {
|
2023-07-05 21:46:44 +00:00
|
|
|
// Invalid alias
|
|
|
|
return Ok(HttpResponse::BadRequest().finish());
|
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let identifier_opt = state
|
|
|
|
.repo
|
2024-03-31 21:00:23 +00:00
|
|
|
.variant_identifier(hash.clone(), variant.clone())
|
2024-02-03 19:31:54 +00:00
|
|
|
.await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
if identifier_opt.is_some() {
|
|
|
|
return Ok(HttpResponse::Accepted().finish());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2024-04-01 01:26:15 +00:00
|
|
|
queue_generate(&state.repo, target_format, source, variant, variant_args).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().finish())
|
|
|
|
}
|
|
|
|
|
2023-07-23 16:44:41 +00:00
|
|
|
/// Fetch file details
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Fetching query details", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn details_query<S: Store + 'static>(
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, &alias).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&details.into_api_details()))
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
/// Fetch file details
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Fetching details", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn details<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
alias: web::Path<Serde<Alias>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, &alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-31 21:26:45 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&details.into_api_details()))
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-23 16:44:41 +00:00
|
|
|
/// Serve files based on alias query
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Serving file query", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn serve_query<S: Store + 'static>(
|
2023-07-23 16:44:41 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = proxy_alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
do_serve(range, alias, state).await
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
/// Serve files
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Serving file", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn serve<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
|
|
|
alias: web::Path<Serde<Alias>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
do_serve(range, Serde::into_inner(alias.into_inner()), state).await
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn do_serve<S: Store + 'static>(
|
2023-07-23 16:44:41 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2023-07-23 17:10:36 +00:00
|
|
|
alias: Alias,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let (hash, alias, not_found) = if let Some(hash) = state.repo.hash(&alias).await? {
|
2023-07-23 17:10:36 +00:00
|
|
|
(hash, alias, false)
|
2023-07-06 00:06:48 +00:00
|
|
|
} else {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some((alias, hash)) = not_found_hash(&state.repo).await? else {
|
2023-07-06 00:06:48 +00:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
|
|
|
|
|
|
|
(hash, alias, true)
|
2023-07-05 21:46:44 +00:00
|
|
|
};
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(identifier) = state.repo.identifier(hash.clone()).await? else {
|
2023-09-24 16:54:16 +00:00
|
|
|
tracing::warn!("Original File identifier for hash {hash:?} is missing, queue cleanup task",);
|
2024-02-03 19:31:54 +00:00
|
|
|
crate::queue::cleanup_hash(&state.repo, hash).await?;
|
2023-07-07 18:17:26 +00:00
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
2023-07-06 00:06:48 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, &alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
2023-07-14 19:53:37 +00:00
|
|
|
return Ok(HttpResponse::SeeOther()
|
|
|
|
.insert_header((actix_web::http::header::LOCATION, public_url.as_str()))
|
|
|
|
.finish());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
ranged_file_resp(&state.store, identifier, range, details, not_found).await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Serving query file headers", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn serve_query_head<S: Store + 'static>(
|
2023-07-23 16:44:41 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-04 00:18:13 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
do_serve_head(range, alias, state).await
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Serving file headers", skip(state))]
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn serve_head<S: Store + 'static>(
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
|
|
|
alias: web::Path<Serde<Alias>>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
do_serve_head(range, Serde::into_inner(alias.into_inner()), state).await
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn do_serve_head<S: Store + 'static>(
|
2023-07-23 16:44:41 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
2023-07-23 17:10:36 +00:00
|
|
|
alias: Alias,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-23 16:44:41 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
let Some(identifier) = state.repo.identifier_from_alias(&alias).await? else {
|
2023-07-05 21:46:44 +00:00
|
|
|
// Invalid alias
|
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
};
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let details = ensure_details(&state, &alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Some(public_url) = state.store.public_url(&identifier) {
|
2023-07-14 19:53:37 +00:00
|
|
|
return Ok(HttpResponse::SeeOther()
|
|
|
|
.insert_header((actix_web::http::header::LOCATION, public_url.as_str()))
|
|
|
|
.finish());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
ranged_file_head_resp(&state.store, identifier, range, details).await
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn ranged_file_head_resp<S: Store + 'static>(
|
|
|
|
store: &S,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: Arc<str>,
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
|
|
|
details: Details,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let builder = if let Some(web::Header(range_header)) = range {
|
|
|
|
//Range header exists - return as ranged
|
|
|
|
if let Some(range) = range::single_bytes_range(&range_header) {
|
|
|
|
let len = store.len(&identifier).await?;
|
|
|
|
|
|
|
|
if let Some(content_range) = range::to_content_range(range, len) {
|
|
|
|
let mut builder = HttpResponse::PartialContent();
|
|
|
|
builder.insert_header(content_range);
|
|
|
|
builder
|
|
|
|
} else {
|
|
|
|
HttpResponse::RangeNotSatisfiable()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(UploadError::Range.into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no range header
|
|
|
|
HttpResponse::Ok()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(srv_head(
|
|
|
|
builder,
|
2023-07-14 19:53:37 +00:00
|
|
|
details.media_type(),
|
2022-12-23 18:56:15 +00:00
|
|
|
7 * DAYS,
|
|
|
|
details.system_time(),
|
|
|
|
)
|
|
|
|
.finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn ranged_file_resp<S: Store + 'static>(
|
|
|
|
store: &S,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: Arc<str>,
|
2022-12-23 18:56:15 +00:00
|
|
|
range: Option<web::Header<Range>>,
|
|
|
|
details: Details,
|
2023-07-06 00:06:48 +00:00
|
|
|
not_found: bool,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let (builder, stream) = if let Some(web::Header(range_header)) = range {
|
|
|
|
//Range header exists - return as ranged
|
|
|
|
if let Some(range) = range::single_bytes_range(&range_header) {
|
|
|
|
let len = store.len(&identifier).await?;
|
|
|
|
|
|
|
|
if let Some(content_range) = range::to_content_range(range, len) {
|
|
|
|
let mut builder = HttpResponse::PartialContent();
|
|
|
|
builder.insert_header(content_range);
|
|
|
|
(
|
|
|
|
builder,
|
|
|
|
Either::left(Either::left(
|
2023-09-10 22:55:13 +00:00
|
|
|
range::chop_store(range, store, &identifier, len).await?,
|
2022-12-23 18:56:15 +00:00
|
|
|
)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
HttpResponse::RangeNotSatisfiable(),
|
|
|
|
Either::left(Either::right(empty())),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(UploadError::Range.into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//No Range header in the request - return the entire document
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::from_err(store.to_stream(&identifier, None, None).await?);
|
2023-07-06 00:06:48 +00:00
|
|
|
|
|
|
|
if not_found {
|
|
|
|
(HttpResponse::NotFound(), Either::right(stream))
|
|
|
|
} else {
|
|
|
|
(HttpResponse::Ok(), Either::right(stream))
|
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(srv_response(
|
|
|
|
builder,
|
|
|
|
stream,
|
2023-07-14 19:53:37 +00:00
|
|
|
details.media_type(),
|
2022-12-23 18:56:15 +00:00
|
|
|
7 * DAYS,
|
|
|
|
details.system_time(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper method to produce responses with proper cache headers
|
|
|
|
fn srv_response<S, E>(
|
|
|
|
builder: HttpResponseBuilder,
|
|
|
|
stream: S,
|
|
|
|
ext: mime::Mime,
|
|
|
|
expires: u32,
|
|
|
|
modified: SystemTime,
|
|
|
|
) -> HttpResponse
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<web::Bytes, E>> + 'static,
|
|
|
|
E: std::error::Error + 'static,
|
|
|
|
actix_web::Error: From<E>,
|
|
|
|
{
|
2023-09-10 22:55:13 +00:00
|
|
|
let stream = crate::stream::timeout(Duration::from_secs(5), stream);
|
|
|
|
|
|
|
|
let stream = streem::try_from_fn(|yielder| async move {
|
|
|
|
let stream = std::pin::pin!(stream);
|
|
|
|
let mut streamer = stream.into_streamer();
|
|
|
|
|
|
|
|
while let Some(res) = streamer.next().await {
|
2023-12-28 17:58:38 +00:00
|
|
|
tracing::trace!("srv_response: looping");
|
|
|
|
|
2023-09-10 22:55:13 +00:00
|
|
|
let item = res.map_err(Error::from)??;
|
|
|
|
yielder.yield_ok(item).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(()) as Result<(), actix_web::Error>
|
2022-12-23 18:56:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
srv_head(builder, ext, expires, modified).streaming(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A helper method to produce responses with proper cache headers
|
|
|
|
fn srv_head(
|
|
|
|
mut builder: HttpResponseBuilder,
|
|
|
|
ext: mime::Mime,
|
|
|
|
expires: u32,
|
|
|
|
modified: SystemTime,
|
|
|
|
) -> HttpResponseBuilder {
|
|
|
|
builder
|
|
|
|
.insert_header(LastModified(modified.into()))
|
|
|
|
.insert_header(CacheControl(vec![
|
|
|
|
CacheDirective::Public,
|
|
|
|
CacheDirective::MaxAge(expires),
|
|
|
|
CacheDirective::Extension("immutable".to_owned(), None),
|
|
|
|
]))
|
|
|
|
.insert_header((ACCEPT_RANGES, "bytes"))
|
|
|
|
.content_type(ext.to_string());
|
|
|
|
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
#[tracing::instrument(level = "DEBUG", skip(state))]
|
|
|
|
async fn proxy_alias_from_query<S: Store + 'static>(
|
|
|
|
alias_query: AliasQuery,
|
|
|
|
state: &State<S>,
|
|
|
|
) -> Result<Alias, Error> {
|
|
|
|
match alias_query {
|
|
|
|
AliasQuery::Alias { alias } => Ok(Serde::into_inner(alias)),
|
2024-02-24 04:12:19 +00:00
|
|
|
AliasQuery::Proxy { proxy } => {
|
|
|
|
let alias = if let Some(alias) = state.repo.related(proxy.clone()).await? {
|
|
|
|
alias
|
|
|
|
} else if !state.config.server.read_only {
|
2024-03-03 17:13:51 +00:00
|
|
|
let stream = download_stream(proxy.as_str(), state).await?;
|
2024-02-24 04:12:19 +00:00
|
|
|
|
2024-06-09 19:44:18 +00:00
|
|
|
// some time has passed, see if we've proxied elsewhere
|
|
|
|
if let Some(alias) = state.repo.related(proxy.clone()).await? {
|
|
|
|
alias
|
|
|
|
} else {
|
|
|
|
let (alias, token, _) =
|
|
|
|
ingest_inline(stream, state, &Default::default()).await?;
|
|
|
|
|
|
|
|
// last check, do we succeed or fail to relate the proxy alias
|
|
|
|
if let Err(ProxyAlreadyExists) =
|
|
|
|
state.repo.relate_url(proxy.clone(), alias.clone()).await?
|
|
|
|
{
|
|
|
|
queue::cleanup_alias(&state.repo, alias, token).await?;
|
|
|
|
|
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.related(proxy)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?
|
|
|
|
} else {
|
|
|
|
alias
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 04:12:19 +00:00
|
|
|
} else {
|
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
};
|
|
|
|
|
|
|
|
if !state.config.server.read_only {
|
|
|
|
state.repo.accessed_alias(alias.clone()).await?;
|
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
Ok(alias)
|
2024-02-24 04:12:19 +00:00
|
|
|
}
|
2024-03-02 19:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 04:12:19 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
async fn blurhash<S: Store + 'static>(
|
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
|
|
|
state: web::Data<State<S>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let alias = proxy_alias_from_query(query, &state).await?;
|
|
|
|
|
|
|
|
let hash = state
|
|
|
|
.repo
|
|
|
|
.hash(&alias)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
|
|
|
|
|
|
|
let blurhash = if let Some(blurhash) = state.repo.blurhash(hash.clone()).await? {
|
|
|
|
blurhash
|
|
|
|
} else {
|
|
|
|
let details = ensure_details(&state, &alias).await?;
|
|
|
|
let blurhash = blurhash::generate(&state, &alias, &details).await?;
|
|
|
|
let blurhash: Arc<str> = Arc::from(blurhash);
|
|
|
|
state.repo.relate_blurhash(hash, blurhash.clone()).await?;
|
|
|
|
|
|
|
|
blurhash
|
|
|
|
};
|
2024-02-24 04:12:19 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
|
|
|
"msg": "ok",
|
2024-03-02 19:27:58 +00:00
|
|
|
"blurhash": blurhash.as_ref(),
|
2024-02-24 04:12:19 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2023-12-12 22:54:41 +00:00
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct PruneResponse {
|
|
|
|
complete: bool,
|
|
|
|
progress: u64,
|
|
|
|
total: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct PruneQuery {
|
2024-06-09 16:05:13 +00:00
|
|
|
force: Serde<bool>,
|
2023-12-12 22:54:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Prune missing identifiers", skip(state))]
|
|
|
|
async fn prune_missing<S>(
|
|
|
|
state: web::Data<State<S>>,
|
2023-12-12 22:54:41 +00:00
|
|
|
query: Option<web::Query<PruneQuery>>,
|
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
2023-12-12 22:54:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let total = state.repo.size().await?;
|
|
|
|
|
|
|
|
let progress = if let Some(progress) = state.repo.get("prune-missing-queued").await? {
|
2023-12-12 22:54:41 +00:00
|
|
|
progress
|
|
|
|
.as_ref()
|
|
|
|
.try_into()
|
|
|
|
.map(u64::from_be_bytes)
|
|
|
|
.unwrap_or(0)
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let complete = state.repo.get("prune-missing-complete").await?.is_some();
|
2023-12-12 22:54:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let started = state.repo.get("prune-missing-started").await?.is_some();
|
2023-12-12 22:54:41 +00:00
|
|
|
|
2024-06-09 16:05:13 +00:00
|
|
|
if !started || query.is_some_and(|q| *q.force) {
|
2024-02-03 19:31:54 +00:00
|
|
|
queue::prune_missing(&state.repo).await?;
|
2023-12-12 22:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(PruneResponse {
|
|
|
|
complete,
|
|
|
|
progress,
|
|
|
|
total,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Spawning variant cleanup", skip(state))]
|
|
|
|
async fn clean_variants<S>(state: web::Data<State<S>>) -> Result<HttpResponse, Error> {
|
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
queue::cleanup_all_variants(&state.repo).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
2023-07-23 20:45:52 +00:00
|
|
|
#[serde(untagged)]
|
2023-07-23 16:44:41 +00:00
|
|
|
enum AliasQuery {
|
|
|
|
Proxy { proxy: url::Url },
|
|
|
|
Alias { alias: Serde<Alias> },
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
impl From<ProcessSource> for AliasQuery {
|
|
|
|
fn from(value: ProcessSource) -> Self {
|
|
|
|
match value {
|
|
|
|
ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => {
|
|
|
|
Self::Alias { alias }
|
|
|
|
}
|
|
|
|
ProcessSource::Proxy { proxy } => Self::Proxy { proxy },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Setting 404 Image", skip(state))]
|
|
|
|
async fn set_not_found<S>(
|
2023-07-06 00:06:48 +00:00
|
|
|
json: web::Json<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-07-06 00:06:48 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2023-07-23 16:44:41 +00:00
|
|
|
let alias = match json.into_inner() {
|
2023-07-23 17:10:36 +00:00
|
|
|
AliasQuery::Alias { alias } => Serde::into_inner(alias),
|
2023-07-23 20:45:52 +00:00
|
|
|
AliasQuery::Proxy { .. } => {
|
|
|
|
return Ok(HttpResponse::BadRequest().json(serde_json::json!({
|
|
|
|
"msg": "Cannot use proxied media as Not Found image",
|
2024-03-02 19:27:58 +00:00
|
|
|
"code": "proxy-not-allowed",
|
2023-07-23 20:45:52 +00:00
|
|
|
})));
|
2023-07-23 16:44:41 +00:00
|
|
|
}
|
|
|
|
};
|
2023-07-06 00:06:48 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.hash(&alias)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
2023-07-07 18:33:27 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
state
|
|
|
|
.repo
|
|
|
|
.set(NOT_FOUND_KEY, alias.to_bytes().into())
|
|
|
|
.await?;
|
2023-07-06 00:06:48 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Created().json(serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
async fn alias_from_query<S>(alias_query: AliasQuery, state: &State<S>) -> Result<Alias, Error> {
|
|
|
|
match alias_query {
|
|
|
|
AliasQuery::Alias { alias } => Ok(Serde::into_inner(alias)),
|
|
|
|
AliasQuery::Proxy { proxy } => {
|
|
|
|
let alias = state
|
|
|
|
.repo
|
|
|
|
.related(proxy)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingProxy)?;
|
|
|
|
|
|
|
|
if !state.config.server.read_only {
|
|
|
|
state.repo.accessed_alias(alias.clone()).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Purging file", skip(state))]
|
|
|
|
async fn purge<S>(
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-17 19:24:49 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let aliases = state.repo.aliases_from_alias(&alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
let hash = state
|
|
|
|
.repo
|
|
|
|
.hash(&alias)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
queue::cleanup_hash(&state.repo, hash).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"aliases": aliases.iter().map(|a| a.to_string()).collect::<Vec<_>>()
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Deleting alias", skip(state))]
|
|
|
|
async fn delete_alias<S>(
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2023-09-01 22:25:13 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-09-01 22:25:13 +00:00
|
|
|
return Err(UploadError::ReadOnly.into());
|
|
|
|
}
|
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-09-01 22:25:13 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Some(token) = state.repo.delete_token(&alias).await? {
|
|
|
|
queue::cleanup_alias(&state.repo, alias, token).await?;
|
2023-09-01 22:25:13 +00:00
|
|
|
} else {
|
|
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Fetching aliases", skip(state))]
|
|
|
|
async fn aliases<S>(
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
let aliases = state.repo.aliases_from_alias(&alias).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
|
|
|
"aliases": aliases.iter().map(|a| a.to_string()).collect::<Vec<_>>()
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(name = "Fetching identifier", skip(state))]
|
|
|
|
async fn identifier<S>(
|
2024-03-02 19:27:58 +00:00
|
|
|
web::Query(query): web::Query<AliasQuery>,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: web::Data<State<S>>,
|
2022-12-23 18:56:15 +00:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2024-03-02 19:27:58 +00:00
|
|
|
let alias = alias_from_query(query, &state).await?;
|
2023-07-23 16:44:41 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
let identifier = state
|
|
|
|
.repo
|
|
|
|
.identifier_from_alias(&alias)
|
|
|
|
.await?
|
|
|
|
.ok_or(UploadError::MissingAlias)?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
|
|
|
"msg": "ok",
|
2023-09-02 23:30:45 +00:00
|
|
|
"identifier": identifier.as_ref(),
|
2022-12-23 18:56:15 +00:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
#[tracing::instrument(skip(state))]
|
|
|
|
async fn healthz<S: Store>(state: web::Data<State<S>>) -> Result<HttpResponse, Error> {
|
|
|
|
state.repo.health_check().await?;
|
|
|
|
state.store.health_check().await?;
|
2023-01-29 17:36:09 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
2022-12-23 18:56:15 +00:00
|
|
|
fn transform_error(error: actix_form_data::Error) -> actix_web::Error {
|
|
|
|
let error: Error = error.into();
|
|
|
|
let error: actix_web::Error = error.into();
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
2024-06-09 16:36:03 +00:00
|
|
|
fn build_client_config() -> rustls::ClientConfig {
|
|
|
|
rustls::ClientConfig::builder()
|
|
|
|
.with_root_certificates(rustls::RootCertStore {
|
|
|
|
roots: Vec::from(webpki_roots::TLS_SERVER_ROOTS),
|
|
|
|
})
|
|
|
|
.with_no_client_auth()
|
|
|
|
}
|
|
|
|
|
2023-10-01 03:43:40 +00:00
|
|
|
fn build_client() -> Result<ClientWithMiddleware, Error> {
|
2023-07-21 21:58:31 +00:00
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
.user_agent("pict-rs v0.5.0-main")
|
2024-06-09 16:36:03 +00:00
|
|
|
.use_preconfigured_tls(build_client_config())
|
2023-07-21 21:58:31 +00:00
|
|
|
.build()
|
|
|
|
.map_err(UploadError::BuildClient)?;
|
|
|
|
|
|
|
|
Ok(ClientBuilder::new(client)
|
|
|
|
.with(TracingMiddleware::default())
|
|
|
|
.build())
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:00:54 +00:00
|
|
|
fn query_config() -> web::QueryConfig {
|
|
|
|
web::QueryConfig::default()
|
|
|
|
.error_handler(|err, _| Error::from(UploadError::InvalidQuery(err)).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn json_config() -> web::JsonConfig {
|
|
|
|
web::JsonConfig::default()
|
|
|
|
.error_handler(|err, _| Error::from(UploadError::InvalidJson(err)).into())
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
fn configure_endpoints<S: Store + 'static, F: Fn(&mut web::ServiceConfig)>(
|
2023-06-23 16:20:20 +00:00
|
|
|
config: &mut web::ServiceConfig,
|
2024-02-03 19:31:54 +00:00
|
|
|
state: State<S>,
|
2023-07-08 22:35:57 +00:00
|
|
|
extra_config: F,
|
2023-06-23 16:20:20 +00:00
|
|
|
) {
|
|
|
|
config
|
2024-03-28 00:00:54 +00:00
|
|
|
.app_data(query_config())
|
|
|
|
.app_data(json_config())
|
2024-02-04 00:18:13 +00:00
|
|
|
.app_data(web::Data::new(state.clone()))
|
2023-08-16 00:19:03 +00:00
|
|
|
.route("/healthz", web::get().to(healthz::<S>))
|
2023-06-23 16:20:20 +00:00
|
|
|
.service(
|
|
|
|
web::scope("/image")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
|
|
|
.guard(guard::Post())
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::post().to(upload::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/backgrounded")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
|
|
|
.guard(guard::Post())
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::post().to(upload_backgrounded::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
2023-08-16 00:19:03 +00:00
|
|
|
.service(web::resource("/claim").route(web::get().to(claim_upload::<S>))),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
2023-08-16 00:19:03 +00:00
|
|
|
.service(web::resource("/download").route(web::get().to(download::<S>)))
|
2023-06-23 16:20:20 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/delete/{delete_token}/{filename}")
|
2024-02-03 19:31:54 +00:00
|
|
|
.route(web::delete().to(delete::<S>))
|
|
|
|
.route(web::get().to(delete::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
|
|
|
.service(
|
2023-07-23 16:44:41 +00:00
|
|
|
web::scope("/original")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::get().to(serve_query::<S>))
|
|
|
|
.route(web::head().to(serve_query_head::<S>)),
|
2023-07-23 16:44:41 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/{filename}")
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::get().to(serve::<S>))
|
|
|
|
.route(web::head().to(serve_head::<S>)),
|
2023-07-23 16:44:41 +00:00
|
|
|
),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
2024-02-24 04:12:19 +00:00
|
|
|
.service(web::resource("/blurhash").route(web::get().to(blurhash::<S>)))
|
2023-06-23 16:20:20 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/process.{ext}")
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::get().to(process::<S>))
|
|
|
|
.route(web::head().to(process_head::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/process_backgrounded.{ext}")
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::get().to(process_backgrounded::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/details")
|
|
|
|
.service(
|
2023-07-23 16:44:41 +00:00
|
|
|
web::scope("/original")
|
2023-08-16 00:19:03 +00:00
|
|
|
.service(web::resource("").route(web::get().to(details_query::<S>)))
|
2023-07-23 16:44:41 +00:00
|
|
|
.service(
|
2023-08-16 00:19:03 +00:00
|
|
|
web::resource("/{filename}").route(web::get().to(details::<S>)),
|
2023-07-23 16:44:41 +00:00
|
|
|
),
|
2023-06-23 16:20:20 +00:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/process.{ext}")
|
2023-08-16 00:19:03 +00:00
|
|
|
.route(web::get().to(process_details::<S>)),
|
2023-06-23 16:20:20 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::scope("/internal")
|
2024-02-04 00:18:13 +00:00
|
|
|
.wrap(Internal(state.config.server.api_key.clone()))
|
2023-08-16 00:19:03 +00:00
|
|
|
.service(web::resource("/import").route(web::post().to(import::<S>)))
|
2024-02-03 19:31:54 +00:00
|
|
|
.service(web::resource("/variants").route(web::delete().to(clean_variants::<S>)))
|
|
|
|
.service(web::resource("/purge").route(web::post().to(purge::<S>)))
|
|
|
|
.service(web::resource("/delete").route(web::post().to(delete_alias::<S>)))
|
|
|
|
.service(web::resource("/aliases").route(web::get().to(aliases::<S>)))
|
2023-08-16 00:19:03 +00:00
|
|
|
.service(web::resource("/identifier").route(web::get().to(identifier::<S>)))
|
2024-02-03 19:31:54 +00:00
|
|
|
.service(web::resource("/set_not_found").route(web::post().to(set_not_found::<S>)))
|
2024-02-04 00:18:13 +00:00
|
|
|
.service(web::resource("/hashes").route(web::get().to(page::<S>)))
|
2024-02-03 19:31:54 +00:00
|
|
|
.service(web::resource("/prune_missing").route(web::post().to(prune_missing::<S>)))
|
2023-07-08 22:35:57 +00:00
|
|
|
.configure(extra_config),
|
2023-06-23 16:20:20 +00:00
|
|
|
);
|
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
fn spawn_cleanup<S>(state: State<S>) {
|
2024-02-03 19:31:54 +00:00
|
|
|
if state.config.server.read_only {
|
2023-07-23 01:26:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:08:11 +00:00
|
|
|
crate::sync::spawn("queue-cleanup", async move {
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(30));
|
2023-07-23 00:41:50 +00:00
|
|
|
|
2023-09-04 02:30:47 +00:00
|
|
|
loop {
|
2023-12-28 17:58:38 +00:00
|
|
|
tracing::trace!("queue_cleanup: looping");
|
|
|
|
|
2023-09-04 02:30:47 +00:00
|
|
|
interval.tick().await;
|
2023-07-23 00:41:50 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Err(e) = queue::cleanup_outdated_variants(&state.repo).await {
|
2023-09-04 02:30:47 +00:00
|
|
|
tracing::warn!(
|
|
|
|
"Failed to spawn cleanup for outdated variants:{}",
|
|
|
|
format!("\n{e}\n{e:?}")
|
|
|
|
);
|
|
|
|
}
|
2023-07-23 20:45:52 +00:00
|
|
|
|
2024-02-03 19:31:54 +00:00
|
|
|
if let Err(e) = queue::cleanup_outdated_proxies(&state.repo).await {
|
2023-09-04 02:30:47 +00:00
|
|
|
tracing::warn!(
|
|
|
|
"Failed to spawn cleanup for outdated proxies:{}",
|
|
|
|
format!("\n{e}\n{e:?}")
|
|
|
|
);
|
2023-07-23 00:41:50 +00:00
|
|
|
}
|
2023-09-04 02:30:47 +00:00
|
|
|
}
|
|
|
|
});
|
2023-07-23 00:41:50 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 14:36:31 +00:00
|
|
|
fn spawn_workers<S>(state: State<S>)
|
2024-02-03 19:31:54 +00:00
|
|
|
where
|
2023-07-05 14:52:19 +00:00
|
|
|
S: Store + 'static,
|
|
|
|
{
|
2024-02-03 19:31:54 +00:00
|
|
|
crate::sync::spawn("cleanup-worker", queue::process_cleanup(state.clone()));
|
2024-03-30 14:36:31 +00:00
|
|
|
crate::sync::spawn("process-worker", queue::process_images(state));
|
2023-07-05 14:52:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
fn watch_keys(tls: Tls, sender: ChannelSender) -> DropHandle<()> {
|
|
|
|
crate::sync::abort_on_drop(crate::sync::spawn("cert-reader", async move {
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(30));
|
|
|
|
interval.tick().await;
|
2024-01-31 23:47:42 +00:00
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
loop {
|
2024-01-31 23:47:42 +00:00
|
|
|
interval.tick().await;
|
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
match tls.open_keys().await {
|
|
|
|
Ok(certified_key) => sender.update(certified_key),
|
|
|
|
Err(e) => tracing::error!("Failed to open keys {}", format!("{e}")),
|
2024-01-31 23:47:42 +00:00
|
|
|
}
|
2024-02-04 00:42:34 +00:00
|
|
|
}
|
|
|
|
}))
|
2023-06-23 16:20:20 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
async fn launch<
|
|
|
|
S: Store + Send + 'static,
|
|
|
|
F: Fn(&mut web::ServiceConfig) + Send + Clone + 'static,
|
|
|
|
>(
|
|
|
|
state: State<S>,
|
2023-07-08 22:35:57 +00:00
|
|
|
extra_config: F,
|
2024-01-31 23:47:42 +00:00
|
|
|
) -> color_eyre::Result<()> {
|
2024-02-04 00:18:13 +00:00
|
|
|
let address = state.config.server.address;
|
2024-01-31 23:47:42 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let tls = Tls::from_config(&state.config);
|
2024-02-03 19:31:54 +00:00
|
|
|
|
|
|
|
spawn_cleanup(state.clone());
|
|
|
|
|
2024-01-31 23:47:42 +00:00
|
|
|
let server = HttpServer::new(move || {
|
2023-07-08 22:35:57 +00:00
|
|
|
let extra_config = extra_config.clone();
|
2024-02-03 19:31:54 +00:00
|
|
|
let state = state.clone();
|
2023-06-23 16:20:20 +00:00
|
|
|
|
2024-03-30 14:36:31 +00:00
|
|
|
spawn_workers(state.clone());
|
2023-06-23 16:20:20 +00:00
|
|
|
|
|
|
|
App::new()
|
2024-06-04 03:15:49 +00:00
|
|
|
.wrap(Log::new(state.config.tracing.logging.log_requests))
|
2023-06-23 16:20:20 +00:00
|
|
|
.wrap(TracingLogger::default())
|
|
|
|
.wrap(Deadline)
|
2023-07-22 21:47:59 +00:00
|
|
|
.wrap(Metrics)
|
2023-09-30 21:26:43 +00:00
|
|
|
.wrap(Payload::new())
|
2024-03-30 14:36:31 +00:00
|
|
|
.configure(move |sc| configure_endpoints(sc, state.clone(), extra_config))
|
2024-01-31 23:47:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(tls) = tls {
|
|
|
|
let certified_key = tls.open_keys().await?;
|
|
|
|
|
|
|
|
let (tx, rx) = rustls_channel_resolver::channel::<32>(certified_key);
|
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
let handle = watch_keys(tls, tx);
|
2024-01-31 23:47:42 +00:00
|
|
|
|
2024-02-04 03:58:08 +00:00
|
|
|
let config = rustls::ServerConfig::builder()
|
2024-01-31 23:47:42 +00:00
|
|
|
.with_no_client_auth()
|
|
|
|
.with_cert_resolver(rx);
|
|
|
|
|
2024-02-04 00:42:34 +00:00
|
|
|
tracing::info!("Starting pict-rs with TLS on {address}");
|
|
|
|
|
2024-05-19 15:08:48 +00:00
|
|
|
server.bind_rustls_0_23(address, config)?.run().await?;
|
2024-01-31 23:47:42 +00:00
|
|
|
|
|
|
|
handle.abort();
|
|
|
|
let _ = handle.await;
|
|
|
|
} else {
|
2024-02-04 00:42:34 +00:00
|
|
|
tracing::info!("Starting pict-rs on {address}");
|
|
|
|
|
2024-01-31 23:47:42 +00:00
|
|
|
server.bind(address)?.run().await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 16:32:36 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn migrate_inner<S1>(
|
2024-02-04 00:18:13 +00:00
|
|
|
config: Configuration,
|
2023-10-07 16:32:36 +00:00
|
|
|
tmp_dir: ArcTmpDir,
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir: ArcPolicyDir,
|
2023-09-02 16:52:55 +00:00
|
|
|
repo: ArcRepo,
|
2023-07-21 21:58:31 +00:00
|
|
|
client: ClientWithMiddleware,
|
2023-06-20 20:59:08 +00:00
|
|
|
from: S1,
|
2023-07-11 18:01:58 +00:00
|
|
|
to: config::primitives::Store,
|
2023-06-20 20:59:08 +00:00
|
|
|
skip_missing_files: bool,
|
2023-10-03 20:50:53 +00:00
|
|
|
concurrency: usize,
|
2023-06-20 20:59:08 +00:00
|
|
|
) -> color_eyre::Result<()>
|
2022-12-23 18:56:15 +00:00
|
|
|
where
|
2023-07-14 19:53:37 +00:00
|
|
|
S1: Store + 'static,
|
2022-12-23 18:56:15 +00:00
|
|
|
{
|
|
|
|
match to {
|
2023-07-11 18:01:58 +00:00
|
|
|
config::primitives::Store::Filesystem(config::Filesystem { path }) => {
|
2024-02-26 21:43:30 +00:00
|
|
|
let store = FileStore::build(path.clone()).await?;
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let to = State {
|
|
|
|
config,
|
2023-10-07 16:32:36 +00:00
|
|
|
tmp_dir,
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir,
|
2023-10-07 16:32:36 +00:00
|
|
|
repo,
|
2024-02-04 00:18:13 +00:00
|
|
|
store,
|
|
|
|
client,
|
|
|
|
};
|
|
|
|
|
|
|
|
migrate_store(from, to, skip_missing_files, concurrency).await?
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
2023-07-11 18:01:58 +00:00
|
|
|
config::primitives::Store::ObjectStorage(config::primitives::ObjectStorage {
|
2022-12-23 18:56:15 +00:00
|
|
|
endpoint,
|
|
|
|
bucket_name,
|
|
|
|
use_path_style,
|
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
2023-07-11 18:01:58 +00:00
|
|
|
signature_duration,
|
|
|
|
client_timeout,
|
2023-07-14 19:53:37 +00:00
|
|
|
public_endpoint,
|
2022-12-23 18:56:15 +00:00
|
|
|
}) => {
|
2024-02-04 00:18:13 +00:00
|
|
|
let store = ObjectStore::build(
|
2022-12-23 18:56:15 +00:00
|
|
|
endpoint.clone(),
|
|
|
|
bucket_name,
|
|
|
|
if use_path_style {
|
|
|
|
UrlStyle::Path
|
|
|
|
} else {
|
|
|
|
UrlStyle::VirtualHost
|
|
|
|
},
|
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
2023-07-11 18:01:58 +00:00
|
|
|
signature_duration.unwrap_or(15),
|
|
|
|
client_timeout.unwrap_or(30),
|
2023-07-14 19:53:37 +00:00
|
|
|
public_endpoint,
|
2022-12-23 18:56:15 +00:00
|
|
|
)
|
|
|
|
.await?
|
2024-02-04 00:18:13 +00:00
|
|
|
.build(client.clone());
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let to = State {
|
|
|
|
config,
|
2023-10-07 16:32:36 +00:00
|
|
|
tmp_dir,
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir,
|
2023-10-07 16:32:36 +00:00
|
|
|
repo,
|
2024-02-04 00:18:13 +00:00
|
|
|
store,
|
|
|
|
client,
|
|
|
|
};
|
|
|
|
|
|
|
|
migrate_store(from, to, skip_missing_files, concurrency).await?
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:35:25 +00:00
|
|
|
impl<P: AsRef<Path>, T: serde::Serialize> ConfigSource<P, T> {
|
|
|
|
/// Initialize the pict-rs configuration
|
|
|
|
///
|
|
|
|
/// This takes an optional config_file path which is a valid pict-rs configuration file, and an
|
|
|
|
/// optional save_to path, which the generated configuration will be saved into. Since many
|
|
|
|
/// parameters have defaults, it can be useful to dump a valid configuration with default values to
|
|
|
|
/// see what is available for tweaking.
|
|
|
|
///
|
|
|
|
/// When running pict-rs as a library, configuration is limited to environment variables and
|
|
|
|
/// configuration files. Commandline options are not available.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2023-11-24 17:38:14 +00:00
|
|
|
/// let configuration = pict_rs::ConfigSource::memory(serde_json::json!({
|
2022-12-26 22:35:25 +00:00
|
|
|
/// "server": {
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "address": "127.0.0.1:8080",
|
|
|
|
/// "temporary_directory": "/tmp/t1"
|
2022-12-26 22:35:25 +00:00
|
|
|
/// },
|
|
|
|
/// "repo": {
|
|
|
|
/// "type": "sled",
|
|
|
|
/// "path": "./sled-repo"
|
|
|
|
/// },
|
|
|
|
/// "store": {
|
|
|
|
/// "type": "filesystem",
|
|
|
|
/// "path": "./files"
|
|
|
|
/// }
|
|
|
|
/// })).init::<&str>(None)?;
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-22 17:31:01 +00:00
|
|
|
pub fn init<Q: AsRef<Path>>(
|
|
|
|
self,
|
|
|
|
save_to: Option<Q>,
|
|
|
|
) -> color_eyre::Result<PictRsConfiguration> {
|
|
|
|
config::configure_without_clap(self, save_to)
|
2022-12-26 22:35:25 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-08 22:35:57 +00:00
|
|
|
async fn export_handler(repo: web::Data<SledRepo>) -> Result<HttpResponse, Error> {
|
|
|
|
repo.export().await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Created().json(&serde_json::json!({
|
|
|
|
"msg": "ok"
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
fn sled_extra_config(sc: &mut web::ServiceConfig, repo: SledRepo) {
|
|
|
|
sc.app_data(web::Data::new(repo))
|
|
|
|
.service(web::resource("/export").route(web::post().to(export_handler)));
|
2023-07-08 22:35:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
impl PictRsConfiguration {
|
|
|
|
/// Build the pict-rs configuration from commandline arguments
|
|
|
|
///
|
|
|
|
/// This is probably not useful for 3rd party applications that handle their own commandline
|
|
|
|
pub fn build_default() -> color_eyre::Result<Self> {
|
|
|
|
config::configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Install the default pict-rs tracer
|
|
|
|
///
|
|
|
|
/// This is probably not useful for 3rd party applications that install their own tracing
|
|
|
|
/// subscribers.
|
2023-07-22 17:39:39 +00:00
|
|
|
pub fn install_tracing(self) -> color_eyre::Result<Self> {
|
|
|
|
init_tracing(&self.config.tracing)?;
|
|
|
|
Ok(self)
|
2023-07-22 17:31:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 22:52:58 +00:00
|
|
|
/// Install the configured pict-rs metrics collector
|
|
|
|
///
|
|
|
|
/// This is a no-op if pict-rs is not configured to export metrics. Applications that register
|
|
|
|
/// their own metrics collectors shouldn't call this method.
|
2023-07-22 21:47:59 +00:00
|
|
|
pub fn install_metrics(self) -> color_eyre::Result<Self> {
|
|
|
|
if let Some(addr) = self.config.metrics.prometheus_address {
|
|
|
|
PrometheusBuilder::new()
|
|
|
|
.with_http_listener(addr)
|
|
|
|
.install()?;
|
2024-01-30 21:21:15 +00:00
|
|
|
tracing::info!("Starting prometheus endpoint on {addr}");
|
2023-07-22 21:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2024-05-04 04:05:17 +00:00
|
|
|
/// Install aws-lc-rs as the default crypto provider
|
|
|
|
///
|
|
|
|
/// This would happen automatically anyway unless rustls crate features get mixed up
|
|
|
|
pub fn install_crypto_provider(self) -> Self {
|
2024-06-24 15:32:06 +00:00
|
|
|
if rustls::crypto::ring::default_provider()
|
2024-05-04 04:05:17 +00:00
|
|
|
.install_default()
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
tracing::info!("rustls crypto provider already installed");
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-10-07 16:48:58 +00:00
|
|
|
/// Run the pict-rs application on a tokio `LocalSet`
|
|
|
|
///
|
|
|
|
/// This must be called from within `tokio::main` directly
|
2023-11-24 17:38:14 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// ```rust
|
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main() -> color_eyre::Result<()> {
|
|
|
|
/// let pict_rs_server = pict_rs::ConfigSource::memory(serde_json::json!({
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "server": {
|
|
|
|
/// "temporary_directory": "/tmp/t2"
|
|
|
|
/// },
|
2023-11-24 17:38:14 +00:00
|
|
|
/// "repo": {
|
|
|
|
/// "type": "sled",
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "path": "/tmp/pict-rs-run-on-localset/sled-repo",
|
2023-11-24 17:38:14 +00:00
|
|
|
/// },
|
|
|
|
/// "store": {
|
|
|
|
/// "type": "filesystem",
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "path": "/tmp/pict-rs-run-on-localset/files",
|
2023-11-24 17:38:14 +00:00
|
|
|
/// },
|
|
|
|
/// }))
|
|
|
|
/// .init::<&str>(None)?
|
|
|
|
/// .run_on_localset();
|
|
|
|
///
|
|
|
|
/// let _ = tokio::time::timeout(std::time::Duration::from_secs(1), pict_rs_server).await;
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-10-07 16:48:58 +00:00
|
|
|
pub async fn run_on_localset(self) -> color_eyre::Result<()> {
|
|
|
|
tokio::task::LocalSet::new().run_until(self.run()).await
|
|
|
|
}
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
/// Run the pict-rs application
|
2023-10-07 17:22:17 +00:00
|
|
|
///
|
|
|
|
/// This must be called from within a tokio `LocalSet`, which is created by default for
|
2024-03-11 04:02:03 +00:00
|
|
|
/// actix-rt runtimes
|
2023-11-24 17:38:14 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
2024-03-11 04:02:03 +00:00
|
|
|
/// ```rust
|
2023-11-24 17:38:14 +00:00
|
|
|
/// fn main() -> color_eyre::Result<()> {
|
2024-03-11 04:02:03 +00:00
|
|
|
/// actix_web::rt::System::new().block_on(async move {
|
2023-11-24 17:38:14 +00:00
|
|
|
/// let pict_rs_server = pict_rs::ConfigSource::memory(serde_json::json!({
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "server": {
|
|
|
|
/// "temporary_directory": "/tmp/t3"
|
|
|
|
/// },
|
2023-11-24 17:38:14 +00:00
|
|
|
/// "repo": {
|
|
|
|
/// "type": "sled",
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "path": "/tmp/pict-rs-run/sled-repo",
|
2023-11-24 17:38:14 +00:00
|
|
|
/// },
|
|
|
|
/// "store": {
|
|
|
|
/// "type": "filesystem",
|
2024-04-04 19:39:26 +00:00
|
|
|
/// "path": "/tmp/pict-rs-run/files",
|
2023-11-24 17:38:14 +00:00
|
|
|
/// },
|
|
|
|
/// }))
|
|
|
|
/// .init::<&str>(None)?
|
|
|
|
/// .run();
|
|
|
|
///
|
|
|
|
/// let _ = tokio::time::timeout(std::time::Duration::from_secs(1), pict_rs_server).await;
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-22 17:31:01 +00:00
|
|
|
pub async fn run(self) -> color_eyre::Result<()> {
|
2024-03-11 03:02:27 +00:00
|
|
|
#[cfg(feature = "random-errors")]
|
|
|
|
tracing::error!("pict-rs has been compiled with with the 'random-errors' feature enabled.");
|
|
|
|
#[cfg(feature = "random-errors")]
|
|
|
|
tracing::error!("This is not suitable for production environments");
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
let PictRsConfiguration { config, operation } = self;
|
|
|
|
|
2024-02-04 21:45:47 +00:00
|
|
|
// describe all the metrics pict-rs produces
|
|
|
|
init_metrics::init_metrics();
|
|
|
|
|
2024-03-23 23:41:59 +00:00
|
|
|
let tmp_dir = TmpDir::init(
|
|
|
|
&config.server.temporary_directory,
|
|
|
|
config.server.cleanup_temporary_directory,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-02-01 03:32:01 +00:00
|
|
|
let policy_dir = magick::write_magick_policy(&config.media, &tmp_dir).await?;
|
2023-10-07 16:32:36 +00:00
|
|
|
|
2023-10-01 03:43:40 +00:00
|
|
|
let client = build_client()?;
|
2023-07-22 17:31:01 +00:00
|
|
|
|
|
|
|
match operation {
|
|
|
|
Operation::Run => (),
|
|
|
|
Operation::MigrateStore {
|
|
|
|
skip_missing_files,
|
2023-10-03 20:50:53 +00:00
|
|
|
concurrency,
|
2023-07-22 17:31:01 +00:00
|
|
|
from,
|
|
|
|
to,
|
|
|
|
} => {
|
2023-09-02 16:52:55 +00:00
|
|
|
let repo = Repo::open(config.repo.clone()).await?.to_arc();
|
2023-08-16 21:39:41 +00:00
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
match from {
|
|
|
|
config::primitives::Store::Filesystem(config::Filesystem { path }) => {
|
2024-02-26 21:43:30 +00:00
|
|
|
let from = FileStore::build(path.clone()).await?;
|
2023-08-05 17:41:06 +00:00
|
|
|
migrate_inner(
|
2024-02-04 00:18:13 +00:00
|
|
|
config,
|
2023-10-07 16:32:36 +00:00
|
|
|
tmp_dir,
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir,
|
2023-08-05 17:41:06 +00:00
|
|
|
repo,
|
|
|
|
client,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
skip_missing_files,
|
2023-10-03 20:50:53 +00:00
|
|
|
concurrency,
|
2023-08-05 17:41:06 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2023-07-22 17:31:01 +00:00
|
|
|
}
|
|
|
|
config::primitives::Store::ObjectStorage(
|
|
|
|
config::primitives::ObjectStorage {
|
|
|
|
endpoint,
|
|
|
|
bucket_name,
|
|
|
|
use_path_style,
|
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
|
|
|
signature_duration,
|
|
|
|
client_timeout,
|
|
|
|
public_endpoint,
|
2022-12-23 18:56:15 +00:00
|
|
|
},
|
2023-07-22 17:31:01 +00:00
|
|
|
) => {
|
|
|
|
let from = ObjectStore::build(
|
|
|
|
endpoint,
|
|
|
|
bucket_name,
|
|
|
|
if use_path_style {
|
|
|
|
UrlStyle::Path
|
|
|
|
} else {
|
|
|
|
UrlStyle::VirtualHost
|
|
|
|
},
|
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
|
|
|
signature_duration.unwrap_or(15),
|
|
|
|
client_timeout.unwrap_or(30),
|
|
|
|
public_endpoint,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
.build(client.clone());
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
migrate_inner(
|
2024-02-04 00:18:13 +00:00
|
|
|
config,
|
2023-10-07 16:32:36 +00:00
|
|
|
tmp_dir,
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir,
|
2023-08-05 17:41:06 +00:00
|
|
|
repo,
|
|
|
|
client,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
skip_missing_files,
|
2023-10-03 20:50:53 +00:00
|
|
|
concurrency,
|
2023-08-05 17:41:06 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2023-07-22 17:31:01 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2023-08-16 21:32:19 +00:00
|
|
|
Operation::MigrateRepo { from, to } => {
|
2023-09-02 16:52:55 +00:00
|
|
|
let from = Repo::open(from).await?.to_arc();
|
|
|
|
let to = Repo::open(to).await?.to_arc();
|
2023-08-16 21:32:19 +00:00
|
|
|
|
|
|
|
repo::migrate_repo(from, to).await?;
|
2023-08-16 21:34:42 +00:00
|
|
|
return Ok(());
|
2023-08-16 21:32:19 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 16:52:55 +00:00
|
|
|
let repo = Repo::open(config.repo.clone()).await?;
|
2023-08-16 21:39:41 +00:00
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
if config.server.read_only {
|
|
|
|
tracing::warn!("Launching in READ ONLY mode");
|
|
|
|
}
|
2023-07-17 19:24:49 +00:00
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
match config.store.clone() {
|
|
|
|
config::Store::Filesystem(config::Filesystem { path }) => {
|
2023-08-16 00:19:03 +00:00
|
|
|
let arc_repo = repo.to_arc();
|
|
|
|
|
2024-02-26 21:43:30 +00:00
|
|
|
let store = FileStore::build(path).await?;
|
2023-09-02 16:52:55 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let state = State {
|
|
|
|
tmp_dir: tmp_dir.clone(),
|
|
|
|
policy_dir: policy_dir.clone(),
|
|
|
|
repo: arc_repo.clone(),
|
|
|
|
store: store.clone(),
|
|
|
|
config: config.clone(),
|
|
|
|
client: client.clone(),
|
|
|
|
};
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
if arc_repo.get("migrate-0.4").await?.is_none() {
|
2023-10-01 03:43:40 +00:00
|
|
|
if let Some(path) = config.old_repo_path() {
|
|
|
|
if let Some(old_repo) = repo_04::open(path)? {
|
2024-02-04 00:18:13 +00:00
|
|
|
repo::migrate_04(old_repo, state.clone()).await?;
|
2023-10-01 03:43:40 +00:00
|
|
|
arc_repo
|
|
|
|
.set("migrate-0.4", Arc::from(b"migrated".to_vec()))
|
|
|
|
.await?;
|
|
|
|
}
|
2023-07-22 17:31:01 +00:00
|
|
|
}
|
2023-06-23 16:20:20 +00:00
|
|
|
}
|
2023-08-16 00:19:03 +00:00
|
|
|
|
|
|
|
match repo {
|
|
|
|
Repo::Sled(sled_repo) => {
|
2024-02-04 00:42:34 +00:00
|
|
|
launch(state, move |sc| sled_extra_config(sc, sled_repo.clone())).await?;
|
2023-08-16 00:19:03 +00:00
|
|
|
}
|
2023-09-03 22:11:34 +00:00
|
|
|
Repo::Postgres(_) => {
|
2024-02-04 00:42:34 +00:00
|
|
|
launch(state, |_| {}).await?;
|
2023-09-03 22:11:34 +00:00
|
|
|
}
|
2023-08-16 00:19:03 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
2023-07-22 17:31:01 +00:00
|
|
|
config::Store::ObjectStorage(config::ObjectStorage {
|
2022-12-23 18:56:15 +00:00
|
|
|
endpoint,
|
|
|
|
bucket_name,
|
2023-07-22 17:31:01 +00:00
|
|
|
use_path_style,
|
2022-12-23 18:56:15 +00:00
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
2023-07-11 18:01:58 +00:00
|
|
|
signature_duration,
|
|
|
|
client_timeout,
|
2023-07-14 19:53:37 +00:00
|
|
|
public_endpoint,
|
2023-07-22 17:31:01 +00:00
|
|
|
}) => {
|
2023-09-02 16:52:55 +00:00
|
|
|
let arc_repo = repo.to_arc();
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
let store = ObjectStore::build(
|
|
|
|
endpoint,
|
|
|
|
bucket_name,
|
|
|
|
if use_path_style {
|
|
|
|
UrlStyle::Path
|
|
|
|
} else {
|
|
|
|
UrlStyle::VirtualHost
|
|
|
|
},
|
|
|
|
region,
|
|
|
|
access_key,
|
|
|
|
secret_key,
|
|
|
|
session_token,
|
|
|
|
signature_duration,
|
|
|
|
client_timeout,
|
|
|
|
public_endpoint,
|
|
|
|
)
|
2023-08-16 00:19:03 +00:00
|
|
|
.await?
|
|
|
|
.build(client.clone());
|
2022-12-23 18:56:15 +00:00
|
|
|
|
2024-02-04 00:18:13 +00:00
|
|
|
let state = State {
|
|
|
|
tmp_dir: tmp_dir.clone(),
|
|
|
|
policy_dir: policy_dir.clone(),
|
|
|
|
repo: arc_repo.clone(),
|
|
|
|
store: store.clone(),
|
|
|
|
config: config.clone(),
|
|
|
|
client: client.clone(),
|
|
|
|
};
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
if arc_repo.get("migrate-0.4").await?.is_none() {
|
2023-10-01 03:43:40 +00:00
|
|
|
if let Some(path) = config.old_repo_path() {
|
|
|
|
if let Some(old_repo) = repo_04::open(path)? {
|
2024-02-04 00:18:13 +00:00
|
|
|
repo::migrate_04(old_repo, state.clone()).await?;
|
2023-10-01 03:43:40 +00:00
|
|
|
arc_repo
|
|
|
|
.set("migrate-0.4", Arc::from(b"migrated".to_vec()))
|
|
|
|
.await?;
|
|
|
|
}
|
2023-07-22 17:31:01 +00:00
|
|
|
}
|
2023-06-23 16:20:20 +00:00
|
|
|
}
|
2023-08-16 00:19:03 +00:00
|
|
|
|
|
|
|
match repo {
|
|
|
|
Repo::Sled(sled_repo) => {
|
2024-02-04 00:42:34 +00:00
|
|
|
launch(state, move |sc| sled_extra_config(sc, sled_repo.clone())).await?;
|
2023-08-16 00:19:03 +00:00
|
|
|
}
|
2023-09-03 22:11:34 +00:00
|
|
|
Repo::Postgres(_) => {
|
2024-02-04 00:42:34 +00:00
|
|
|
launch(state, |_| {}).await?;
|
2023-09-03 22:11:34 +00:00
|
|
|
}
|
2023-08-16 00:19:03 +00:00
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-23 16:20:20 +00:00
|
|
|
|
2024-02-01 03:32:01 +00:00
|
|
|
policy_dir.cleanup().await?;
|
2023-10-07 16:36:49 +00:00
|
|
|
tmp_dir.cleanup().await?;
|
|
|
|
|
2023-07-22 17:31:01 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-12-23 18:56:15 +00:00
|
|
|
}
|
2023-07-23 16:44:41 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn source() {
|
|
|
|
let query = super::ProcessQuery {
|
|
|
|
source: super::ProcessSource::Source {
|
|
|
|
src: super::Serde::new(super::Alias::from_existing("example.png")),
|
|
|
|
},
|
|
|
|
operations: vec![("resize".into(), "200".into())],
|
|
|
|
};
|
|
|
|
let encoded = serde_urlencoded::to_string(&query).expect("Encoded");
|
|
|
|
let new_query: super::ProcessQuery = serde_urlencoded::from_str(&encoded).expect("Decoded");
|
|
|
|
// Don't compare entire query - "src" gets deserialized twice
|
|
|
|
assert_eq!(new_query.source, query.source);
|
|
|
|
|
|
|
|
assert!(new_query
|
|
|
|
.operations
|
|
|
|
.contains(&("resize".into(), "200".into())));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias() {
|
|
|
|
let query = super::ProcessQuery {
|
|
|
|
source: super::ProcessSource::Alias {
|
|
|
|
alias: super::Serde::new(super::Alias::from_existing("example.png")),
|
|
|
|
},
|
|
|
|
operations: vec![("resize".into(), "200".into())],
|
|
|
|
};
|
|
|
|
let encoded = serde_urlencoded::to_string(&query).expect("Encoded");
|
|
|
|
let new_query: super::ProcessQuery = serde_urlencoded::from_str(&encoded).expect("Decoded");
|
|
|
|
// Don't compare entire query - "alias" gets deserialized twice
|
|
|
|
assert_eq!(new_query.source, query.source);
|
|
|
|
|
|
|
|
assert!(new_query
|
|
|
|
.operations
|
|
|
|
.contains(&("resize".into(), "200".into())));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn url() {
|
|
|
|
let query = super::ProcessQuery {
|
|
|
|
source: super::ProcessSource::Proxy {
|
|
|
|
proxy: "http://example.com/image.png".parse().expect("valid url"),
|
|
|
|
},
|
|
|
|
operations: vec![("resize".into(), "200".into())],
|
|
|
|
};
|
|
|
|
let encoded = serde_urlencoded::to_string(&query).expect("Encoded");
|
|
|
|
let new_query: super::ProcessQuery = serde_urlencoded::from_str(&encoded).expect("Decoded");
|
|
|
|
// Don't compare entire query - "proxy" gets deserialized twice
|
|
|
|
assert_eq!(new_query.source, query.source);
|
|
|
|
|
|
|
|
assert!(new_query
|
|
|
|
.operations
|
|
|
|
.contains(&("resize".into(), "200".into())));
|
|
|
|
}
|
|
|
|
}
|