Downloader: Change cache_image function to return Result<T, DownlaoderError> instead of Option<T>.
This commit is contained in:
parent
d3f279374a
commit
c6e426cbac
@ -192,40 +192,39 @@ pub fn get_episode(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cache_image(pd: &PodcastCoverQuery) -> Option<String> {
|
pub fn cache_image(pd: &PodcastCoverQuery) -> Result<String, DownloadError> {
|
||||||
let url = pd.image_uri()?.to_owned();
|
let url = pd.image_uri()
|
||||||
|
.ok_or_else(|| DownloadError::NoImageLocation)?
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
if url == "" {
|
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.
|
// Weird glob magic.
|
||||||
if let Ok(mut foo) = glob(&format!("{}/cover.*", cache_download_fold)) {
|
if let Ok(mut foo) = glob(&format!("{}/cover.*", cache_download_fold)) {
|
||||||
// For some reason there is no .first() method so nth(0) is used
|
// For some reason there is no .first() method so nth(0) is used
|
||||||
let path = foo.nth(0).and_then(|x| x.ok());
|
let path = foo.nth(0).and_then(|x| x.ok());
|
||||||
if let Some(p) = path {
|
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.
|
// Create the folders if they don't exist.
|
||||||
DirBuilder::new()
|
DirBuilder::new()
|
||||||
.recursive(true)
|
.recursive(true)
|
||||||
.create(&cache_download_fold)
|
.create(&cache_download_fold)?;
|
||||||
.ok()?;
|
|
||||||
|
|
||||||
match download_into(&cache_download_fold, "cover", &url, None) {
|
let path = download_into(&cache_download_fold, "cover", &url, None)?;
|
||||||
Ok(path) => {
|
info!("Cached img into: {}", &path);
|
||||||
info!("Cached img into: {}", &path);
|
Ok(path)
|
||||||
Some(path)
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
error!("Failed to get feed image.");
|
|
||||||
error!("Error: {}", err);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -235,6 +234,8 @@ mod tests {
|
|||||||
use hammond_data::dbqueries;
|
use hammond_data::dbqueries;
|
||||||
use hammond_data::pipeline;
|
use hammond_data::pipeline;
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
// This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit
|
// This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit
|
||||||
// to run it.
|
// to run it.
|
||||||
@ -257,6 +258,7 @@ mod tests {
|
|||||||
HAMMOND_CACHE.to_str().unwrap(),
|
HAMMOND_CACHE.to_str().unwrap(),
|
||||||
pd.title()
|
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),
|
DataError(#[cause] DataError),
|
||||||
#[fail(display = "Io error: {}", _0)]
|
#[fail(display = "Io error: {}", _0)]
|
||||||
IoError(#[cause] io::Error),
|
IoError(#[cause] io::Error),
|
||||||
#[fail(display = "The Download was cancelled")]
|
|
||||||
DownloadCancelled,
|
|
||||||
#[fail(display = "Unexpected server response: {}", _0)]
|
#[fail(display = "Unexpected server response: {}", _0)]
|
||||||
UnexpectedResponse(reqwest::StatusCode),
|
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 {
|
impl From<reqwest::Error> for DownloadError {
|
||||||
|
|||||||
@ -122,9 +122,9 @@ impl App {
|
|||||||
match receiver.recv_timeout(Duration::from_millis(10)) {
|
match receiver.recv_timeout(Duration::from_millis(10)) {
|
||||||
Ok(Action::UpdateSources(source)) => {
|
Ok(Action::UpdateSources(source)) => {
|
||||||
if let Some(s) = source {
|
if let Some(s) = source {
|
||||||
utils::refresh_feed(Some(vec![s]), sender.clone())
|
utils::refresh_feed(Some(vec![s]), sender.clone());
|
||||||
} else {
|
} else {
|
||||||
utils::refresh_feed(None, sender.clone())
|
utils::refresh_feed(None, sender.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Action::RefreshAllViews) => content.update(),
|
Ok(Action::RefreshAllViews) => content.update(),
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#![cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
#![cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
|
||||||
|
|
||||||
|
use failure::Error;
|
||||||
use gdk_pixbuf::Pixbuf;
|
use gdk_pixbuf::Pixbuf;
|
||||||
use send_cell::SendCell;
|
use send_cell::SendCell;
|
||||||
|
|
||||||
@ -82,7 +83,7 @@ pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let img_path = downloader::cache_image(pd)?;
|
let img_path = downloader::cache_image(pd).ok()?;
|
||||||
let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok();
|
let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok();
|
||||||
if let Some(px) = px {
|
if let Some(px) = px {
|
||||||
let mut hashmap = CACHED_PIXBUFS.write().unwrap();
|
let mut hashmap = CACHED_PIXBUFS.write().unwrap();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user