init
This commit is contained in:
+24
-5
@@ -12,7 +12,12 @@ pub struct PendingMediaFile {
|
||||
}
|
||||
|
||||
impl PendingMediaFile {
|
||||
pub async fn begin(root: &Path, directory: &str, id: Uuid, extension: Option<&str>) -> std::io::Result<Self> {
|
||||
pub async fn begin(
|
||||
root: &Path,
|
||||
directory: &str,
|
||||
id: Uuid,
|
||||
extension: Option<&str>,
|
||||
) -> std::io::Result<Self> {
|
||||
let relative_directory = PathBuf::from(directory);
|
||||
let directory_path = root.join(&relative_directory);
|
||||
fs::create_dir_all(&directory_path).await?;
|
||||
@@ -31,7 +36,10 @@ impl PendingMediaFile {
|
||||
file,
|
||||
temporary_path,
|
||||
final_path,
|
||||
relative_final_path: relative_directory.join(final_name).to_string_lossy().into_owned(),
|
||||
relative_final_path: relative_directory
|
||||
.join(final_name)
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -59,8 +67,16 @@ pub fn extension_from_filename(filename: &str) -> Option<String> {
|
||||
.iter()
|
||||
.find(|candidate| lower.ends_with(&format!(".{candidate}")))
|
||||
.map(|candidate| (*candidate).to_string())
|
||||
.or_else(|| Path::new(name).extension().and_then(|value| value.to_str()).map(str::to_ascii_lowercase))?;
|
||||
if extension.chars().all(|character| character.is_ascii_alphanumeric() || character == '.') {
|
||||
.or_else(|| {
|
||||
Path::new(name)
|
||||
.extension()
|
||||
.and_then(|value| value.to_str())
|
||||
.map(str::to_ascii_lowercase)
|
||||
})?;
|
||||
if extension
|
||||
.chars()
|
||||
.all(|character| character.is_ascii_alphanumeric() || character == '.')
|
||||
{
|
||||
Some(extension)
|
||||
} else {
|
||||
None
|
||||
@@ -117,7 +133,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn preserves_compound_extensions() {
|
||||
assert_eq!(extension_from_filename("archive.tar.gz").as_deref(), Some("tar.gz"));
|
||||
assert_eq!(
|
||||
extension_from_filename("archive.tar.gz").as_deref(),
|
||||
Some("tar.gz")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user