Verified Commit 7e4c47fd authored by Jordan Petridis's avatar Jordan Petridis 🌱

matrix-api/util.rs: Convert media! to a function

parent d867041a
......@@ -14,6 +14,7 @@ use backend::types::Backend;
use util::json_q;
use util::dw_media;
use util::cache_path;
use util::media;
use types::Room;
use types::Protocol;
......@@ -117,7 +118,7 @@ pub fn room_search(bk: &Backend,
/* download the avatar */
if let Some(avatar) = r.avatar.clone() {
if let Ok(dest) = cache_path(&id) {
media!(&base.clone(), &avatar, Some(&dest)).unwrap_or_default();
media(&base.clone(), &avatar, Some(&dest)).unwrap_or_default();
}
}
rooms.push(r);
......
......@@ -11,6 +11,7 @@ use util::cache_dir_path;
use util::get_room_media_list;
use util::resolve_media_url;
use util::semaphore;
use util;
use types::Message;
......@@ -35,7 +36,7 @@ pub fn get_media_async(bk: &Backend, media: String, tx: Sender<String>) -> Resul
let baseu = bk.get_base_url()?;
semaphore(bk.limit_threads.clone(), move || {
match media!(&baseu, &media) {
match util::media(&baseu, &media, None) {
Ok(fname) => {
tx.send(fname).unwrap();
}
......@@ -78,7 +79,7 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
let tx = bk.tx.clone();
thread::spawn(move || {
match media!(&baseu, &media) {
match util::media(&baseu, &media, None) {
Ok(fname) => {
tx.send(BKResponse::Media(fname)).unwrap();
}
......
......@@ -17,6 +17,7 @@ use util::get_initial_room_messages;
use util::{build_url, client_url, media_url};
use util::put_media;
use util;
use util::media;
use util::cache_path;
use backend::types::Backend;
......@@ -71,7 +72,7 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
match r["url"].as_str() {
Some(u) => {
if let Ok(dest) = cache_path(&roomid) {
avatar = media!(&baseu, u, Some(&dest)).unwrap_or_default();
avatar = media(&baseu, u, Some(&dest)).unwrap_or_default();
} else {
avatar = String::from("");
}
......
......@@ -153,16 +153,6 @@ macro_rules! query {
};
}
#[macro_export]
macro_rules! media {
($base: expr, $url: expr, $dest: expr) => {
dw_media($base, $url, false, $dest, 0, 0)
};
($base: expr, $url: expr) => {
dw_media($base, $url, false, None, 0, 0)
};
}
#[macro_export]
macro_rules! thumb {
($base: expr, $url: expr) => {
......@@ -575,6 +565,10 @@ pub fn dw_media(base: &Url,
download_file(url.as_str(), fname, dest)
}
pub fn media(base: &Url, url: &str, dest: Option<&str>) -> Result<String, Error> {
dw_media(base, url, false, dest, 0, 0)
}
pub fn download_file(url: &str, fname: String, dest: Option<&str>) -> Result<String, Error> {
let pathname = fname.clone();
let p = Path::new(&pathname);
......@@ -697,7 +691,7 @@ pub fn get_room_avatar(base: &Url, tk: &str, userid: &str, roomid: &str) -> Resu
let mut fname = match members.count() {
1 => {
if let Ok(dest) = cache_path(&roomid) {
media!(&base, m1, Some(&dest)).unwrap_or_default()
media(&base, m1, Some(&dest)).unwrap_or_default()
} else {
String::new()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment