Skip to content

Commit

Permalink
Add timestamp to filenames of downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-hughes committed May 3, 2021
1 parent bdf923d commit 151fd48
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;

use chrono::{DateTime, Utc};
use sanitize_filename::{sanitize_with_options, Options};

use crate::threadpool::Threadpool;
Expand All @@ -25,6 +26,7 @@ pub struct EpData {
pub pod_id: i64,
pub title: String,
pub url: String,
pub pubdate: Option<DateTime<Utc>>,
pub file_path: Option<PathBuf>,
}

Expand Down Expand Up @@ -87,12 +89,16 @@ fn download_file(ep_data: EpData, dest: PathBuf, mut max_retries: usize) -> Down
_ => "mp3", // assume .mp3 unless we figure out otherwise
};

let file_name = sanitize_with_options(&ep_data.title, Options {
let mut file_name = sanitize_with_options(&ep_data.title, Options {
truncate: true,
windows: true, // for simplicity, we'll just use Windows-friendly paths for everyone
replacement: "",
});

if let Some(pubdate) = ep_data.pubdate {
file_name = format!("{}_{}", file_name, pubdate.format("%Y%m%d_%H%M%S"));
}

let mut file_path = dest;
file_path.push(format!("{}.{}", file_name, ext));

Expand Down
2 changes: 2 additions & 0 deletions src/main_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ impl MainController {
pod_id: ep.pod_id,
title: ep.title.clone(),
url: ep.url.clone(),
pubdate: ep.pubdate,
file_path: None,
},
ep.path.is_none(),
Expand All @@ -469,6 +470,7 @@ impl MainController {
pod_id: ep.pod_id,
title: ep.title.clone(),
url: ep.url.clone(),
pubdate: ep.pubdate,
file_path: None,
})
} else {
Expand Down

0 comments on commit 151fd48

Please sign in to comment.