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> { ...@@ -313,6 +313,25 @@ impl<'a> MessageBox<'a> {
let url = msg.url.clone().unwrap_or_default(); let url = msg.url.clone().unwrap_or_default();
let backend = self.op.backend.clone(); 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( let download_btn = gtk::Button::new_from_icon_name(
"document-save-symbolic", "document-save-symbolic",
gtk::IconSize::Button.into(), gtk::IconSize::Button.into(),
......
...@@ -7,6 +7,7 @@ use backend::types::Backend; ...@@ -7,6 +7,7 @@ use backend::types::Backend;
use util::dw_media; use util::dw_media;
use util::download_file; use util::download_file;
use util::cache_dir_path; use util::cache_dir_path;
use util::resolve_media_url;
pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> { pub fn get_thumb_async(bk: &Backend, media: String, tx: Sender<String>) -> Result<(), Error> {
let baseu = bk.get_base_url()?; let baseu = bk.get_base_url()?;
...@@ -60,6 +61,23 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> { ...@@ -60,6 +61,23 @@ pub fn get_media(bk: &Backend, media: String) -> Result<(), Error> {
Ok(()) 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> { pub fn get_file_async(url: String, tx: Sender<String>) -> Result<(), Error> {
let fname; let fname;
{ {
......
...@@ -320,6 +320,10 @@ impl Backend { ...@@ -320,6 +320,10 @@ impl Backend {
let r = media::get_media(self, media); let r = media::get_media(self, media);
bkerror!(r, tx, BKResponse::CommandError); 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)) => { Ok(BKCommand::GetFileAsync(url, ctx)) => {
let r = media::get_file_async(url, ctx); let r = media::get_file_async(url, ctx);
bkerror!(r, tx, BKResponse::CommandError); bkerror!(r, tx, BKResponse::CommandError);
......
...@@ -13,7 +13,7 @@ use types::Sticker; ...@@ -13,7 +13,7 @@ use types::Sticker;
use types::UserInfo; use types::UserInfo;
use cache::CacheMap; use cache::CacheMap;
use url::Url;
#[derive(Debug)] #[derive(Debug)]
pub enum BKCommand { pub enum BKCommand {
...@@ -47,6 +47,7 @@ pub enum BKCommand { ...@@ -47,6 +47,7 @@ pub enum BKCommand {
GetFileAsync(String, Sender<String>), GetFileAsync(String, Sender<String>),
GetAvatarAsync(Option<Member>, Sender<String>), GetAvatarAsync(Option<Member>, Sender<String>),
GetMedia(String), GetMedia(String),
GetMediaUrl(String, Sender<String>),
GetUserInfoAsync(String, Sender<(String, String)>), GetUserInfoAsync(String, Sender<(String, String)>),
SendMsg(Message), SendMsg(Message),
SetRoom(Room), SetRoom(Room),
...@@ -115,6 +116,7 @@ pub enum BKResponse { ...@@ -115,6 +116,7 @@ pub enum BKResponse {
RoomName(String, String), RoomName(String, String),
RoomTopic(String, String), RoomTopic(String, String),
Media(String), Media(String),
MediaUrl(Url),
AttachedFile(Message), AttachedFile(Message),
SearchEnd, SearchEnd,
NewRoom(Room, String), NewRoom(Room, String),
......
...@@ -5,6 +5,8 @@ extern crate serde_json; ...@@ -5,6 +5,8 @@ extern crate serde_json;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate url;
#[macro_use] #[macro_use]
pub mod util; pub mod util;
pub mod error; pub mod error;
......
...@@ -500,6 +500,33 @@ pub fn put_media(url: &str, file: Vec<u8>) -> Result<JsonValue, 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, pub fn dw_media(base: &Url,
url: &str, url: &str,
thumb: bool, 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