mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 19:31:35 +00:00
Return details in upload method, clippy nits
This commit is contained in:
parent
5dc4af366a
commit
0acdfffe08
7 changed files with 60 additions and 52 deletions
|
@ -87,7 +87,7 @@ impl Config {
|
|||
}
|
||||
|
||||
pub(crate) fn api_key(&self) -> Option<&str> {
|
||||
self.api_key.as_ref().map(|s| s.as_str())
|
||||
self.api_key.as_deref()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -35,7 +35,7 @@ const MINUTES: u32 = 60;
|
|||
const HOURS: u32 = 60 * MINUTES;
|
||||
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();
|
||||
|
||||
// try moving a file
|
||||
|
@ -150,17 +150,35 @@ async fn upload(
|
|||
|
||||
let mut files = Vec::new();
|
||||
for image in images.into_iter().filter_map(|i| i.file()) {
|
||||
if let Some(saved_as) = image
|
||||
if let Some(alias) = image
|
||||
.saved_as
|
||||
.as_ref()
|
||||
.and_then(|s| s.file_name())
|
||||
.and_then(|s| s.to_str())
|
||||
{
|
||||
info!("Uploaded {} as {:?}", image.filename, saved_as);
|
||||
let delete_token = manager.delete_token(saved_as.to_owned()).await?;
|
||||
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 {
|
||||
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!({
|
||||
"file": saved_as,
|
||||
"delete_token": delete_token
|
||||
"file": alias,
|
||||
"delete_token": delete_token,
|
||||
"details": details,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,8 +103,7 @@ where
|
|||
|
||||
fn call(&mut self, req: S::Request) -> Self::Future {
|
||||
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);
|
||||
return Box::pin(async move { fut.await });
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::UploadError;
|
||||
use sled;
|
||||
use std::path::PathBuf;
|
||||
|
||||
mod s034;
|
||||
|
@ -83,7 +82,7 @@ enum DbVersion {
|
|||
|
||||
impl DbVersion {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -304,7 +304,7 @@ impl std::fmt::Debug for ProcessChain {
|
|||
#[instrument]
|
||||
pub(crate) fn build_chain(args: &[(String, String)]) -> ProcessChain {
|
||||
let inner = args
|
||||
.into_iter()
|
||||
.iter()
|
||||
.filter_map(|(k, v)| {
|
||||
let k = k.as_str();
|
||||
let v = v.as_str();
|
||||
|
@ -345,10 +345,7 @@ pub(crate) enum Exists {
|
|||
|
||||
impl Exists {
|
||||
pub(crate) fn is_new(&self) -> bool {
|
||||
match self {
|
||||
Exists::New => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, Exists::New)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -417,7 +414,7 @@ pub(crate) async fn process_image(
|
|||
|
||||
let vec = wand.op(|w| w.write_image_blob(format.to_magick_format()))?;
|
||||
drop(entered);
|
||||
return Ok(Bytes::from(vec)) as Result<Bytes, UploadError>;
|
||||
Ok(Bytes::from(vec)) as Result<Bytes, UploadError>
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
|
|
@ -57,36 +57,34 @@ impl<T> Serde<T> {
|
|||
}
|
||||
}
|
||||
|
||||
mod my_serde {
|
||||
impl<T> serde::Serialize for super::Serde<T>
|
||||
impl<T> serde::Serialize for Serde<T>
|
||||
where
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
T: std::fmt::Display,
|
||||
S: serde::Serializer,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let s = self.inner.to_string();
|
||||
serde::Serialize::serialize(s.as_str(), 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
|
||||
T: std::str::FromStr,
|
||||
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let s: String = serde::Deserialize::deserialize(deserializer)?;
|
||||
let inner = s
|
||||
.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 {
|
||||
fn exists(&self) -> bool {
|
||||
match self {
|
||||
Dup::Exists => true,
|
||||
_ => false,
|
||||
}
|
||||
matches!(self, Dup::Exists)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -392,7 +387,7 @@ impl UploadManager {
|
|||
debug!("Deleting alias -> delete-token mapping");
|
||||
let existing_token = alias_tree
|
||||
.remove(delete_key(&alias2).as_bytes())?
|
||||
.ok_or(trans_err(UploadError::MissingAlias))?;
|
||||
.ok_or_else(|| trans_err(UploadError::MissingAlias))?;
|
||||
|
||||
// Bail if invalid token
|
||||
if existing_token != token {
|
||||
|
@ -404,14 +399,14 @@ impl UploadManager {
|
|||
debug!("Deleting alias -> id mapping");
|
||||
let id = alias_tree
|
||||
.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()))?;
|
||||
|
||||
// -- GET HASH FOR HASH TREE CLEANUP --
|
||||
debug!("Deleting alias -> hash mapping");
|
||||
let hash = alias_tree
|
||||
.remove(alias2.as_bytes())?
|
||||
.ok_or(trans_err(UploadError::MissingAlias))?;
|
||||
.ok_or_else(|| trans_err(UploadError::MissingAlias))?;
|
||||
|
||||
// -- REMOVE HASH TREE ELEMENT --
|
||||
debug!("Deleting hash -> alias mapping");
|
||||
|
@ -877,7 +872,7 @@ impl UploadManager {
|
|||
return Ok(Err(UploadError::DuplicateAlias));
|
||||
}
|
||||
|
||||
return Ok(Ok(()));
|
||||
Ok(Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ impl Op for MagickWand {
|
|||
Err(e) => {
|
||||
if let Ok(e) = self.get_exception() {
|
||||
error!("WandError: {}", e.0);
|
||||
Err(UploadError::Wand(e.0.to_owned()))
|
||||
Err(UploadError::Wand(e.0))
|
||||
} else {
|
||||
Err(UploadError::Wand(e.to_owned()))
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ impl Op for MagickWand {
|
|||
Err(e) => {
|
||||
if let Ok(e) = self.get_exception() {
|
||||
error!("WandError: {}", e.0);
|
||||
Err(UploadError::Wand(e.0.to_owned()))
|
||||
Err(UploadError::Wand(e.0))
|
||||
} else {
|
||||
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)) {
|
||||
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);
|
||||
if let Err(e) = wand.op(|w| w.write_image(&newfile_str)) {
|
||||
std::fs::remove_file(&newfile_str)?;
|
||||
return Err(e.into());
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue