2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00

Return details in upload method, clippy nits

This commit is contained in:
asonix 2020-12-25 20:39:19 -06:00
parent 5dc4af366a
commit 0acdfffe08
7 changed files with 60 additions and 52 deletions

View file

@ -87,7 +87,7 @@ impl Config {
} }
pub(crate) fn api_key(&self) -> Option<&str> { pub(crate) fn api_key(&self) -> Option<&str> {
self.api_key.as_ref().map(|s| s.as_str()) self.api_key.as_deref()
} }
} }

View file

@ -35,7 +35,7 @@ const MINUTES: u32 = 60;
const HOURS: u32 = 60 * MINUTES; const HOURS: u32 = 60 * MINUTES;
const DAYS: u32 = 24 * HOURS; const DAYS: u32 = 24 * HOURS;
static CONFIG: Lazy<Config> = Lazy::new(|| Config::from_args()); static CONFIG: Lazy<Config> = Lazy::new(Config::from_args);
static MAGICK_INIT: Once = Once::new(); static MAGICK_INIT: Once = Once::new();
// try moving a file // try moving a file
@ -150,17 +150,35 @@ async fn upload(
let mut files = Vec::new(); let mut files = Vec::new();
for image in images.into_iter().filter_map(|i| i.file()) { for image in images.into_iter().filter_map(|i| i.file()) {
if let Some(saved_as) = image if let Some(alias) = image
.saved_as .saved_as
.as_ref() .as_ref()
.and_then(|s| s.file_name()) .and_then(|s| s.file_name())
.and_then(|s| s.to_str()) .and_then(|s| s.to_str())
{ {
info!("Uploaded {} as {:?}", image.filename, saved_as); info!("Uploaded {} as {:?}", image.filename, alias);
let delete_token = manager.delete_token(saved_as.to_owned()).await?; 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 {
details
} else {
let new_details = Details::from_path(path.clone()).await?;
manager
.store_variant_details(path, name, &new_details)
.await?;
new_details
};
files.push(serde_json::json!({ files.push(serde_json::json!({
"file": saved_as, "file": alias,
"delete_token": delete_token "delete_token": delete_token,
"details": details,
})); }));
} }
} }

View file

@ -103,8 +103,7 @@ where
fn call(&mut self, req: S::Request) -> Self::Future { fn call(&mut self, req: S::Request) -> Self::Future {
if let Some(value) = req.headers().get("x-api-token") { if let Some(value) = req.headers().get("x-api-token") {
if value.to_str().is_ok() && value.to_str().ok() == self.0.as_ref().map(|s| s.as_str()) if value.to_str().is_ok() && value.to_str().ok() == self.0.as_deref() {
{
let fut = self.1.call(req); let fut = self.1.call(req);
return Box::pin(async move { fut.await }); return Box::pin(async move { fut.await });
} }

View file

@ -1,5 +1,4 @@
use crate::UploadError; use crate::UploadError;
use sled;
use std::path::PathBuf; use std::path::PathBuf;
mod s034; mod s034;
@ -83,7 +82,7 @@ enum DbVersion {
impl DbVersion { impl DbVersion {
fn exists(root: PathBuf) -> Self { fn exists(root: PathBuf) -> Self {
if s034::exists(root.clone()) && !s034::migrating(root.clone()) { if s034::exists(root.clone()) && !s034::migrating(root) {
return DbVersion::Sled034; return DbVersion::Sled034;
} }

View file

@ -304,7 +304,7 @@ impl std::fmt::Debug for ProcessChain {
#[instrument] #[instrument]
pub(crate) fn build_chain(args: &[(String, String)]) -> ProcessChain { pub(crate) fn build_chain(args: &[(String, String)]) -> ProcessChain {
let inner = args let inner = args
.into_iter() .iter()
.filter_map(|(k, v)| { .filter_map(|(k, v)| {
let k = k.as_str(); let k = k.as_str();
let v = v.as_str(); let v = v.as_str();
@ -345,10 +345,7 @@ pub(crate) enum Exists {
impl Exists { impl Exists {
pub(crate) fn is_new(&self) -> bool { pub(crate) fn is_new(&self) -> bool {
match self { matches!(self, Exists::New)
Exists::New => true,
_ => false,
}
} }
} }
@ -417,7 +414,7 @@ pub(crate) async fn process_image(
let vec = wand.op(|w| w.write_image_blob(format.to_magick_format()))?; let vec = wand.op(|w| w.write_image_blob(format.to_magick_format()))?;
drop(entered); drop(entered);
return Ok(Bytes::from(vec)) as Result<Bytes, UploadError>; Ok(Bytes::from(vec)) as Result<Bytes, UploadError>
}) })
.await?; .await?;

View file

@ -57,36 +57,34 @@ impl<T> Serde<T> {
} }
} }
mod my_serde { impl<T> serde::Serialize for Serde<T>
impl<T> serde::Serialize for super::Serde<T> where
T: std::fmt::Display,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
T: std::fmt::Display, S: serde::Serializer,
{ {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> let s = self.inner.to_string();
where serde::Serialize::serialize(s.as_str(), serializer)
S: serde::Serializer,
{
let s = self.inner.to_string();
serde::Serialize::serialize(s.as_str(), serializer)
}
} }
}
impl<'de, T> serde::Deserialize<'de> for super::Serde<T> impl<'de, T> serde::Deserialize<'de> for Serde<T>
where
T: std::str::FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
T: std::str::FromStr, D: serde::Deserializer<'de>,
<T as std::str::FromStr>::Err: std::fmt::Display,
{ {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> let s: String = serde::Deserialize::deserialize(deserializer)?;
where let inner = s
D: serde::Deserializer<'de>, .parse::<T>()
{ .map_err(|e| serde::de::Error::custom(e.to_string()))?;
let s: String = serde::Deserialize::deserialize(deserializer)?;
let inner = s
.parse::<T>()
.map_err(|e| serde::de::Error::custom(e.to_string()))?;
Ok(super::Serde { inner }) Ok(Serde { inner })
}
} }
} }
@ -198,10 +196,7 @@ enum Dup {
impl Dup { impl Dup {
fn exists(&self) -> bool { fn exists(&self) -> bool {
match self { matches!(self, Dup::Exists)
Dup::Exists => true,
_ => false,
}
} }
} }
@ -392,7 +387,7 @@ impl UploadManager {
debug!("Deleting alias -> delete-token mapping"); debug!("Deleting alias -> delete-token mapping");
let existing_token = alias_tree let existing_token = alias_tree
.remove(delete_key(&alias2).as_bytes())? .remove(delete_key(&alias2).as_bytes())?
.ok_or(trans_err(UploadError::MissingAlias))?; .ok_or_else(|| trans_err(UploadError::MissingAlias))?;
// Bail if invalid token // Bail if invalid token
if existing_token != token { if existing_token != token {
@ -404,14 +399,14 @@ impl UploadManager {
debug!("Deleting alias -> id mapping"); debug!("Deleting alias -> id mapping");
let id = alias_tree let id = alias_tree
.remove(alias_id_key(&alias2).as_bytes())? .remove(alias_id_key(&alias2).as_bytes())?
.ok_or(trans_err(UploadError::MissingAlias))?; .ok_or_else(|| trans_err(UploadError::MissingAlias))?;
let id = String::from_utf8(id.to_vec()).map_err(|e| trans_err(e.into()))?; let id = String::from_utf8(id.to_vec()).map_err(|e| trans_err(e.into()))?;
// -- GET HASH FOR HASH TREE CLEANUP -- // -- GET HASH FOR HASH TREE CLEANUP --
debug!("Deleting alias -> hash mapping"); debug!("Deleting alias -> hash mapping");
let hash = alias_tree let hash = alias_tree
.remove(alias2.as_bytes())? .remove(alias2.as_bytes())?
.ok_or(trans_err(UploadError::MissingAlias))?; .ok_or_else(|| trans_err(UploadError::MissingAlias))?;
// -- REMOVE HASH TREE ELEMENT -- // -- REMOVE HASH TREE ELEMENT --
debug!("Deleting hash -> alias mapping"); debug!("Deleting hash -> alias mapping");
@ -877,7 +872,7 @@ impl UploadManager {
return Ok(Err(UploadError::DuplicateAlias)); return Ok(Err(UploadError::DuplicateAlias));
} }
return Ok(Ok(())); Ok(Ok(()))
} }
} }

View file

@ -29,7 +29,7 @@ impl Op for MagickWand {
Err(e) => { Err(e) => {
if let Ok(e) = self.get_exception() { if let Ok(e) = self.get_exception() {
error!("WandError: {}", e.0); error!("WandError: {}", e.0);
Err(UploadError::Wand(e.0.to_owned())) Err(UploadError::Wand(e.0))
} else { } else {
Err(UploadError::Wand(e.to_owned())) Err(UploadError::Wand(e.to_owned()))
} }
@ -46,7 +46,7 @@ impl Op for MagickWand {
Err(e) => { Err(e) => {
if let Ok(e) = self.get_exception() { if let Ok(e) = self.get_exception() {
error!("WandError: {}", e.0); error!("WandError: {}", e.0);
Err(UploadError::Wand(e.0.to_owned())) Err(UploadError::Wand(e.0))
} else { } else {
Err(UploadError::Wand(e.to_owned())) Err(UploadError::Wand(e.to_owned()))
} }
@ -152,7 +152,7 @@ pub(crate) async fn validate_image(
if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) { if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) {
std::fs::remove_file(&newfile_str)?; std::fs::remove_file(&newfile_str)?;
return Err(e.into()); return Err(e);
} }
} }
@ -175,7 +175,7 @@ pub(crate) async fn validate_image(
debug!("writing: {}", newfile_str); debug!("writing: {}", newfile_str);
if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) { if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) {
std::fs::remove_file(&newfile_str)?; std::fs::remove_file(&newfile_str)?;
return Err(e.into()); return Err(e);
} }
} }