Skip to content

Instantly share code, notes, and snippets.

@mmstick
Last active June 14, 2016 15:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmstick/1d249f12555dd33df38f37da744c249b to your computer and use it in GitHub Desktop.
Save mmstick/1d249f12555dd33df38f37da744c249b to your computer and use it in GitHub Desktop.
Obtains a list of all videos in a given directory path
/// Collects a list of all of the episodes in a given directory. Files that are not videos are ignored.
pub fn get_episodes(directory: &str) -> Result<Vec<PathBuf>, &str> {
fs::read_dir(directory).ok().map_or(Err("tv-renamer: unable to read file"), |files| {
let video_extensions = try!(mimetypes::get_video_extensions());
let mut episodes = Vec::new();
for entry in files {
let status = entry.ok().map_or(Some("tv-renamer: unable to get file entry"), |entry| {
entry.metadata().ok().map_or(Some("tv-renamer: unable to get metadata"), |metadata| {
if metadata.is_file() {
for extension in &video_extensions {
if extension.as_str() == entry.path().extension().unwrap().to_str().unwrap() {
episodes.push(entry.path());
}
}
}
None
})
});
if status.is_some() { return Err(status.unwrap()); }
}
episodes.sort_by(|a, b| a.to_string_lossy().to_lowercase().cmp(&b.to_string_lossy().to_lowercase()));
Ok(episodes)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment