2020-06-06 21:02:26 +00:00
|
|
|
use actix_form_data::{Field, Form, Value};
|
2021-01-14 16:26:17 +00:00
|
|
|
use actix_web::{
|
|
|
|
guard,
|
2021-01-18 23:11:32 +00:00
|
|
|
http::header::{CacheControl, CacheDirective, LastModified, ACCEPT_RANGES},
|
2021-08-26 02:46:11 +00:00
|
|
|
middleware::Logger,
|
2021-04-17 19:59:42 +00:00
|
|
|
web, App, HttpResponse, HttpResponseBuilder, HttpServer,
|
2021-01-14 16:26:17 +00:00
|
|
|
};
|
2021-03-10 02:51:03 +00:00
|
|
|
use awc::Client;
|
2021-09-05 22:15:30 +00:00
|
|
|
use dashmap::{mapref::entry::Entry, DashMap};
|
2021-09-04 19:20:31 +00:00
|
|
|
use futures_core::stream::Stream;
|
2021-09-04 21:01:48 +00:00
|
|
|
use once_cell::sync::{Lazy, OnceCell};
|
2021-09-06 00:05:58 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
future::{ready, Future},
|
|
|
|
path::PathBuf,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
time::SystemTime,
|
|
|
|
};
|
2020-06-07 00:54:06 +00:00
|
|
|
use structopt::StructOpt;
|
2021-09-05 22:15:30 +00:00
|
|
|
use tokio::{
|
|
|
|
io::{AsyncReadExt, AsyncWriteExt},
|
2021-09-06 00:05:58 +00:00
|
|
|
sync::oneshot::{Receiver, Sender},
|
2021-09-05 22:15:30 +00:00
|
|
|
};
|
2020-06-14 15:07:31 +00:00
|
|
|
use tracing::{debug, error, info, instrument, Span};
|
2020-06-14 02:28:06 +00:00
|
|
|
use tracing_subscriber::EnvFilter;
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2020-06-07 00:54:06 +00:00
|
|
|
mod config;
|
2020-06-06 21:41:17 +00:00
|
|
|
mod error;
|
2021-08-31 16:02:30 +00:00
|
|
|
mod exiftool;
|
2021-08-26 02:46:11 +00:00
|
|
|
mod ffmpeg;
|
2021-08-28 22:15:14 +00:00
|
|
|
mod magick;
|
2020-06-14 02:28:06 +00:00
|
|
|
mod middleware;
|
2020-07-11 20:40:40 +00:00
|
|
|
mod migrate;
|
2020-06-07 17:51:45 +00:00
|
|
|
mod processor;
|
2021-01-18 23:11:32 +00:00
|
|
|
mod range;
|
2021-08-31 02:19:47 +00:00
|
|
|
mod stream;
|
2020-06-06 21:41:17 +00:00
|
|
|
mod upload_manager;
|
2020-06-11 16:46:00 +00:00
|
|
|
mod validate;
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2020-06-14 02:28:06 +00:00
|
|
|
use self::{
|
2020-11-01 20:23:56 +00:00
|
|
|
config::{Config, Format},
|
2020-06-24 16:58:46 +00:00
|
|
|
error::UploadError,
|
2020-07-11 21:28:49 +00:00
|
|
|
middleware::{Internal, Tracing},
|
2021-09-04 19:20:31 +00:00
|
|
|
stream::{once, LocalBoxStream},
|
2020-12-10 05:05:04 +00:00
|
|
|
upload_manager::{Details, UploadManager},
|
2020-06-24 16:58:46 +00:00
|
|
|
validate::{image_webp, video_mp4},
|
2020-06-14 02:28:06 +00:00
|
|
|
};
|
2020-06-06 21:02:26 +00:00
|
|
|
|
|
|
|
const MEGABYTES: usize = 1024 * 1024;
|
2020-10-02 20:38:52 +00:00
|
|
|
const MINUTES: u32 = 60;
|
|
|
|
const HOURS: u32 = 60 * MINUTES;
|
|
|
|
const DAYS: u32 = 24 * HOURS;
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2021-01-05 17:51:13 +00:00
|
|
|
static TMP_DIR: Lazy<PathBuf> = Lazy::new(|| {
|
|
|
|
use rand::{
|
|
|
|
distributions::{Alphanumeric, Distribution},
|
|
|
|
thread_rng,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
let tmp_nonce = Alphanumeric
|
|
|
|
.sample_iter(&mut rng)
|
|
|
|
.take(7)
|
2021-02-10 22:57:42 +00:00
|
|
|
.map(char::from)
|
2021-01-05 17:51:13 +00:00
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
let mut path = std::env::temp_dir();
|
|
|
|
path.push(format!("pict-rs-{}", tmp_nonce));
|
|
|
|
path
|
|
|
|
});
|
2020-12-26 02:39:19 +00:00
|
|
|
static CONFIG: Lazy<Config> = Lazy::new(Config::from_args);
|
2021-09-04 21:01:48 +00:00
|
|
|
static PROCESS_SEMAPHORE: OnceCell<tokio::sync::Semaphore> = OnceCell::new();
|
2021-09-05 22:15:30 +00:00
|
|
|
static PROCESS_MAP: Lazy<DashMap<PathBuf, Vec<Sender<(Details, web::Bytes)>>>> =
|
|
|
|
Lazy::new(DashMap::new);
|
2021-09-04 21:01:48 +00:00
|
|
|
|
|
|
|
fn process_semaphore() -> &'static tokio::sync::Semaphore {
|
|
|
|
PROCESS_SEMAPHORE
|
|
|
|
.get_or_init(|| tokio::sync::Semaphore::new(num_cpus::get().saturating_sub(1).max(1)))
|
|
|
|
}
|
2020-06-14 15:07:31 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
struct CancelSafeProcessor<F> {
|
|
|
|
path: PathBuf,
|
|
|
|
receiver: Option<Receiver<(Details, web::Bytes)>>,
|
|
|
|
fut: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> CancelSafeProcessor<F>
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<(Details, web::Bytes), UploadError>> + Unpin,
|
|
|
|
{
|
|
|
|
pub(crate) fn new(path: PathBuf, fut: F) -> Self {
|
|
|
|
let entry = PROCESS_MAP.entry(path.clone());
|
|
|
|
|
|
|
|
let receiver = match entry {
|
|
|
|
Entry::Vacant(vacant) => {
|
|
|
|
vacant.insert(Vec::new());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut occupied) => {
|
|
|
|
let (tx, rx) = tokio::sync::oneshot::channel();
|
|
|
|
occupied.get_mut().push(tx);
|
|
|
|
Some(rx)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-06 00:05:58 +00:00
|
|
|
CancelSafeProcessor {
|
|
|
|
path,
|
|
|
|
receiver,
|
|
|
|
fut,
|
|
|
|
}
|
2021-09-05 22:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Future for CancelSafeProcessor<F>
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<(Details, web::Bytes), UploadError>> + Unpin,
|
|
|
|
{
|
|
|
|
type Output = Result<(Details, web::Bytes), UploadError>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
if let Some(ref mut rx) = self.receiver {
|
2021-09-06 00:05:58 +00:00
|
|
|
Pin::new(rx)
|
|
|
|
.poll(cx)
|
|
|
|
.map(|res| res.map_err(|_| UploadError::Canceled))
|
2021-09-05 22:15:30 +00:00
|
|
|
} else {
|
2021-09-06 00:05:58 +00:00
|
|
|
Pin::new(&mut self.fut).poll(cx).map(|res| {
|
|
|
|
let opt = PROCESS_MAP.remove(&self.path);
|
|
|
|
res.map(|tup| {
|
|
|
|
if let Some((_, vec)) = opt {
|
|
|
|
for sender in vec {
|
|
|
|
let _ = sender.send(tup.clone());
|
2021-09-05 22:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-06 00:05:58 +00:00
|
|
|
tup
|
|
|
|
})
|
|
|
|
})
|
2021-09-05 22:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Drop for CancelSafeProcessor<F> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.receiver.is_none() {
|
|
|
|
PROCESS_MAP.remove(&self.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
// try moving a file
|
|
|
|
#[instrument]
|
|
|
|
async fn safe_move_file(from: PathBuf, to: PathBuf) -> Result<(), UploadError> {
|
|
|
|
if let Some(path) = to.parent() {
|
|
|
|
debug!("Creating directory {:?}", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
tokio::fs::create_dir_all(path).await?;
|
2020-06-24 16:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Checking if {:?} already exists", to);
|
2021-09-04 19:20:31 +00:00
|
|
|
if let Err(e) = tokio::fs::metadata(&to).await {
|
|
|
|
if e.kind() != std::io::ErrorKind::NotFound {
|
2020-06-24 16:58:46 +00:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(UploadError::FileExists);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Moving {:?} to {:?}", from, to);
|
2021-09-04 19:20:31 +00:00
|
|
|
tokio::fs::copy(&from, to).await?;
|
|
|
|
tokio::fs::remove_file(from).await?;
|
2020-06-24 16:58:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
async fn safe_create_parent<P>(path: P) -> Result<(), UploadError>
|
|
|
|
where
|
|
|
|
P: AsRef<std::path::Path>,
|
|
|
|
{
|
|
|
|
if let Some(path) = path.as_ref().parent() {
|
2020-10-16 22:48:42 +00:00
|
|
|
debug!("Creating directory {:?}", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
tokio::fs::create_dir_all(path).await?;
|
2020-10-16 22:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// Try writing to a file
|
2020-06-14 15:07:31 +00:00
|
|
|
#[instrument(skip(bytes))]
|
2021-09-04 19:20:31 +00:00
|
|
|
async fn safe_save_file(path: PathBuf, mut bytes: web::Bytes) -> Result<(), UploadError> {
|
2020-06-06 21:41:17 +00:00
|
|
|
if let Some(path) = path.parent() {
|
|
|
|
// create the directory for the file
|
2020-06-14 15:07:31 +00:00
|
|
|
debug!("Creating directory {:?}", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
tokio::fs::create_dir_all(path).await?;
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// Only write the file if it doesn't already exist
|
2020-06-14 15:07:31 +00:00
|
|
|
debug!("Checking if {:?} already exists", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
if let Err(e) = tokio::fs::metadata(&path).await {
|
|
|
|
if e.kind() != std::io::ErrorKind::NotFound {
|
2020-06-06 21:02:26 +00:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2020-06-06 21:41:17 +00:00
|
|
|
} else {
|
|
|
|
return Ok(());
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// Open the file for writing
|
2020-06-14 15:07:31 +00:00
|
|
|
debug!("Creating {:?}", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
let mut file = tokio::fs::File::create(&path).await?;
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// try writing
|
2020-06-14 15:07:31 +00:00
|
|
|
debug!("Writing to {:?}", path);
|
2021-09-04 19:20:31 +00:00
|
|
|
if let Err(e) = file.write_all_buf(&mut bytes).await {
|
2020-06-14 15:07:31 +00:00
|
|
|
error!("Error writing {:?}, {}", path, e);
|
2020-06-06 21:41:17 +00:00
|
|
|
// remove file if writing failed before completion
|
2021-09-04 19:20:31 +00:00
|
|
|
tokio::fs::remove_file(path).await?;
|
2020-06-06 21:41:17 +00:00
|
|
|
return Err(e.into());
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
2020-06-14 15:07:31 +00:00
|
|
|
debug!("{:?} written", path);
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
Ok(())
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
pub(crate) fn tmp_file() -> PathBuf {
|
|
|
|
use rand::distributions::{Alphanumeric, Distribution};
|
|
|
|
let limit: usize = 10;
|
|
|
|
let rng = rand::thread_rng();
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
let s: String = Alphanumeric
|
|
|
|
.sample_iter(rng)
|
|
|
|
.take(limit)
|
|
|
|
.map(char::from)
|
|
|
|
.collect();
|
2020-06-24 16:58:46 +00:00
|
|
|
|
|
|
|
let name = format!("{}.tmp", s);
|
|
|
|
|
2021-01-05 17:51:13 +00:00
|
|
|
let mut path = TMP_DIR.clone();
|
2020-06-24 16:58:46 +00:00
|
|
|
path.push(&name);
|
|
|
|
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_ext(mime: mime::Mime) -> Result<&'static str, UploadError> {
|
2020-06-06 21:02:26 +00:00
|
|
|
if mime == mime::IMAGE_PNG {
|
2020-06-24 16:58:46 +00:00
|
|
|
Ok(".png")
|
2020-06-06 21:02:26 +00:00
|
|
|
} else if mime == mime::IMAGE_JPEG {
|
2020-06-24 16:58:46 +00:00
|
|
|
Ok(".jpg")
|
|
|
|
} else if mime == video_mp4() {
|
|
|
|
Ok(".mp4")
|
|
|
|
} else if mime == image_webp() {
|
|
|
|
Ok(".webp")
|
2020-06-06 21:02:26 +00:00
|
|
|
} else {
|
2020-06-24 16:58:46 +00:00
|
|
|
Err(UploadError::UnsupportedFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
/// Handle responding to succesful uploads
|
2020-06-14 18:56:42 +00:00
|
|
|
#[instrument(skip(value, manager))]
|
2020-06-07 00:29:15 +00:00
|
|
|
async fn upload(
|
|
|
|
value: Value,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
2020-06-06 21:02:26 +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();
|
|
|
|
for image in images.into_iter().filter_map(|i| i.file()) {
|
2020-12-26 02:39:19 +00:00
|
|
|
if let Some(alias) = image
|
2020-06-06 21:02:26 +00:00
|
|
|
.saved_as
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|s| s.file_name())
|
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
{
|
2020-12-26 02:39:19 +00:00
|
|
|
info!("Uploaded {} as {:?}", image.filename, alias);
|
|
|
|
let delete_token = manager.delete_token(alias.to_owned()).await?;
|
|
|
|
|
|
|
|
let name = manager.from_alias(alias.to_owned()).await?;
|
|
|
|
let mut path = manager.image_dir();
|
|
|
|
path.push(name.clone());
|
|
|
|
|
|
|
|
let details = manager.variant_details(path.clone(), name.clone()).await?;
|
|
|
|
|
|
|
|
let details = if let Some(details) = details {
|
2021-08-31 17:14:23 +00:00
|
|
|
debug!("details exist");
|
2020-12-26 02:39:19 +00:00
|
|
|
details
|
|
|
|
} else {
|
2021-08-31 17:14:23 +00:00
|
|
|
debug!("generating new details from {:?}", path);
|
2020-12-26 02:39:19 +00:00
|
|
|
let new_details = Details::from_path(path.clone()).await?;
|
2021-08-31 17:14:23 +00:00
|
|
|
debug!("storing details for {:?} {}", path, name);
|
2020-12-26 02:39:19 +00:00
|
|
|
manager
|
|
|
|
.store_variant_details(path, name, &new_details)
|
|
|
|
.await?;
|
2021-08-31 17:14:23 +00:00
|
|
|
debug!("stored");
|
2020-12-26 02:39:19 +00:00
|
|
|
new_details
|
|
|
|
};
|
|
|
|
|
2020-06-07 00:29:15 +00:00
|
|
|
files.push(serde_json::json!({
|
2020-12-26 02:39:19 +00:00
|
|
|
"file": alias,
|
|
|
|
"delete_token": delete_token,
|
|
|
|
"details": details,
|
2020-06-07 00:29:15 +00:00
|
|
|
}));
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Created().json(&serde_json::json!({
|
2020-06-07 15:59:58 +00:00
|
|
|
"msg": "ok",
|
|
|
|
"files": files
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct UrlQuery {
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
2020-06-07 15:59:58 +00:00
|
|
|
/// download an image from a URL
|
2020-06-14 15:07:31 +00:00
|
|
|
#[instrument(skip(client, manager))]
|
2020-06-07 15:59:58 +00:00
|
|
|
async fn download(
|
|
|
|
client: web::Data<Client>,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
query: web::Query<UrlQuery>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let mut res = client.get(&query.url).send().await?;
|
|
|
|
|
|
|
|
if !res.status().is_success() {
|
|
|
|
return Err(UploadError::Download(res.status()));
|
|
|
|
}
|
|
|
|
|
2020-06-14 15:07:31 +00:00
|
|
|
let fut = res.body().limit(CONFIG.max_file_size() * MEGABYTES);
|
2020-06-07 15:59:58 +00:00
|
|
|
|
2021-09-04 19:20:31 +00:00
|
|
|
let stream = Box::pin(once(fut));
|
2020-06-07 15:59:58 +00:00
|
|
|
|
2021-09-04 21:01:48 +00:00
|
|
|
let permit = process_semaphore().acquire().await?;
|
2020-06-14 15:07:31 +00:00
|
|
|
let alias = manager.upload(stream).await?;
|
2021-09-04 21:01:48 +00:00
|
|
|
drop(permit);
|
2020-06-07 15:59:58 +00:00
|
|
|
let delete_token = manager.delete_token(alias.clone()).await?;
|
|
|
|
|
2021-01-05 06:28:17 +00:00
|
|
|
let name = manager.from_alias(alias.to_owned()).await?;
|
|
|
|
let mut path = manager.image_dir();
|
|
|
|
path.push(name.clone());
|
|
|
|
|
|
|
|
let details = manager.variant_details(path.clone(), name.clone()).await?;
|
|
|
|
|
|
|
|
let details = if let Some(details) = details {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
let new_details = Details::from_path(path.clone()).await?;
|
|
|
|
manager
|
|
|
|
.store_variant_details(path, name, &new_details)
|
|
|
|
.await?;
|
|
|
|
new_details
|
|
|
|
};
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Created().json(&serde_json::json!({
|
2020-06-07 15:59:58 +00:00
|
|
|
"msg": "ok",
|
|
|
|
"files": [{
|
|
|
|
"file": alias,
|
2021-01-05 06:28:17 +00:00
|
|
|
"delete_token": delete_token,
|
|
|
|
"details": details,
|
2020-06-07 15:59:58 +00:00
|
|
|
}]
|
|
|
|
})))
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 02:41:45 +00:00
|
|
|
/// Delete aliases and files
|
2020-06-14 15:07:31 +00:00
|
|
|
#[instrument(skip(manager))]
|
2020-06-07 00:29:15 +00:00
|
|
|
async fn delete(
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
path_entries: web::Path<(String, String)>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let (alias, token) = path_entries.into_inner();
|
|
|
|
|
|
|
|
manager.delete(token, alias).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
type ProcessQuery = Vec<(String, String)>;
|
|
|
|
|
2020-12-10 18:49:10 +00:00
|
|
|
async fn prepare_process(
|
2020-06-24 16:58:46 +00:00
|
|
|
query: web::Query<ProcessQuery>,
|
2020-12-10 18:49:10 +00:00
|
|
|
ext: &str,
|
|
|
|
manager: &UploadManager,
|
|
|
|
whitelist: &Option<HashSet<String>>,
|
2021-08-28 22:15:14 +00:00
|
|
|
) -> Result<(Format, String, PathBuf, Vec<String>), UploadError> {
|
2020-06-24 16:58:46 +00:00
|
|
|
let (alias, operations) =
|
|
|
|
query
|
|
|
|
.into_inner()
|
|
|
|
.into_iter()
|
|
|
|
.fold((String::new(), Vec::new()), |(s, mut acc), (k, v)| {
|
|
|
|
if k == "src" {
|
|
|
|
(v, acc)
|
|
|
|
} else {
|
|
|
|
acc.push((k, v));
|
|
|
|
(s, acc)
|
|
|
|
}
|
|
|
|
});
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
if alias == "" {
|
|
|
|
return Err(UploadError::MissingFilename);
|
|
|
|
}
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2020-06-06 22:43:33 +00:00
|
|
|
let name = manager.from_alias(alias).await?;
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
let operations = if let Some(whitelist) = whitelist.as_ref() {
|
|
|
|
operations
|
|
|
|
.into_iter()
|
|
|
|
.filter(|(k, _)| whitelist.contains(&k.to_lowercase()))
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
operations
|
|
|
|
};
|
|
|
|
|
|
|
|
let chain = self::processor::build_chain(&operations);
|
|
|
|
|
2020-11-01 20:23:56 +00:00
|
|
|
let format = ext
|
|
|
|
.parse::<Format>()
|
|
|
|
.map_err(|_| UploadError::UnsupportedFormat)?;
|
2020-06-24 16:58:46 +00:00
|
|
|
let processed_name = format!("{}.{}", name, ext);
|
|
|
|
let base = manager.image_dir();
|
2020-12-10 05:05:04 +00:00
|
|
|
let thumbnail_path = self::processor::build_path(base, &chain, processed_name);
|
2021-08-28 22:15:14 +00:00
|
|
|
let thumbnail_args = self::processor::build_args(&chain);
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2021-08-28 22:15:14 +00:00
|
|
|
Ok((format, name, thumbnail_path, thumbnail_args))
|
2020-12-10 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn process_details(
|
|
|
|
query: web::Query<ProcessQuery>,
|
|
|
|
ext: web::Path<String>,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
whitelist: web::Data<Option<HashSet<String>>>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
2021-08-28 22:15:14 +00:00
|
|
|
let (_, name, thumbnail_path, _) =
|
2020-12-10 18:49:10 +00:00
|
|
|
prepare_process(query, ext.as_str(), &manager, &whitelist).await?;
|
|
|
|
|
|
|
|
let details = manager.variant_details(thumbnail_path, name).await?;
|
|
|
|
|
|
|
|
let details = details.ok_or(UploadError::NoFiles)?;
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&details))
|
2020-12-10 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Process files
|
|
|
|
#[instrument(skip(manager, whitelist))]
|
|
|
|
async fn process(
|
2021-01-18 23:11:32 +00:00
|
|
|
range: Option<range::RangeHeader>,
|
2020-12-10 18:49:10 +00:00
|
|
|
query: web::Query<ProcessQuery>,
|
|
|
|
ext: web::Path<String>,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
whitelist: web::Data<Option<HashSet<String>>>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
2021-08-28 22:15:14 +00:00
|
|
|
let (format, name, thumbnail_path, thumbnail_args) =
|
2020-12-10 18:49:10 +00:00
|
|
|
prepare_process(query, ext.as_str(), &manager, &whitelist).await?;
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// If the thumbnail doesn't exist, we need to create it
|
2021-09-04 19:20:31 +00:00
|
|
|
let thumbnail_exists = if let Err(e) = tokio::fs::metadata(&thumbnail_path).await {
|
|
|
|
if e.kind() != std::io::ErrorKind::NotFound {
|
2020-06-07 17:51:45 +00:00
|
|
|
error!("Error looking up processed image, {}", e);
|
2020-06-06 21:02:26 +00:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2020-12-10 05:05:04 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2020-12-10 05:05:04 +00:00
|
|
|
let details = manager
|
|
|
|
.variant_details(thumbnail_path.clone(), name.clone())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
if !thumbnail_exists || details.is_none() {
|
2020-06-06 21:41:17 +00:00
|
|
|
let mut original_path = manager.image_dir();
|
2020-06-06 21:02:26 +00:00
|
|
|
original_path.push(name.clone());
|
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
let thumbnail_path2 = thumbnail_path.clone();
|
|
|
|
let process_fut = async {
|
|
|
|
let thumbnail_path = thumbnail_path2;
|
|
|
|
// Create and save a JPG for motion images (gif, mp4)
|
|
|
|
if let Some((updated_path, exists)) =
|
|
|
|
self::processor::prepare_image(original_path.clone()).await?
|
|
|
|
{
|
|
|
|
original_path = updated_path.clone();
|
|
|
|
|
|
|
|
if exists.is_new() {
|
|
|
|
// Save the transcoded file in another task
|
|
|
|
debug!("Spawning storage task");
|
|
|
|
let span = Span::current();
|
|
|
|
let manager2 = manager.clone();
|
|
|
|
let name = name.clone();
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
let entered = span.enter();
|
|
|
|
if let Err(e) = manager2.store_variant(updated_path, name).await {
|
|
|
|
error!("Error storing variant, {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
drop(entered);
|
|
|
|
});
|
|
|
|
}
|
2020-06-16 23:13:22 +00:00
|
|
|
}
|
2020-06-24 16:58:46 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
let permit = process_semaphore().acquire().await?;
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
let file = tokio::fs::File::open(original_path.clone()).await?;
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
let mut processed_reader =
|
|
|
|
crate::magick::process_image_write_read(file, thumbnail_args, format)?;
|
2021-09-04 21:01:48 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
let mut vec = Vec::new();
|
|
|
|
processed_reader.read_to_end(&mut vec).await?;
|
|
|
|
let bytes = web::Bytes::from(vec);
|
2021-08-31 02:19:47 +00:00
|
|
|
|
2021-09-05 22:15:30 +00:00
|
|
|
drop(permit);
|
|
|
|
|
|
|
|
let details = if let Some(details) = details {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
Details::from_bytes(bytes.clone()).await?
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = tracing::Span::current();
|
|
|
|
let details2 = details.clone();
|
|
|
|
let bytes2 = bytes.clone();
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
let entered = span.enter();
|
|
|
|
if let Err(e) = safe_save_file(thumbnail_path.clone(), bytes2).await {
|
|
|
|
tracing::warn!("Error saving thumbnail: {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Err(e) = manager
|
|
|
|
.store_variant_details(thumbnail_path.clone(), name.clone(), &details2)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
tracing::warn!("Error saving variant details: {}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Err(e) = manager.store_variant(thumbnail_path, name.clone()).await {
|
|
|
|
tracing::warn!("Error saving variant info: {}", e);
|
|
|
|
}
|
|
|
|
drop(entered);
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok((details, bytes)) as Result<(Details, web::Bytes), UploadError>
|
2020-12-10 18:49:10 +00:00
|
|
|
};
|
|
|
|
|
2021-09-06 00:05:58 +00:00
|
|
|
let (details, bytes) =
|
|
|
|
CancelSafeProcessor::new(thumbnail_path.clone(), Box::pin(process_fut)).await?;
|
2021-08-31 02:19:47 +00:00
|
|
|
|
|
|
|
return Ok(srv_response(
|
|
|
|
HttpResponse::Ok(),
|
2021-09-04 19:20:31 +00:00
|
|
|
once(ready(Ok(bytes) as Result<_, UploadError>)),
|
2021-08-31 02:19:47 +00:00
|
|
|
details.content_type(),
|
|
|
|
7 * DAYS,
|
|
|
|
details.system_time(),
|
|
|
|
));
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 18:49:10 +00:00
|
|
|
let details = if let Some(details) = details {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
let details = Details::from_path(thumbnail_path.clone()).await?;
|
|
|
|
manager
|
|
|
|
.store_variant_details(thumbnail_path.clone(), name, &details)
|
|
|
|
.await?;
|
|
|
|
details
|
|
|
|
};
|
2020-12-10 05:05:04 +00:00
|
|
|
|
2021-01-18 23:11:32 +00:00
|
|
|
ranged_file_resp(thumbnail_path, range, details).await
|
2020-06-24 16:58:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 18:49:10 +00:00
|
|
|
/// Fetch file details
|
|
|
|
async fn details(
|
|
|
|
alias: web::Path<String>,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let name = manager.from_alias(alias.into_inner()).await?;
|
|
|
|
let mut path = manager.image_dir();
|
|
|
|
path.push(name.clone());
|
|
|
|
|
|
|
|
let details = manager.variant_details(path.clone(), name.clone()).await?;
|
|
|
|
|
|
|
|
let details = if let Some(details) = details {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
let new_details = Details::from_path(path.clone()).await?;
|
|
|
|
manager
|
|
|
|
.store_variant_details(path.clone(), name, &new_details)
|
|
|
|
.await?;
|
|
|
|
new_details
|
|
|
|
};
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&details))
|
2020-12-10 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 16:58:46 +00:00
|
|
|
/// Serve files
|
|
|
|
#[instrument(skip(manager))]
|
|
|
|
async fn serve(
|
2021-01-18 23:11:32 +00:00
|
|
|
range: Option<range::RangeHeader>,
|
2020-06-24 16:58:46 +00:00
|
|
|
alias: web::Path<String>,
|
|
|
|
manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let name = manager.from_alias(alias.into_inner()).await?;
|
|
|
|
let mut path = manager.image_dir();
|
2020-12-10 05:05:04 +00:00
|
|
|
path.push(name.clone());
|
|
|
|
|
|
|
|
let details = manager.variant_details(path.clone(), name.clone()).await?;
|
|
|
|
|
2020-12-10 18:49:10 +00:00
|
|
|
let details = if let Some(details) = details {
|
|
|
|
details
|
|
|
|
} else {
|
|
|
|
let details = Details::from_path(path.clone()).await?;
|
|
|
|
manager
|
|
|
|
.store_variant_details(path.clone(), name, &details)
|
|
|
|
.await?;
|
|
|
|
details
|
|
|
|
};
|
2020-06-24 16:58:46 +00:00
|
|
|
|
2021-01-18 23:11:32 +00:00
|
|
|
ranged_file_resp(path, range, details).await
|
2021-01-14 13:52:11 +00:00
|
|
|
}
|
2021-01-14 01:08:04 +00:00
|
|
|
|
2021-01-14 16:26:17 +00:00
|
|
|
async fn ranged_file_resp(
|
|
|
|
path: PathBuf,
|
2021-01-18 23:11:32 +00:00
|
|
|
range: Option<range::RangeHeader>,
|
2021-01-14 16:26:17 +00:00
|
|
|
details: Details,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
2021-01-18 23:11:32 +00:00
|
|
|
let (builder, stream) = match range {
|
2021-01-14 13:52:11 +00:00
|
|
|
//Range header exists - return as ranged
|
2021-01-18 23:11:32 +00:00
|
|
|
Some(range_header) => {
|
|
|
|
if !range_header.is_bytes() {
|
2021-01-14 13:52:11 +00:00
|
|
|
return Err(UploadError::Range);
|
|
|
|
}
|
2021-01-14 01:08:04 +00:00
|
|
|
|
2021-01-18 23:11:32 +00:00
|
|
|
if range_header.is_empty() {
|
|
|
|
return Err(UploadError::Range);
|
|
|
|
} else if range_header.len() == 1 {
|
2021-09-04 19:20:31 +00:00
|
|
|
let file = tokio::fs::File::open(path).await?;
|
2021-01-18 23:11:32 +00:00
|
|
|
|
2021-09-04 19:20:31 +00:00
|
|
|
let meta = file.metadata().await?;
|
2021-01-18 23:11:32 +00:00
|
|
|
|
|
|
|
let range = range_header.ranges().next().unwrap();
|
|
|
|
|
|
|
|
let mut builder = HttpResponse::PartialContent();
|
2021-02-10 22:57:42 +00:00
|
|
|
builder.insert_header(range.to_content_range(meta.len()));
|
2021-01-14 01:08:04 +00:00
|
|
|
|
2021-01-18 23:11:32 +00:00
|
|
|
(builder, range.chop_file(file).await?)
|
|
|
|
} else {
|
|
|
|
return Err(UploadError::Range);
|
|
|
|
}
|
2021-01-14 01:08:04 +00:00
|
|
|
}
|
|
|
|
//No Range header in the request - return the entire document
|
|
|
|
None => {
|
2021-09-04 19:20:31 +00:00
|
|
|
let file = tokio::fs::File::open(path).await?;
|
|
|
|
let stream = Box::pin(crate::stream::bytes_stream(file)) as LocalBoxStream<'_, _>;
|
2021-01-18 23:11:32 +00:00
|
|
|
(HttpResponse::Ok(), stream)
|
2021-01-14 01:08:04 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-18 23:11:32 +00:00
|
|
|
|
|
|
|
Ok(srv_response(
|
|
|
|
builder,
|
|
|
|
stream,
|
|
|
|
details.content_type(),
|
|
|
|
7 * DAYS,
|
|
|
|
details.system_time(),
|
|
|
|
))
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// A helper method to produce responses with proper cache headers
|
2020-10-02 20:38:52 +00:00
|
|
|
fn srv_response<S, E>(
|
2021-01-18 23:11:32 +00:00
|
|
|
mut builder: HttpResponseBuilder,
|
2020-10-02 20:38:52 +00:00
|
|
|
stream: S,
|
|
|
|
ext: mime::Mime,
|
|
|
|
expires: u32,
|
|
|
|
modified: SystemTime,
|
|
|
|
) -> HttpResponse
|
2020-06-06 21:02:26 +00:00
|
|
|
where
|
2021-02-10 22:57:42 +00:00
|
|
|
S: Stream<Item = Result<web::Bytes, E>> + Unpin + 'static,
|
2021-06-19 19:39:41 +00:00
|
|
|
E: std::error::Error + 'static,
|
2021-01-18 23:11:32 +00:00
|
|
|
actix_web::Error: From<E>,
|
2021-01-14 01:08:04 +00:00
|
|
|
{
|
2021-01-18 23:11:32 +00:00
|
|
|
builder
|
2021-02-10 22:57:42 +00:00
|
|
|
.insert_header(LastModified(modified.into()))
|
|
|
|
.insert_header(CacheControl(vec![
|
2021-01-14 01:08:04 +00:00
|
|
|
CacheDirective::Public,
|
|
|
|
CacheDirective::MaxAge(expires),
|
|
|
|
CacheDirective::Extension("immutable".to_owned(), None),
|
|
|
|
]))
|
2021-02-10 22:57:42 +00:00
|
|
|
.insert_header((ACCEPT_RANGES, "bytes"))
|
2020-06-06 21:02:26 +00:00
|
|
|
.content_type(ext.to_string())
|
2021-01-18 23:11:32 +00:00
|
|
|
.streaming(stream)
|
2020-06-06 21:02:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 22:22:45 +00:00
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum FileOrAlias {
|
|
|
|
File { file: String },
|
|
|
|
Alias { alias: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn purge(
|
|
|
|
query: web::Query<FileOrAlias>,
|
|
|
|
upload_manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let aliases = match query.into_inner() {
|
|
|
|
FileOrAlias::File { file } => upload_manager.aliases_by_filename(file).await?,
|
|
|
|
FileOrAlias::Alias { alias } => upload_manager.aliases_by_alias(alias).await?,
|
|
|
|
};
|
|
|
|
|
|
|
|
for alias in aliases.iter() {
|
|
|
|
upload_manager
|
|
|
|
.delete_without_token(alias.to_owned())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
2020-07-11 22:22:45 +00:00
|
|
|
"msg": "ok",
|
|
|
|
"aliases": aliases
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn aliases(
|
|
|
|
query: web::Query<FileOrAlias>,
|
|
|
|
upload_manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let aliases = match query.into_inner() {
|
|
|
|
FileOrAlias::File { file } => upload_manager.aliases_by_filename(file).await?,
|
|
|
|
FileOrAlias::Alias { alias } => upload_manager.aliases_by_alias(alias).await?,
|
|
|
|
};
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
2020-07-11 22:22:45 +00:00
|
|
|
"msg": "ok",
|
|
|
|
"aliases": aliases,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
|
struct ByAlias {
|
|
|
|
alias: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn filename_by_alias(
|
|
|
|
query: web::Query<ByAlias>,
|
|
|
|
upload_manager: web::Data<UploadManager>,
|
|
|
|
) -> Result<HttpResponse, UploadError> {
|
|
|
|
let filename = upload_manager.from_alias(query.into_inner().alias).await?;
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
Ok(HttpResponse::Ok().json(&serde_json::json!({
|
2020-07-11 22:22:45 +00:00
|
|
|
"msg": "ok",
|
|
|
|
"filename": filename,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2020-06-06 21:02:26 +00:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> Result<(), anyhow::Error> {
|
2020-06-14 02:28:06 +00:00
|
|
|
if std::env::var("RUST_LOG").is_err() {
|
|
|
|
std::env::set_var("RUST_LOG", "info");
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_env_filter(EnvFilter::from_default_env())
|
|
|
|
.init();
|
|
|
|
|
2020-06-14 15:07:31 +00:00
|
|
|
let manager = UploadManager::new(CONFIG.data_dir(), CONFIG.format()).await?;
|
2020-06-06 21:02:26 +00:00
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
// Create a new Multipart Form validator
|
|
|
|
//
|
|
|
|
// This form is expecting a single array field, 'images' with at most 10 files in it
|
2020-06-06 21:02:26 +00:00
|
|
|
let manager2 = manager.clone();
|
|
|
|
let form = Form::new()
|
|
|
|
.max_files(10)
|
2020-06-14 15:07:31 +00:00
|
|
|
.max_file_size(CONFIG.max_file_size() * MEGABYTES)
|
2020-06-13 19:04:33 +00:00
|
|
|
.transform_error(|e| UploadError::from(e).into())
|
2020-06-06 21:02:26 +00:00
|
|
|
.field(
|
|
|
|
"images",
|
2020-06-14 18:56:42 +00:00
|
|
|
Field::array(Field::file(move |filename, _, stream| {
|
2020-06-06 21:02:26 +00:00
|
|
|
let manager = manager2.clone();
|
|
|
|
|
2020-06-07 15:59:58 +00:00
|
|
|
async move {
|
2020-06-14 18:56:42 +00:00
|
|
|
let span = tracing::info_span!("file-upload", ?filename);
|
|
|
|
let entered = span.enter();
|
|
|
|
|
2021-09-04 21:01:48 +00:00
|
|
|
let permit = process_semaphore().acquire().await?;
|
|
|
|
|
2020-06-14 18:56:42 +00:00
|
|
|
let res = manager.upload(stream).await.map(|alias| {
|
2020-06-07 15:59:58 +00:00
|
|
|
let mut path = PathBuf::new();
|
|
|
|
path.push(alias);
|
|
|
|
Some(path)
|
2020-06-14 18:56:42 +00:00
|
|
|
});
|
2021-09-04 21:01:48 +00:00
|
|
|
|
|
|
|
drop(permit);
|
2020-06-14 18:56:42 +00:00
|
|
|
drop(entered);
|
|
|
|
res
|
2020-06-07 15:59:58 +00:00
|
|
|
}
|
2020-06-06 21:02:26 +00:00
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
2020-06-11 00:26:54 +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
|
2020-06-14 15:07:31 +00:00
|
|
|
let validate_imports = CONFIG.validate_imports();
|
2020-06-11 00:26:54 +00:00
|
|
|
let manager2 = manager.clone();
|
|
|
|
let import_form = Form::new()
|
|
|
|
.max_files(10)
|
2020-06-14 15:07:31 +00:00
|
|
|
.max_file_size(CONFIG.max_file_size() * MEGABYTES)
|
2020-06-13 19:04:33 +00:00
|
|
|
.transform_error(|e| UploadError::from(e).into())
|
2020-06-11 00:26:54 +00:00
|
|
|
.field(
|
|
|
|
"images",
|
|
|
|
Field::array(Field::file(move |filename, content_type, stream| {
|
|
|
|
let manager = manager2.clone();
|
|
|
|
|
|
|
|
async move {
|
2020-06-14 18:56:42 +00:00
|
|
|
let span = tracing::info_span!("file-import", ?filename);
|
|
|
|
let entered = span.enter();
|
|
|
|
|
2021-09-04 21:01:48 +00:00
|
|
|
let permit = process_semaphore().acquire().await?;
|
|
|
|
|
2020-06-14 18:56:42 +00:00
|
|
|
let res = manager
|
2020-06-14 15:07:31 +00:00
|
|
|
.import(filename, content_type, validate_imports, stream)
|
2020-06-11 00:26:54 +00:00
|
|
|
.await
|
|
|
|
.map(|alias| {
|
|
|
|
let mut path = PathBuf::new();
|
|
|
|
path.push(alias);
|
|
|
|
Some(path)
|
2020-06-14 18:56:42 +00:00
|
|
|
});
|
2021-09-04 21:01:48 +00:00
|
|
|
|
|
|
|
drop(permit);
|
2020-06-14 18:56:42 +00:00
|
|
|
drop(entered);
|
|
|
|
res
|
2020-06-11 00:26:54 +00:00
|
|
|
}
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
2020-06-06 21:02:26 +00:00
|
|
|
HttpServer::new(move || {
|
2020-09-14 15:52:18 +00:00
|
|
|
let client = Client::builder()
|
2020-06-07 15:59:58 +00:00
|
|
|
.header("User-Agent", "pict-rs v0.1.0-master")
|
|
|
|
.finish();
|
|
|
|
|
2020-06-06 21:02:26 +00:00
|
|
|
App::new()
|
2020-06-14 02:28:06 +00:00
|
|
|
.wrap(Logger::default())
|
|
|
|
.wrap(Tracing)
|
2021-06-26 18:10:28 +00:00
|
|
|
.app_data(web::Data::new(manager.clone()))
|
|
|
|
.app_data(web::Data::new(client))
|
|
|
|
.app_data(web::Data::new(CONFIG.filter_whitelist()))
|
2020-06-06 21:02:26 +00:00
|
|
|
.service(
|
|
|
|
web::scope("/image")
|
|
|
|
.service(
|
|
|
|
web::resource("")
|
|
|
|
.guard(guard::Post())
|
|
|
|
.wrap(form.clone())
|
|
|
|
.route(web::post().to(upload)),
|
|
|
|
)
|
2020-06-07 15:59:58 +00:00
|
|
|
.service(web::resource("/download").route(web::get().to(download)))
|
2020-06-07 00:29:15 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/delete/{delete_token}/{filename}")
|
2020-06-09 22:37:48 +00:00
|
|
|
.route(web::delete().to(delete))
|
|
|
|
.route(web::get().to(delete)),
|
2020-06-07 00:29:15 +00:00
|
|
|
)
|
2020-06-24 16:58:46 +00:00
|
|
|
.service(web::resource("/original/{filename}").route(web::get().to(serve)))
|
2020-12-10 18:49:10 +00:00
|
|
|
.service(web::resource("/process.{ext}").route(web::get().to(process)))
|
|
|
|
.service(
|
|
|
|
web::scope("/details")
|
|
|
|
.service(
|
|
|
|
web::resource("/original/{filename}").route(web::get().to(details)),
|
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/process.{ext}")
|
|
|
|
.route(web::get().to(process_details)),
|
|
|
|
),
|
|
|
|
),
|
2020-06-06 21:02:26 +00:00
|
|
|
)
|
2020-06-11 00:26:54 +00:00
|
|
|
.service(
|
2020-07-11 21:28:49 +00:00
|
|
|
web::scope("/internal")
|
|
|
|
.wrap(Internal(CONFIG.api_key().map(|s| s.to_owned())))
|
|
|
|
.service(
|
|
|
|
web::resource("/import")
|
|
|
|
.wrap(import_form.clone())
|
|
|
|
.route(web::post().to(upload)),
|
2020-07-11 22:22:45 +00:00
|
|
|
)
|
|
|
|
.service(web::resource("/purge").route(web::post().to(purge)))
|
|
|
|
.service(web::resource("/aliases").route(web::get().to(aliases)))
|
|
|
|
.service(web::resource("/filename").route(web::get().to(filename_by_alias))),
|
2020-06-11 00:26:54 +00:00
|
|
|
)
|
2020-06-06 21:02:26 +00:00
|
|
|
})
|
2020-06-14 15:07:31 +00:00
|
|
|
.bind(CONFIG.bind_address())?
|
2020-06-06 21:02:26 +00:00
|
|
|
.run()
|
|
|
|
.await?;
|
|
|
|
|
2021-09-04 19:20:31 +00:00
|
|
|
if tokio::fs::metadata(&*TMP_DIR).await.is_ok() {
|
|
|
|
tokio::fs::remove_dir_all(&*TMP_DIR).await?;
|
2021-01-05 17:51:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 21:02:26 +00:00
|
|
|
Ok(())
|
|
|
|
}
|