Downloader: Change cache_image function to return Result<T, DownlaoderError> instead of Option<T>.
This commit is contained in:
@@ -192,40 +192,39 @@ pub fn get_episode(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cache_image(pd: &PodcastCoverQuery) -> Option<String> {
|
||||
let url = pd.image_uri()?.to_owned();
|
||||
pub fn cache_image(pd: &PodcastCoverQuery) -> Result<String, DownloadError> {
|
||||
let url = pd.image_uri()
|
||||
.ok_or_else(|| DownloadError::NoImageLocation)?
|
||||
.to_owned();
|
||||
|
||||
if url == "" {
|
||||
return None;
|
||||
return Err(DownloadError::NoImageLocation);
|
||||
}
|
||||
|
||||
let cache_download_fold = format!("{}{}", HAMMOND_CACHE.to_str()?, pd.title().to_owned());
|
||||
let cache_path = HAMMOND_CACHE
|
||||
.to_str()
|
||||
.ok_or_else(|| DownloadError::InvalidCacheLocation)?;
|
||||
let cache_download_fold = format!("{}{}", cache_path, pd.title().to_owned());
|
||||
|
||||
// Weird glob magic.
|
||||
if let Ok(mut foo) = glob(&format!("{}/cover.*", cache_download_fold)) {
|
||||
// For some reason there is no .first() method so nth(0) is used
|
||||
let path = foo.nth(0).and_then(|x| x.ok());
|
||||
if let Some(p) = path {
|
||||
return Some(p.to_str()?.into());
|
||||
return Ok(p.to_str()
|
||||
.ok_or_else(|| DownloadError::InvalidCachedImageLocation)?
|
||||
.into());
|
||||
}
|
||||
};
|
||||
|
||||
// Create the folders if they don't exist.
|
||||
DirBuilder::new()
|
||||
.recursive(true)
|
||||
.create(&cache_download_fold)
|
||||
.ok()?;
|
||||
.create(&cache_download_fold)?;
|
||||
|
||||
match download_into(&cache_download_fold, "cover", &url, None) {
|
||||
Ok(path) => {
|
||||
info!("Cached img into: {}", &path);
|
||||
Some(path)
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Failed to get feed image.");
|
||||
error!("Error: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
let path = download_into(&cache_download_fold, "cover", &url, None)?;
|
||||
info!("Cached img into: {}", &path);
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -235,6 +234,8 @@ mod tests {
|
||||
use hammond_data::dbqueries;
|
||||
use hammond_data::pipeline;
|
||||
|
||||
use std::fs;
|
||||
|
||||
#[test]
|
||||
// This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit
|
||||
// to run it.
|
||||
@@ -257,6 +258,7 @@ mod tests {
|
||||
HAMMOND_CACHE.to_str().unwrap(),
|
||||
pd.title()
|
||||
);
|
||||
assert_eq!(img_path, Some(foo_));
|
||||
assert_eq!(img_path.unwrap(), foo_);
|
||||
fs::remove_file(foo_).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,16 @@ pub enum DownloadError {
|
||||
DataError(#[cause] DataError),
|
||||
#[fail(display = "Io error: {}", _0)]
|
||||
IoError(#[cause] io::Error),
|
||||
#[fail(display = "The Download was cancelled")]
|
||||
DownloadCancelled,
|
||||
#[fail(display = "Unexpected server response: {}", _0)]
|
||||
UnexpectedResponse(reqwest::StatusCode),
|
||||
#[fail(display = "The Download was cancelled.")]
|
||||
DownloadCancelled,
|
||||
#[fail(display = "Remote Image location not found.")]
|
||||
NoImageLocation,
|
||||
#[fail(display = "Failed to parse CacheLocation.")]
|
||||
InvalidCacheLocation,
|
||||
#[fail(display = "Failed to parse Cached Image Location.")]
|
||||
InvalidCachedImageLocation,
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for DownloadError {
|
||||
|
||||
Reference in New Issue
Block a user