Commit c4f69d6f authored by Jordan Petridis's avatar Jordan Petridis

backend: Add command to retrieve a media url.

parent 5a9ffa1b
......@@ -313,6 +313,25 @@ impl<'a> MessageBox<'a> {
let url = msg.url.clone().unwrap_or_default();
let backend = self.op.backend.clone();
let (tx, rx): (Sender<String>, Receiver<String>) = channel();
backend.send(BKCommand::GetMediaUrl(url.clone(), tx)).unwrap();
gtk::timeout_add(50, clone!(player => move || {
match rx.try_recv() {
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => {
let msg = i18n("Could not retrieve file's uri");
APPOP!(show_error, (msg));
gtk::Continue(true)
},
Ok(uri) => {
println!("AUDIO URI: {}", &uri);
player.initialize_stream(&uri);
gtk::Continue(false)
}
}
}));
let download_btn = gtk::Button::new_from_icon_name(
"document-save-symbolic",
gtk::IconSize::Button.into(),
......
......@@ -7,6 +7,7 @@ use backend::types::Backend;
use util::dw_media;
use util::download_file;
use util::cache_dir_path;
use util::resolve_media_url;
pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
......@@ -60,6 +61,23 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
Ok(())
}
pub fn get_media_url(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?;
semaphore!(bk.limit_threads, {
match resolve_media_url(&baseu, &media, false, 0, 0) {
Ok(uri) => {
tx.send(uri.to_string()).unwrap();
}
Err(_) => {
tx.send(String::from("")).unwrap();
}
};
});
Ok(())
}
pub fn get_file_async(url: String, tx: Sender<String>) -> Result<(), Error> {
let fname;
{
......
......@@ -320,6 +320,10 @@ impl Backend {
let r = media::get_media(self, media);
bkerror!(r, tx, BKResponse::CommandError);
}
Ok(BKCommand::GetMediaUrl(media, ctx)) => {
let r = media::get_media_url(self, media.to_string(), ctx);
bkerror!(r, tx, BKResponse::CommandError);
}
Ok(BKCommand::GetFileAsync(url, ctx)) => {
let r = media::get_file_async(url, ctx);
bkerror!(r, tx, BKResponse::CommandError);
......
......@@ -13,7 +13,7 @@ use types::Sticker;
use types::UserInfo;
use cache::CacheMap;
use url::Url;
#[derive(Debug)]
pub enum BKCommand {
......@@ -47,6 +47,7 @@ pub enum BKCommand {
GetFileAsync(String, Sender<String>),
GetAvatarAsync(Option<Member>, Sender<String>),
GetMedia(String),
GetMediaUrl(String, Sender<String>),
GetUserInfoAsync(String, Sender<(String, String)>),
SendMsg(Message),
SetRoom(Room),
......@@ -115,6 +116,7 @@ pub enum BKResponse {
RoomName(String, String),
RoomTopic(String, String),
Media(String),
MediaUrl(Url),
AttachedFile(Message),
SearchEnd,
NewRoom(Room, String),
......
......@@ -5,6 +5,8 @@ extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate url;
#[macro_use]
pub mod util;
pub mod error;
......
......@@ -500,6 +500,33 @@ pub fn put_media(url: &str, file: Vec<u8>) -> Result<JsonValue, Error> {
}
}
pub fn resolve_media_url(
base: &Url,
url: &str,
thumb: bool,
w: i32,
h: i32,
) -> Result<Url, Error> {
let re = Regex::new(r"mxc://(?P<server>[^/]+)/(?P<media>.+)")?;
let caps = re.captures(url).ok_or(Error::BackendError)?;
let server = String::from(&caps["server"]);
let media = String::from(&caps["media"]);
let mut params: Vec<(&str, String)> = vec![];
let path: String;
if thumb {
params.push(("width", format!("{}", w)));
params.push(("height", format!("{}", h)));
params.push(("method", strn!("scale")));
path = format!("thumbnail/{}/{}", server, media);
} else {
path = format!("download/{}/{}", server, media);
}
media_url!(base, &path, params)
}
pub fn dw_media(base: &Url,
url: &str,
thumb: bool,
......
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