2016-05-15 15:54:09 +00:00
|
|
|
// Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
// 2016 Luis de Bethencourt <luisbg@osg.samsung.com>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
// Boston, MA 02110-1301, USA.
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use std::io::Write;
|
2016-05-23 17:29:01 +00:00
|
|
|
use std::sync::Mutex;
|
2016-08-27 08:16:17 +00:00
|
|
|
use std::convert::From;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
use error::*;
|
2016-05-14 13:31:39 +00:00
|
|
|
use rssink::*;
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
macro_rules! error_msg(
|
|
|
|
// Format strings
|
|
|
|
($err:expr, ($($msg:tt)*), [$($dbg:tt)*]) => { {
|
|
|
|
ErrorMessage::new(&$err, Some(From::from(format!($($msg)*))),
|
|
|
|
From::from(Some(format!($($dbg)*))),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
($err:expr, ($($msg:tt)*)) => { {
|
|
|
|
ErrorMessage::new(&$err, Some(From::from(format!($($msg)*))),
|
|
|
|
None,
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
|
|
|
|
($err:expr, [$($dbg:tt)*]) => { {
|
|
|
|
ErrorMessage::new(&$err, None,
|
|
|
|
Some(From::from(format!($($dbg)*))),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Plain strings
|
|
|
|
($err:expr, ($msg:expr), [$dbg:expr]) => {
|
|
|
|
ErrorMessage::new(&$err, Some(From::from($msg)),
|
|
|
|
Some(From::from($dbg)),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
($err:expr, ($msg:expr)) => {
|
|
|
|
ErrorMessage::new(&$err, Some(From::from($msg)),
|
|
|
|
None,
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
($err:expr, [$dbg:expr]) => {
|
|
|
|
ErrorMessage::new(&$err, None,
|
|
|
|
Some(From::from($dbg)),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2016-08-22 19:25:58 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Settings {
|
|
|
|
location: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum StreamingState {
|
|
|
|
Stopped,
|
|
|
|
Started { file: File, position: u64 },
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FileSink {
|
2016-07-20 09:46:03 +00:00
|
|
|
controller: SinkController,
|
2016-08-22 19:25:58 +00:00
|
|
|
settings: Mutex<Settings>,
|
|
|
|
streaming_state: Mutex<StreamingState>,
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 15:26:17 +00:00
|
|
|
unsafe impl Sync for FileSink {}
|
|
|
|
unsafe impl Send for FileSink {}
|
|
|
|
|
2016-05-14 13:31:39 +00:00
|
|
|
impl FileSink {
|
2016-08-10 16:27:38 +00:00
|
|
|
pub fn new(controller: SinkController) -> FileSink {
|
2016-07-20 08:28:58 +00:00
|
|
|
FileSink {
|
2016-07-20 09:46:03 +00:00
|
|
|
controller: controller,
|
2016-08-22 19:25:58 +00:00
|
|
|
settings: Mutex::new(Settings { location: None }),
|
|
|
|
streaming_state: Mutex::new(StreamingState::Stopped),
|
2016-07-20 08:28:58 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 16:27:38 +00:00
|
|
|
pub fn new_boxed(controller: SinkController) -> Box<Sink> {
|
|
|
|
Box::new(FileSink::new(controller))
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for FileSink {
|
2016-08-24 21:08:06 +00:00
|
|
|
fn get_controller(&self) -> &SinkController {
|
|
|
|
&self.controller
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:47:56 +00:00
|
|
|
fn set_uri(&self, uri: Option<Url>) -> Result<(), UriError> {
|
2016-08-22 19:35:16 +00:00
|
|
|
let location = &mut self.settings.lock().unwrap().location;
|
2016-08-22 19:25:58 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
*location = None;
|
2016-05-24 20:24:05 +00:00
|
|
|
match uri {
|
2016-08-27 08:16:17 +00:00
|
|
|
None => Ok(()),
|
2016-05-24 20:24:05 +00:00
|
|
|
Some(ref uri) => {
|
2016-08-27 08:16:17 +00:00
|
|
|
*location = Some(try!(uri.to_file_path()
|
|
|
|
.or_else(|_| {
|
2016-08-24 21:47:56 +00:00
|
|
|
Err(UriError::new(UriErrorKind::UnsupportedProtocol,
|
|
|
|
Some(format!("Unsupported file URI '{}'", uri.as_str()))))
|
2016-08-27 08:16:17 +00:00
|
|
|
})));
|
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:24:05 +00:00
|
|
|
fn get_uri(&self) -> Option<Url> {
|
2016-08-22 19:35:16 +00:00
|
|
|
let location = &self.settings.lock().unwrap().location;
|
2016-08-22 19:25:58 +00:00
|
|
|
location.as_ref()
|
2016-05-15 14:43:13 +00:00
|
|
|
.map(|l| Url::from_file_path(l).ok())
|
|
|
|
.and_then(|i| i) // join()
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
fn start(&self) -> Result<(), ErrorMessage> {
|
2016-08-22 19:35:16 +00:00
|
|
|
let location = &self.settings.lock().unwrap().location;
|
2016-08-22 19:25:58 +00:00
|
|
|
let mut streaming_state = self.streaming_state.lock().unwrap();
|
|
|
|
|
|
|
|
if let StreamingState::Started { .. } = *streaming_state {
|
2016-08-27 08:16:17 +00:00
|
|
|
return Err(error_msg!(SinkError::Failure, ["Sink already started"]));
|
2016-08-22 19:25:58 +00:00
|
|
|
}
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
let location = &try!(location.as_ref().ok_or_else(|| {
|
|
|
|
error_msg!(SinkError::Failure, ["No URI provided"])
|
|
|
|
}));
|
|
|
|
|
|
|
|
let file = try!(File::create(location.as_path()).or_else(|err| {
|
|
|
|
Err(error_msg!(SinkError::OpenFailed,
|
|
|
|
["Could not open file for writing '{}': {}",
|
|
|
|
location.to_str().unwrap_or("Non-UTF8 path"),
|
|
|
|
err.to_string()]))
|
|
|
|
}));
|
|
|
|
|
|
|
|
*streaming_state = StreamingState::Started {
|
|
|
|
file: file,
|
|
|
|
position: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
fn stop(&self) -> Result<(), ErrorMessage> {
|
2016-08-22 19:25:58 +00:00
|
|
|
let mut streaming_state = self.streaming_state.lock().unwrap();
|
|
|
|
*streaming_state = StreamingState::Stopped;
|
2016-05-14 13:31:39 +00:00
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
fn render(&self, data: &[u8]) -> Result<(), FlowError> {
|
2016-08-22 19:25:58 +00:00
|
|
|
let mut streaming_state = self.streaming_state.lock().unwrap();
|
|
|
|
|
2016-08-27 08:16:17 +00:00
|
|
|
let (file, position) = match *streaming_state {
|
|
|
|
StreamingState::Started { ref mut file, ref mut position } => (file, position),
|
|
|
|
StreamingState::Stopped => {
|
|
|
|
return Err(FlowError::Error(error_msg!(SinkError::Failure, ["Not started yet"])));
|
2016-07-20 08:28:58 +00:00
|
|
|
}
|
2016-08-27 08:16:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try!(file.write_all(data).or_else(|err| {
|
|
|
|
Err(FlowError::Error(error_msg!(SinkError::WriteFailed, ["Failed to write: {}", err])))
|
|
|
|
}));
|
|
|
|
|
|
|
|
*position += data.len() as u64;
|
|
|
|
Ok(())
|
2016-05-14 13:31:39 +00:00
|
|
|
}
|
2016-05-13 15:07:26 +00:00
|
|
|
}
|