Merge branch 'master' into develop

This commit is contained in:
2011-12-13 01:16:56 +00:00
8 changed files with 169 additions and 16 deletions

View File

@@ -13,12 +13,28 @@ class DownloadDispatcher_Processor {
$config = $main->config();
$log = $main->log();
// Find the list of available plugins
// Find the list of available Sync plugins
$sync_plugins = $config->get('sync');
foreach ($sync_plugins as $plugin_name) {
// Get a list of all the instances of this plugin to be used
$instances = $config->get("sync.{$plugin_name}");
foreach ($instances as $instance) {
try {
$plugin = DownloadDispatcher_Sync_PluginFactory::create($plugin_name, $config, $log, $instance);
$plugin->run();
} catch(SihnonFramework_Exception_LogException $e) {
SihnonFramework_LogEntry::warning($log, $e->getMessage());
}
}
}
// Find the list of available source plugins
DownloadDispatcher_Source_PluginFactory::scan();
$plugins = DownloadDispatcher_Source_PluginFactory::getValidPlugins();
$source_plugins = DownloadDispatcher_Source_PluginFactory::getValidPlugins();
$enabled_plugins = $config->get('sources');
foreach ($plugins as $plugin_name) {
foreach ($source_plugins as $plugin_name) {
if (in_array($plugin_name, $enabled_plugins)) {
$plugin = DownloadDispatcher_Source_PluginFactory::create($plugin_name, $config, $log);
$plugin->run();

View File

@@ -1,6 +1,6 @@
<?php
class DownloadDispatcher_Source_Plugin_RouterboardFirmware extends DownloadDispatcher_PluginBase implements DownloadDispatcher_Source_IPlugin {
class DownloadDispatcher_Source_Plugin_RouterboardFirmware extends DownloadDispatcher_Source_PluginBase implements DownloadDispatcher_Source_IPlugin {
/**
* Name of this plugin

View File

@@ -1,6 +1,6 @@
<?php
class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_PluginBase implements DownloadDispatcher_Source_IPlugin {
class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_Source_PluginBase implements DownloadDispatcher_Source_IPlugin {
/**
* Name of this plugin
@@ -28,20 +28,27 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_PluginBase
$source_dirs = $this->config->get('sources.TV.input-directories');
foreach ($source_dirs as $dir) {
if (is_dir($dir) && is_readable($dir)) {
$this->process_dir($dir);
$iterator = new DownloadDispatcher_Utility_MediaFilesIterator(new DownloadDispatcher_Utility_VisibleFilesIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir))));
foreach ($iterator as /** @var SplFileInfo */ $file) {
$this->process_matched_file($file->getPath(), $file->getFilename());
}
} else {
DownloadDispatcher_LogEntry::warning($this->log, "TV input directory '{$dir}' does not exist or cannot be read.");
}
}
}
protected function process_dir($dir) {
// TODO - Iterate over the contents of the directory, process files and recurse deeper
}
protected function process_matched_file($dir, $file) {
// TODO - Handle movement of the matched file to the correct output directory
// Handle direct media files, and also RAR archives
DownloadDispatcher_LogEntry::debug($this->log, "Media file: {$file}");
// Check to see if this file has been handled previously
if ($this->check_processed($dir . '/' . $file)) {
DownloadDispatcher_LogEntry::debug($this->log, "Skipping previously seen file");
return;
}
}
protected function identify_output_dir($dir, $file) {
@@ -58,11 +65,6 @@ class DownloadDispatcher_Source_Plugin_TV extends DownloadDispatcher_PluginBase
// TODO - use tvrenamer to update the filenames
}
protected function mark_processed($dir, $file) {
// TODO - Update the cache to show that a file has already been handled
// TODO - Upstream caching
}
}
?>

View File

@@ -0,0 +1,32 @@
<?php
class DownloadDispatcher_Source_PluginBase extends DownloadDispatcher_PluginBase {
static protected $source_cache = array();
protected function init_cache() {
if ( ! array_key_exists(get_called_class(), static::$source_cache)) {
// TODO - attempt to load data from persistent storage
static::$source_cache[get_called_class()] = array();
}
}
protected function mark_processed($file) {
$this->init_cache();
if ( ! in_array($file, static::$source_cache[get_called_class()])) {
static::$source_cache[get_called_class()][] = $file;
}
// TODO - flush cache to persistent storage
}
protected function check_processed($file) {
$this->init_cache();
return in_array($file, static::$source_cache[get_called_class()]);
}
}
?>

View File

@@ -0,0 +1,16 @@
<?php
interface DownloadDispatcher_Sync_IPlugin extends DownloadDispatcher_IPlugin {
/**
* Process files recognised by this plugin
*
*
*/
public function run();
public static function create($config, $log, $instance);
}
?>

View File

@@ -0,0 +1,48 @@
<?php
class DownloadDispatcher_Sync_Plugin_Rsync extends DownloadDispatcher_PluginBase implements DownloadDispatcher_Sync_IPlugin {
/**
* Name of this plugin
*
* @var string
*/
const PLUGIN_NAME = 'Rsync';
protected $config;
protected $log;
protected $instance;
protected $options;
protected $source;
protected $destination;
public static function create($config, $log, $instance) {
return new self($config, $log, $instance);
}
protected function __construct($config, $log, $instance) {
$this->config = $config;
$this->log = $log;
$this->instance = $instance;
$this->options = $this->config->get("sync.Rsync.{$this->instance}.options");
$this->source = $this->config->get("sync.Rsync.{$this->instance}.source");
$this->destination = $this->config->get("sync.Rsync.{$this->instance}.destination");
}
public function run() {
DownloadDispatcher_LogEntry::debug($this->log, "Running Rsync synchroniser: '{$this->instance}'");
$command = "/usr/bin/rsync {$this->options} '{$this->source}' '{$this->destination}'";
DownloadDispatcher_LogEntry::debug($this->log, "Running foreground task: {$command}");
DownloadDispatcher_ForegroundTask::execute($command, null, null, null, array($this, 'output'), null, $this->instance);
}
public function output($identifier, $data) {
DownloadDispatcher_LogEntry::debug($this->log, "{$identifier}: {$data}");
}
}
?>

View File

@@ -0,0 +1,30 @@
<?php
class DownloadDispatcher_Sync_PluginFactory extends DownloadDispatcher_PluginFactory {
protected static $plugin_prefix = 'DownloadDispatcher_Sync_Plugin_';
protected static $plugin_interface = 'DownloadDispatcher_Sync_IPlugin';
protected static $plugin_dir = array(
DownloadDispatcher_Lib => 'DownloadDispatcher/Sync/Plugin/',
);
public static function init() {
}
public static function create($plugin, SihnonFramework_Config $config, SihnonFramework_Log $log, $instance) {
self::ensureScanned();
if (! self::isValidPlugin($plugin)) {
throw new Sihnon_Exception_InvalidPluginName($plugin);
}
$classname = self::classname($plugin);
return call_user_func(array($classname, 'create'), $config, $log, $instance);
}
}
?>

View File

@@ -0,0 +1,9 @@
<?php
class DownloadDispatcher_Utility_MediaFilesIterator extends FilterIterator {
public function accept() {
return preg_match('/(?<!\.sample)\.(?:avi|ogm|m4v|mkv|mov|mp4|mpg|srt|rar)$/i', $this->current()->getFilename());
}
}
?>