webmetro/src/commands/send.rs

76 lines
2.0 KiB
Rust
Raw Normal View History

2018-04-15 05:43:23 +00:00
use clap::{App, Arg, ArgMatches, SubCommand};
use futures::{
future,
prelude::*
};
use hyper::{
2018-09-18 06:15:02 +00:00
Client,
client::HttpConnector,
Request
2018-04-15 05:43:23 +00:00
};
use super::{
stdin_stream,
2018-09-18 06:15:02 +00:00
WebmPayload
2018-04-15 05:43:23 +00:00
};
use webmetro::{
chunk::{
Chunk,
WebmStream
},
error::WebmetroError,
fixers::ChunkStream,
stream_parser::StreamEbml
};
pub fn options() -> App<'static, 'static> {
SubCommand::with_name("send")
.about("PUTs WebM from stdin to a relay server.")
.arg(Arg::with_name("url")
.help("The location to upload to")
.required(true))
.arg(Arg::with_name("throttle")
.long("throttle")
.help("Slow down upload to \"real time\" speed as determined by the timestamps (useful for streaming static files)"))
}
2018-09-18 06:15:02 +00:00
type BoxedChunkStream = Box<Stream<Item = Chunk, Error = WebmetroError> + Send>;
2018-04-15 05:43:23 +00:00
pub fn run(args: &ArgMatches) -> Box<Future<Item=(), Error=WebmetroError> + Send> {
2018-04-15 05:43:23 +00:00
let mut chunk_stream: BoxedChunkStream = Box::new(
stdin_stream()
.parse_ebml()
.chunk_webm()
.fix_timecodes()
);
let url_str = match args.value_of("url") {
Some(url) => String::from(url),
_ => return Box::new(Err(WebmetroError::from_str("Listen address wasn't provided")).into_future())
};
if args.is_present("throttle") {
chunk_stream = Box::new(chunk_stream.throttle());
}
2018-09-18 06:15:02 +00:00
let request_payload = WebmPayload(chunk_stream.map_err(|err| {
2018-04-16 02:06:42 +00:00
eprintln!("{}", &err);
2018-09-18 06:15:02 +00:00
err
}));
2018-04-15 05:43:23 +00:00
Box::new(future::lazy(move || {
2018-09-18 06:15:02 +00:00
Request::put(url_str)
.body(request_payload)
.map_err(WebmetroError::from_err)
}).and_then(|request| {
let client = Client::builder().build(HttpConnector::new(1));
2018-04-15 05:43:23 +00:00
client.request(request)
.and_then(|response| {
2018-09-18 06:15:02 +00:00
response.into_body().for_each(|_chunk| {
Ok(())
})
})
2018-04-15 05:43:23 +00:00
.map_err(WebmetroError::from_err)
}))
}