Skip to content

Commit

Permalink
remove extensions from URLs when generating upload URLs, add defaults…
Browse files Browse the repository at this point in the history
… for known file types
  • Loading branch information
jesseditson committed Jun 15, 2024
1 parent 0acf9bf commit 9ffa2d2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/fields/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum InvalidFieldError {
ReservedObjectNameError(String),
#[error("cannot create type {0} from a string value")]
UnsupportedStringValue(String),
#[error("type {0} was not provided a value and has no default")]
NoDefaultForType(String),
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
Expand Down
13 changes: 13 additions & 0 deletions src/fields/field_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ impl FieldValue {
field_type: &FieldType,
value: String,
) -> Result<FieldValue, InvalidFieldError> {
if value.is_empty() {
// Defaults
let default_val = match field_type {
FieldType::String => Ok(FieldValue::String(value.clone())),
FieldType::Markdown => Ok(FieldValue::Markdown(value.clone())),
FieldType::Number => Ok(FieldValue::Number(0.0)),
FieldType::Boolean => Ok(FieldValue::Boolean(false)),
_ => Err(InvalidFieldError::NoDefaultForType(field_type.to_string())),
};
if default_val.is_ok() {
return default_val;
}
}
match field_type {
FieldType::String => Ok(FieldValue::String(value)),
FieldType::Markdown => Ok(FieldValue::Markdown(value)),
Expand Down
33 changes: 11 additions & 22 deletions src/fields/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::fields::FieldConfig;
use liquid::{ObjectView, ValueView};
use mime_guess::{get_mime_extensions, Mime, MimeGuess};
use mime_guess::MimeGuess;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::HashMap, fmt::Display, str::FromStr};
use std::{collections::HashMap, fmt::Display};
use tracing::warn;

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -69,7 +69,7 @@ impl File {
display_type: DisplayType,
) -> Self {
Self {
url: Self::_url(sha, mime),
url: Self::_url(sha),
sha: sha.to_string(),
name: name.map(|n| n.to_string()),
filename: filename.to_string(),
Expand All @@ -93,7 +93,7 @@ impl File {
match &k[..] {
"sha" => {
self.sha = v.as_str().unwrap().into();
self.url = Self::_url(&self.sha, &self.mime);
self.url = Self::_url(&self.sha);
}
"name" => self.name = Some(v.as_str().unwrap().into()),
"filename" => self.filename = v.as_str().unwrap().into(),
Expand All @@ -116,7 +116,7 @@ impl File {
pub fn image() -> Self {
let mime = "image/*";
Self {
url: Self::_url("", mime),
url: Self::_url(""),
sha: "".to_string(),
name: None,
filename: "".to_string(),
Expand All @@ -127,7 +127,7 @@ impl File {
pub fn video() -> Self {
let mime = "video/*";
Self {
url: Self::_url("", mime),
url: Self::_url(""),
sha: "".to_string(),
name: None,
filename: "".to_string(),
Expand All @@ -138,7 +138,7 @@ impl File {
pub fn audio() -> Self {
let mime = "audio/*";
Self {
url: Self::_url("", mime),
url: Self::_url(""),
sha: "".to_string(),
name: None,
filename: "".to_string(),
Expand All @@ -149,33 +149,23 @@ impl File {
pub fn download() -> Self {
let mime = "*/*";
Self {
url: Self::_url("", mime),
url: Self::_url(""),
sha: "".to_string(),
name: None,
filename: "".to_string(),
mime: mime.to_string(),
display_type: DisplayType::Download.to_string(),
}
}
fn _url(sha: &str, mime: &str) -> String {
fn _url(sha: &str) -> String {
if sha.is_empty() {
return "".to_string();
}
let config = FieldConfig::get();
let mut ext = Cow::from("");
if let Ok(m) = Mime::from_str(mime) {
if m.subtype() != mime_guess::mime::STAR {
if let Some(exts) = get_mime_extensions(&m) {
if !exts.is_empty() {
ext = Cow::from(format!(".{}", exts.first().unwrap()))
}
}
}
}
format!("{}/{}{}", config.uploads_url, sha, ext)
format!("{}/{}", config.uploads_url, sha)
}
pub fn url(&self) -> String {
Self::_url(&self.sha, &self.mime)
Self::_url(&self.sha)
}
pub fn to_map(&self, include_url: bool) -> HashMap<&str, &String> {
let mut m = HashMap::new();
Expand Down Expand Up @@ -213,7 +203,6 @@ pub mod tests {
file.sha = "fake-sha".to_string();
println!("{}", file.url());
assert!(!file.url().is_empty());
assert!(file.url().contains('.'));
}
}
#[test]
Expand Down

0 comments on commit 9ffa2d2

Please sign in to comment.