diff --git a/.gitignore b/.gitignore index d094d36..ace654b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /nbproject/ /composer.lock -/Nette/ \ No newline at end of file +/Nette/ +.idea/ +vendor/ diff --git a/MultipleFileUpload/Extension.php b/MultipleFileUpload/Extension.php new file mode 100644 index 0000000..fc9cfbf --- /dev/null +++ b/MultipleFileUpload/Extension.php @@ -0,0 +1,52 @@ +getContainerBuilder(); + $this->compiler->parseServices($builder, $this->loadFromFile(__DIR__ . '/config.neon')); + } + + + public function beforeCompile() { + parent::beforeCompile(); + } + + + public function afterCompile(Nette\PhpGenerator\ClassType $class) { + $initialize = $class->methods['initialize']; + + // add ->addMultipleFileUpload shortcut into forms + $initialize->addBody( + 'Nette\Forms\Container::extensionMethod( + "\Nette\Forms\Container::addMultipleFileUpload", + function (\Nette\Forms\Container $_this, $name, $label = null, $maxFileSize = 25) { + return $_this[$name] = new MultipleFileUpload\MultipleFileUpload($label, $maxFileSize, $this->getByType(?), $this->getByType(?)); + } + );', + ['Nette\Http\IRequest', 'MultipleFileUpload\UI\Registrator'] + ); + + $initialize->addBody( + 'MultipleFileUpload\MultipleFileUpload::init( + $this->getByType(?), $this->getByType(?), $this->getByType(?) + );', + ['MultipleFileUpload\Model\IQueues', 'Nette\Http\IRequest', 'MultipleFileUpload\UI\Registrator'] + ); + + $initialize->addBody( + '$this->getService(?)->onStartup[] = [?, ?];', + ['application', 'MultipleFileUpload\MultipleFileUpload', 'handleUploads'] + ); + $initialize->addBody( + '$this->getService(?)->onShutdown[] = [?, ?];', + ['application', 'MultipleFileUpload\MultipleFileUpload', 'cleanCache'] + ); + } + +} diff --git a/MultipleFileUpload/Model/Dibi/Queue.php b/MultipleFileUpload/Model/Dibi/Queue.php index 1f7373d..232569b 100644 --- a/MultipleFileUpload/Model/Dibi/Queue.php +++ b/MultipleFileUpload/Model/Dibi/Queue.php @@ -13,7 +13,6 @@ use dibi, MultipleFileUpload\Model\BaseQueue, - Nette\Environment, Nette\Http\FileUpload, Nette\InvalidStateException; @@ -94,23 +93,19 @@ function updateFile($name, $chunk, FileUpload $file = null) */ function getUploadedFilesTemporaryPath() { - if (!Queues::$uploadsTempDir) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); + if (!$this->getQueuesModel()->uploadsTempDir) { + throw new InvalidStateException("Directory for temp files is not set."); } - if (!file_exists(Queues::$uploadsTempDir)) { - mkdir(Queues::$uploadsTempDir, 0777, true); + if (!file_exists($this->getQueuesModel()->uploadsTempDir)) { + mkdir($this->getQueuesModel()->uploadsTempDir, 0777, true); } - if (!is_writable(Queues::$uploadsTempDir)) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%"); - } - - if (!is_writable(Queues::$uploadsTempDir)) { + if (!is_writable($this->getQueuesModel()->uploadsTempDir)) { throw new InvalidStateException("Directory for temp files is not writable!"); } - return Queues::$uploadsTempDir; + return $this->getQueuesModel()->uploadsTempDir; } @@ -163,4 +158,4 @@ function getLastAccess() } -} \ No newline at end of file +} diff --git a/MultipleFileUpload/Model/Dibi/Queues.php b/MultipleFileUpload/Model/Dibi/Queues.php index 7d96c05..5fd3729 100644 --- a/MultipleFileUpload/Model/Dibi/Queues.php +++ b/MultipleFileUpload/Model/Dibi/Queues.php @@ -11,10 +11,9 @@ namespace MultipleFileUpload\Model\Dibi; -use dibi, +use DibiConnection, MultipleFileUpload\Model\BaseQueues, - MultipleFileUpload\Model\IQueue, - Nette\InvalidStateException; + MultipleFileUpload\Model\IQueue; /** * Multiple File Uploader driver for Dibi @@ -28,13 +27,13 @@ class Queues extends BaseQueues * Path to directory of uploaded files (temp) * @var string */ - public static $uploadsTempDir; + public $uploadsTempDir; /** * Connection - * @var \DibiConnection + * @var DibiConnection */ - public static $dibiConnection; + public $connection; /** @@ -45,38 +44,24 @@ function initialize() } - - // - - /** - * Gets dibi connection - * @return \DibiConnection - */ - function getConnection() + public function __construct(string $tempDir, DibiConnection $conection) { - if (!self::$dibiConnection) { - self::$dibiConnection = dibi::getConnection(); + $this->uploadsTempDir = $tempDir; + if(!file_exists($this->uploadsTempDir)) { + mkdir($this->uploadsTempDir, 0775, TRUE); } - return self::$dibiConnection; + $this->conection = $conection; } - /** - * Executes query - * @return \DibiResult - * @throws InvalidStateException + * @return DibiConnection */ - function query() + public function getConnection() { - $params = func_get_args(); - return call_user_func_array( - array($this->getConnection(), 'query'), $params - ); + return $this->database; } - - - // - + + /** * Gets queue (create if needed) * @param string $id @@ -95,7 +80,7 @@ function getQueue($id) */ function createQueueObj($queueID) { - $queue = new Queue(); + $queue = new Queue(); // todo: remove setQueuesModel --> move to __construct $queue->setQueuesModel($this); $queue->setQueueID($queueID); $queue->initialize(); @@ -108,13 +93,13 @@ function createQueueObj($queueID) */ function cleanup() { - $this->getConnection()->begin(); + $this->connection->begin(); foreach ($this->getQueues() AS $queue) { if ($queue->getLastAccess() < time() - $this->getLifeTime()) { $queue->delete(); } } - $this->getConnection()->commit(); + $this->connection->commit(); } @@ -136,4 +121,4 @@ function getQueues() } -} \ No newline at end of file +} diff --git a/MultipleFileUpload/Model/IQueue.php b/MultipleFileUpload/Model/IQueue.php index 25843df..eabd9f1 100644 --- a/MultipleFileUpload/Model/IQueue.php +++ b/MultipleFileUpload/Model/IQueue.php @@ -72,7 +72,7 @@ function addFileManually($name, $chunk, $chunks); * @param type $chunk * @param FileUpload $file */ - function updateFile($name, $chunk, FileUpload $file = null); + function updateFile($name, $chunk, FileUpload $file); /** * Gets all files in queue diff --git a/MultipleFileUpload/Model/NetteDatabase/Queue.php b/MultipleFileUpload/Model/NetteDatabase/Queue.php index ff6913b..60902a7 100644 --- a/MultipleFileUpload/Model/NetteDatabase/Queue.php +++ b/MultipleFileUpload/Model/NetteDatabase/Queue.php @@ -12,7 +12,6 @@ namespace MultipleFileUpload\Model\NetteDatabase; use MultipleFileUpload\Model\BaseQueue, - Nette\Environment, Nette\Http\FileUpload, Nette\InvalidStateException; @@ -68,8 +67,9 @@ function addFileManually($name, $chunk, $chunks) function updateFile($name, $chunk, FileUpload $file = null) { - $this->getConnection()->beginTransaction(); - $selection = $this->getConnection()->table($this->getFilesTable())->where('queueID', $this->getQueueID())->where('name', $name); + $connection = $this->getConnection(); + $connection->beginTransaction(); + $selection = $connection->table($this->getFilesTable())->where('queueID', $this->getQueueID())->where('name', $name); $data = array("chunk" => $chunk); if ($file){ @@ -79,7 +79,7 @@ function updateFile($name, $chunk, FileUpload $file = null) foreach ($selection as $row){ $row->update($data); } - $this->getConnection()->commit(); + $connection->commit(); } @@ -90,17 +90,13 @@ function updateFile($name, $chunk, FileUpload $file = null) function getUploadedFilesTemporaryPath() { if (!Queues::$uploadsTempDir) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); + throw new InvalidStateException("Directory for temp files is not set."); } if (!file_exists(Queues::$uploadsTempDir)) { mkdir(Queues::$uploadsTempDir, 0777, true); } - if (!is_writable(Queues::$uploadsTempDir)) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%"); - } - if (!is_writable(Queues::$uploadsTempDir)) { throw new InvalidStateException("Directory for temp files is not writable!"); } @@ -152,4 +148,4 @@ function getLastAccess() } -} \ No newline at end of file +} diff --git a/MultipleFileUpload/Model/NetteDatabase/Queues.php b/MultipleFileUpload/Model/NetteDatabase/Queues.php index 63335f9..99ca727 100644 --- a/MultipleFileUpload/Model/NetteDatabase/Queues.php +++ b/MultipleFileUpload/Model/NetteDatabase/Queues.php @@ -13,7 +13,7 @@ use MultipleFileUpload\Model\BaseQueues, MultipleFileUpload\Model\IQueue, - Nette\InvalidStateException; + Nette; /** * Multiple File Uploader driver for Nette\Database @@ -48,8 +48,12 @@ public function getFilesTable(){ return self::$filesTable; } - public function __construct(\Nette\Database\Context $database) + public function __construct($tempDir, Nette\Database\Context $database) { + self::$uploadsTempDir = $tempDir; + if(!file_exists(self::$uploadsTempDir)) { + mkdir(self::$uploadsTempDir, 0775, TRUE); + } $this->database = $database; } @@ -122,4 +126,4 @@ function getQueues() } -} \ No newline at end of file +} diff --git a/MultipleFileUpload/Model/SQLite/.gitignore b/MultipleFileUpload/Model/SQLite/.gitignore deleted file mode 100644 index 0177c42..0000000 --- a/MultipleFileUpload/Model/SQLite/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/database.sdb diff --git a/MultipleFileUpload/Model/SQLite/Queue.php b/MultipleFileUpload/Model/SQLite/Queue.php deleted file mode 100644 index 927517e..0000000 --- a/MultipleFileUpload/Model/SQLite/Queue.php +++ /dev/null @@ -1,136 +0,0 @@ -getQueuesModel()->query($sql); - } - - - /** - * Adds file to queue - * @param FileUpload $file - */ - function addFile(FileUpload $file) - { - $file->move($this->getUniqueFilePath()); - $this->query('INSERT INTO files (queueID, created, data, name) VALUES ("' . sqlite_escape_string($this->getQueueID()) . '",' . time() . ',\'' . sqlite_escape_string(serialize($file)) . '\', \'' . sqlite_escape_string($file->getName()) . '\')'); - } - - - // TODO: rename!!! - function addFileManually($name, $chunk, $chunks) - { - $this->query('INSERT INTO files (queueID, created, name, chunk, chunks) VALUES ("' . sqlite_escape_string($this->getQueueID()) . '",' . time() . ',\'' . sqlite_escape_string($name) . '\', \'' . sqlite_escape_string($chunk) . '\', \'' . sqlite_escape_string($chunks) . '\')'); - } - - - function updateFile($name, $chunk, FileUpload $file = null) - { - $this->query("BEGIN TRANSACTION"); - $where = 'queueID = \'' . sqlite_escape_string($this->getQueueID()) . '\' AND name = \'' . sqlite_escape_string($name) . '\''; - $this->query('UPDATE files SET chunk = \'' . sqlite_escape_string($chunk) . '\' WHERE ' . $where); - if ($file) { - $this->query('UPDATE files SET data = \'' . sqlite_escape_string(serialize($file)) . '\' WHERE ' . $where); - } - $this->query("END TRANSACTION"); - } - - - /** - * Gets upload directory path - * @return string - */ - function getUploadedFilesTemporaryPath() - { - if (!Queues::$uploadsTempDir) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); - } - - if (!file_exists(Queues::$uploadsTempDir)) { - mkdir(Queues::$uploadsTempDir, 0777, true); - } - - if (!is_writable(Queues::$uploadsTempDir)) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%"); - } - - if (!is_writable(Queues::$uploadsTempDir)) { - throw new InvalidStateException("Directory for temp files is not writable!"); - } - - return Queues::$uploadsTempDir; - } - - - /** - * Gets files - * @return FileUpload[] - */ - function getFiles() - { - $files = array(); - - foreach ($this->query("SELECT * FROM files WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "'")->fetchAll() AS $row) { - $f = unserialize($row["data"]); - if (!$f instanceof FileUpload) - continue; - $files[] = $f; - } - return $files; - } - - - function delete() - { - $dir = realpath($this->getUploadedFilesTemporaryPath()); - foreach ($this->getFiles() AS $file) { - $fileDir = dirname($file->getTemporaryFile()); - if (realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) { - // Delete file only if user leaved file in temp directory - @unlink($file->getTemporaryFile()); // intentionally @ - } - } - - $this->query("DELETE FROM files WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "'"); - } - - - /** - * When was queue last accessed? - * @return int timestamp - */ - function getLastAccess() - { - $lastAccess = (int) $this->query("SELECT created - FROM files - WHERE queueID = '" . sqlite_escape_string($this->getQueueID()) . "' - ORDER BY created DESC")->fetchSingle(); - return $lastAccess; - } - - -} \ No newline at end of file diff --git a/MultipleFileUpload/Model/SQLite/Queues.php b/MultipleFileUpload/Model/SQLite/Queues.php deleted file mode 100644 index d26bc25..0000000 --- a/MultipleFileUpload/Model/SQLite/Queues.php +++ /dev/null @@ -1,189 +0,0 @@ - - - function getConnection() - { - if (!$this->connection) { - $this->connection = $this->openDB(); - - // load database - if (filesize(self::$databasePath) == 0) { - //$this->beginTransaction(); - $this->connection->queryExec(file_get_contents(dirname(__FILE__) . "/setupDB.sql"), $error); - if ($error) { - throw new InvalidStateException("Can't create SQLite database: " . $error); - } - //$this->endTransaction(); - } - } - return $this->connection; - } - - - /** - * Executes query - * @param string $sql - * @return SQLiteResult - * @throws InvalidStateException - */ - function query($sql) - { - $r = $this->getConnection()->query($sql, SQLITE_ASSOC, $error); - if ($error) { - throw new InvalidStateException("Can't execute queury: '" . $sql . "'. error: " . $error); - } - return $r; - } - - - /* function beginTransaction() { - $this->query("BEGIN TRANSACTION"); - } - - function endTransaction() { - $this->query("END TRANSACTION"); - } */ - - /** - * Open SQLite file - * @return SQLiteDatabase - * @throws InvalidStateException - */ - function openDB() - { - - if (!($connection = new \SQLiteDatabase(self::$databasePath, 0777, $error))) { - throw new InvalidStateException("Can't create sqlite database: " . $error); - } - - return $connection; - } - - - // - - /** - * Gets queue (create if needed) - * @param string $id - * @return Queue - */ - function getQueue($id) - { - return $this->createQueueObj($id); - } - - - /** - * Queue factory. - * @param string $queueID - * @return Queue - */ - function createQueueObj($queueID) - { - $queue = new Queue(); - $queue->setQueuesModel($this); - $queue->setQueueID($queueID); - $queue->initialize(); - return $queue; - } - - - /** - * Executes cleanup - */ - function cleanup() - { - $this->query("BEGIN TRANSACTION"); - foreach ($this->getQueues() AS $queue) { - if ($queue->getLastAccess() < time() - $this->getLifeTime()) { - $queue->delete(); - } - } - $this->query("END TRANSACTION"); - - // physically delete files marked for deletion - $this->query("VACUUM"); - } - - - /** - * Gets all queues - * @return IQueue[] - */ - function getQueues() - { - $queuesOut = array(); - $qs = $this->query("SELECT queueID - FROM files - GROUP BY queueID")->fetchAll(); - - foreach ($qs AS $row) { - $queuesOut[] = $this->createQueueObj($row["queueID"]); - } - - return $queuesOut; - } - - - static function init() - { - // TODO: remove this magic - $config = Environment::getConfig("MultipleFileUploader", array( - "databasePath" => dirname(__FILE__) . "/database.sdb", - "uploadsTempDir" => "" - )); - - foreach ($config AS $key => $val) { - self::$$key = $val; - } - } - - -} -Queues::init(); diff --git a/MultipleFileUpload/Model/SQLite/setupDB.sql b/MultipleFileUpload/Model/SQLite/setupDB.sql deleted file mode 100644 index 56bab59..0000000 --- a/MultipleFileUpload/Model/SQLite/setupDB.sql +++ /dev/null @@ -1,17 +0,0 @@ ---Table: files - ---DROP TABLE files; - -CREATE TABLE files ( - id integer PRIMARY KEY, - queueID char(13) NOT NULL, - created integer NOT NULL, - data text, - chunk integer NOT NULL DEFAULT 1, - chunks integer NOT NULL DEFAULT 1, - name varchar(255) NOT NULL -); - -CREATE UNIQUE INDEX files_Name - ON files - (name); diff --git a/MultipleFileUpload/Model/SQLite3/Queue.php b/MultipleFileUpload/Model/SQLite3/Queue.php index 87b614e..c0ac36b 100644 --- a/MultipleFileUpload/Model/SQLite3/Queue.php +++ b/MultipleFileUpload/Model/SQLite3/Queue.php @@ -13,7 +13,6 @@ namespace MultipleFileUpload\Model\SQLite3; use MultipleFileUpload\Model\BaseQueue, - Nette\Environment, Nette\Http\FileUpload, Nette\InvalidStateException, SQLite3, @@ -104,23 +103,19 @@ function updateFile($name, $chunk, FileUpload $file = null) /** - * Get upload directory path + * Gets upload directory path * @return string */ function getUploadedFilesTemporaryPath() { if (!Queues::$uploadsTempDir) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%" . DIRECTORY_SEPARATOR . "uploads-MFU"); + throw new InvalidStateException("Directory for temp files is not set."); } if (!file_exists(Queues::$uploadsTempDir)) { mkdir(Queues::$uploadsTempDir, 0777, true); } - if (!is_writable(Queues::$uploadsTempDir)) { - Queues::$uploadsTempDir = Environment::expand("%tempDir%"); - } - if (!is_writable(Queues::$uploadsTempDir)) { throw new InvalidStateException("Directory for temp files is not writable!"); } diff --git a/MultipleFileUpload/Model/SQLite3/Queues.php b/MultipleFileUpload/Model/SQLite3/Queues.php index b6fd9fb..1611d38 100644 --- a/MultipleFileUpload/Model/SQLite3/Queues.php +++ b/MultipleFileUpload/Model/SQLite3/Queues.php @@ -14,7 +14,6 @@ use MultipleFileUpload\Model\BaseQueues, MultipleFileUpload\Model\IQueue, - Nette\Environment, Nette\InvalidStateException, SQLite3, SQLite3Result, @@ -23,7 +22,7 @@ class Queues extends BaseQueues { /** - * @var \SQLite3 + * @var SQLite3 */ private $connection; @@ -48,7 +47,22 @@ function initialize() } + // todo: add to Travis CI parallel lint https://github.com/JakubOnderka/PHP-Parallel-Lint + // todo: remove me :-) http://eyeleo.com/ + /** + * */ + public function __construct($tempDir, $databaseFilePath) + { + self::$uploadsTempDir = $tempDir; + if(!file_exists(self::$uploadsTempDir)) { + mkdir(self::$uploadsTempDir, 0775, TRUE); + } + self::$databasePath = $databaseFilePath; + $this->connection = new SQLite3($databaseFilePath); + } + + // function getConnection() @@ -187,15 +201,7 @@ function getQueues() static function init() { - // TODO: remove this magic - $config = Environment::getConfig('MultipleFileUploader', array( - 'databasePath' => dirname(__FILE__) . '/database.sqlite3', - 'uploadsTempDir' => '' - )); - - foreach ($config AS $key => $val) { - self::$$key = $val; - } + } diff --git a/MultipleFileUpload/MultipleFileUpload.php b/MultipleFileUpload/MultipleFileUpload.php index 720f06e..b38f1e3 100644 --- a/MultipleFileUpload/MultipleFileUpload.php +++ b/MultipleFileUpload/MultipleFileUpload.php @@ -1,540 +1,527 @@ -register("MultipleFileUpload\\UI\\HTML4SingleUpload"); - $uiReg->register("MultipleFileUpload\\UI\\Plupload"); - - // Set default check callback - self::$validateFileCallback = callback(__CLASS__, "validateFile"); - - // TODO: remove this magic - self::$baseWWWRoot = Environment::getHttpRequest()->url->baseUrl . "MultipleFileUpload/"; - } - - - /** - * Register MFU into Nette - */ - public static function register() - { - self::init(); - - $application = Environment::getApplication(); - $application->onStartup[] = callback(__CLASS__, "handleUploads"); - $application->onShutdown[] = callback(__CLASS__, "cleanCache"); - } - - - /* ########## HANDLING UPLOADS ########### */ - - /** - * Sets life time of files in queue (shortcut for self::getQueuesModel()->setLifeTime) - * @param int $lifeTime Time in seconds - */ - static function setLifeTime($lifeTime) - { - self::getQueuesModel() - ->setLifeTime((int) $lifeTime); - } - - - protected static function _doSetLifetime() - { - // Auto config of lifeTime - $maxInputTime = (int) ini_get("max_input_time"); - // default if no max input time defined (-1) - if ($maxInputTime < 0) { - $lifeTime = 3600; - } else { - $lifeTime = $maxInputTime + 5; - } - - self::setLifeTime($lifeTime); - } - - - /** - * Handles uploading files - */ - public static function handleUploads() - { - if (self::$handleUploadsCalled === true) { - return; - } else { - self::$handleUploadsCalled = true; - } - - $req = Environment::getHttpRequest(); - - // Workaround for: http://forum.nettephp.com/cs/3680-httprequest-getheaders-a-content-type - $contentType = $req->getHeader("content-type"); - if (!$contentType and isset($_SERVER["CONTENT_TYPE"])) { - $contentType = $_SERVER["CONTENT_TYPE"]; - } - - if ($req->getMethod() !== "POST") { - return; - } - - self::getQueuesModel()->initialize(); - - foreach (self::getUIRegistrator()->getInterfaces() AS $interface) { -// \Nette\Diagnostics\Debugger::log($interface->getReflection()->getName().": is this your upload? ".$interface->isThisYourUpload()); - if ($interface->isThisYourUpload()) { - $ret = $interface->handleUploads(); - if ($ret === true) - break; - } - } - } - - - /** - * Checks file if is ok and can be processed - * @param FileUpload $file - * @return bool - */ - public static function validateFile(FileUpload $file) - { - return $file->isOk(); - } - - - /** - * Cleans cache - */ - public static function cleanCache() - { - if (!Environment::isProduction() or rand(1, 100) < 5) { - self::getQueuesModel()->cleanup(); - } - } - - - /** - * @return IQueues - * @throws InvalidStateException - */ - public static function getQueuesModel() - { - if (!self::$queuesModel) { // if nothing is set, setup sqlite model, which should work on all systems with SQLite - self::setQueuesModel(new Model\SQLite3\Queues()); - } - - if (!self::$queuesModel instanceof IQueues) { - throw new InvalidStateException("Queues model is not instance of Model\IQueues!"); - } - return self::$queuesModel; - } - - - /** - * Sets new queues model - * @param IQueues $model - */ - public static function setQueuesModel(IQueues $model) { - self::$queuesModel = $model; - self::_doSetLifetime(); - } - - - /** - * @return Registrator - */ - public static function getUIRegistrator() - { - if (!self::$interfaceRegistrator instanceof Registrator) { - throw new InvalidStateException("Interface registrator is not instance of MultipleFileUpload\UI\Registrator!"); - } - return self::$interfaceRegistrator; - } - - - public static function getHead() - { - // TODO: Add MFUFallbackController? - - $out = ""; - foreach (self::getUIRegistrator()->getInterfaces() AS $interface) { - $out .= $interface->renderHeadSection(); - } - return $out; - } - - - /* * **************************************************************************** - * ************************* Form Control ************************************** - * ***************************************************************************** */ - /** - * Unique identifier - * @var string - */ - public $token; - - /** - * Maximum selected files in one input - * @var int - */ - public $maxFiles; - - /** - * Maximum file size of single uploaded file - * @var int - */ - public $maxFileSize; - - /** - * How many threads will be used to upload files - * @var int - */ - public $simUploadThreads; - - - /** - * Constructor - * @param string $label Label - */ - public function __construct($label = NULL, $maxSelectedFiles = 25) - { - parent::__construct($label); - - if (!self::$handleUploadsCalled) { - throw new InvalidStateException("MultipleFileUpload::handleUpload() has not been called. Call `MultipleFileUpload::register();` from your bootstrap before you call Applicaton::run();"); - }; - - $this->maxFiles = $maxSelectedFiles; - $this->control = Html::el("div"); // TODO: support for prototype - $this->maxFileSize = self::parseIniSize(ini_get('upload_max_filesize')); - $this->simUploadThreads = 5; - } - - - /** - * Generates control - * @return Html - */ - public function getControl() - { - $this->setOption('rendered', TRUE); - - // Create control - $control = Html::el('div class=MultipleFileUpload')->id($this->getHtmlId()); - - //
- $tokenField = Html::el('input type=hidden')->name($this->getHtmlName() . '[token]')->value($this->getToken()); - $control->add($tokenField); - //
- - $fallbacks = array(); - - $interfaces = self::getUIRegistrator()->getInterfaces(); - $num = count($interfaces); - $cnt = 1; - foreach ($interfaces AS $interface) { - $html = $interface->render($this); - // remove wrapping {else} diff --git a/MultipleFileUpload/UI/Registrator.php b/MultipleFileUpload/UI/Registrator.php index dafe9f9..25c3141 100644 --- a/MultipleFileUpload/UI/Registrator.php +++ b/MultipleFileUpload/UI/Registrator.php @@ -1,65 +1,82 @@ -interfaces[] = $interface; - } elseif (is_string($interface)) { - // User gives us only namespace - $this->interfaces[] = $interface . "\\Controller"; - } else { - throw new InvalidArgumentException("Not supported interface!"); - } - return $this; - } - - - public function clear() - { - $this->interfaces = array(); - return $this; - } - - - public function getInterfaces() - { - $interfaces = $this->interfaces; - foreach ($interfaces AS $key => $interface) { - if (is_string($interface)) { - $interface = $interfaces[$key] = new $interface; - } - if (!$interface instanceof IUserInterface) { - throw new InvalidArgumentException($interface->reflection->name . " is not compatible with MFU!"); - } - } - $this->interfaces = $interfaces; - return array_reverse($interfaces); - } - - +productionMode = $productionMode; + $this->interfaces[] = $htmlUpload; + $this->interfaces[] = $plupload; + } + + + public function getProductionMode() { + return $this->productionMode; + } + + + public function register($interface) + { + if (is_object($interface)) { + if (!$interface instanceof IUserInterface) { + throw new InvalidArgumentException("Interface must implement MultipleFileUpload\UI\IUserInterface!"); + } + $this->interfaces[] = $interface; + } elseif (is_string($interface)) { + // User gives us only namespace + $this->interfaces[] = $interface . "\\Controller"; + } else { + throw new InvalidArgumentException("Not supported interface!"); + } + return $this; + } + + + public function clear() + { + $this->interfaces = array(); + return $this; + } + + + public function getInterfaces(Request $request) + { + $interfaces = $this->interfaces; + foreach ($interfaces AS $key => $interface) { + if (is_string($interface)) { + $interface = $interfaces[$key] = new $interface($request); + } + if (!$interface instanceof IUserInterface) { + throw new InvalidArgumentException($interface->reflection->name . " is not compatible with MFU!"); + } + } + $this->interfaces = $interfaces; + return array_reverse($interfaces); + } + + } \ No newline at end of file diff --git a/MultipleFileUpload/UI/Swfupload/Controller.php b/MultipleFileUpload/UI/Swfupload/Controller.php index cb8bac2..f9b6908 100644 --- a/MultipleFileUpload/UI/Swfupload/Controller.php +++ b/MultipleFileUpload/UI/Swfupload/Controller.php @@ -1,113 +1,116 @@ -httpRequest->getPost('sender') === "MFU-Swfupload"; - } - - - /** - * Handles uploaded files - * forwards it to model - */ - public function handleUploads() - { - $token = $this->httpRequest->getPost('token'); - if (!$token) { - return; - } - - /* @var $file FileUpload */ - foreach ($this->httpRequest->getFiles() AS $file) { - self::processFile($token, $file); - } - - // Response to client - echo "1"; - - // End the script - exit; - } - - - /** - * Renders interface to
- */ - public function render(MultipleFileUpload $upload) - { - $template = $this->createTemplate(dirname(__FILE__) . "/html.latte"); - $template->swfuId = $upload->getHtmlId() . "-swfuBox"; - return $template->__toString(TRUE); - } - - - /** - * Renders JavaScript body of function. - */ - public function renderInitJavaScript(MultipleFileUpload $upload) - { - $tpl = $this->createTemplate(dirname(__FILE__) . "/initJS.latte"); - $tpl->sizeLimit = ini_get('upload_max_filesize') . 'B'; - $tpl->token = $upload->getToken(); - $tpl->maxFiles = $upload->maxFiles; - $tpl->backLink = (string) $upload->form->action; - $tpl->swfuId = $upload->getHtmlId() . "-swfuBox"; - $tpl->simUploadFiles = $upload->simUploadThreads; - return $tpl->__toString(TRUE); - } - - - /** - * Renders JavaScript body of function. - */ - public function renderDestructJavaScript(MultipleFileUpload $upload) - { - return $this->createTemplate(dirname(__FILE__) . "/destructJS.js")->__toString(TRUE); - } - - - /** - * Renders set-up tags to attribute - */ - public function renderHeadSection() - { - return $this->createTemplate(dirname(__FILE__) . "/head.latte")->__toString(TRUE); - } - - +httpRequest->getPost('sender') === "MFU-Swfupload"; + } + + + /** + * Handles uploaded files + * forwards it to model + */ + public function handleUploads() + { + $token = $this->httpRequest->getPost('token'); + if (!$token) { + return; + } + + /* @var $file FileUpload */ + foreach ($this->httpRequest->getFiles() AS $file) { + self::processFile($token, $file); + } + + // Response to client + echo "1"; + + // End the script + exit; + } + + + /** + * Renders interface to
+ */ + public function render(MultipleFileUpload $upload) + { + $template = $this->createTemplate(dirname(__FILE__) . "/html.latte"); + $template->swfuId = $upload->getHtmlId() . "-swfuBox"; + return $template->__toString(TRUE); + } + + + /** + * Renders JavaScript body of function. + */ + public function renderInitJavaScript(MultipleFileUpload $upload) + { + $template = $this->createTemplate(dirname(__FILE__) . "/initJS.latte"); + $template->sizeLimit = ini_get('upload_max_filesize') . 'B'; + $template->token = $upload->getToken(); + $template->maxFiles = $upload->maxFiles; + $template->backLink = (string) $upload->form->action; + $template->swfuId = $upload->getHtmlId() . "-swfuBox"; + $template->simUploadFiles = $upload->simUploadThreads; + $template->flash_url = $this->httpRequest->url->baseUrl . '/swf/swfupload.swf'; + $template->flash9_url = $this->httpRequest->url->baseUrl . '/swf/swfupload_fp9.swf'; + $template->button_image_url = $this->httpRequest->url->baseUrl . 'imgs/XPButtonUploadText_89x88.png'; + return $template->__toString(TRUE); + } + + + /** + * Renders JavaScript body of function. + */ + public function renderDestructJavaScript(MultipleFileUpload $upload) + { + return $this->createTemplate(dirname(__FILE__) . "/destructJS.js")->__toString(TRUE); + } + + + /** + * Renders set-up tags to attribute + */ + public function renderHeadSection() + { + return $this->createTemplate(dirname(__FILE__) . "/head.latte")->__toString(TRUE); + } + + } \ No newline at end of file diff --git a/MultipleFileUpload/UI/Swfupload/initJS.latte b/MultipleFileUpload/UI/Swfupload/initJS.latte index dd3d1f4..eb48263 100644 --- a/MultipleFileUpload/UI/Swfupload/initJS.latte +++ b/MultipleFileUpload/UI/Swfupload/initJS.latte @@ -1,34 +1,34 @@ - \ No newline at end of file diff --git a/MultipleFileUpload/UI/Uploadify/Controller.php b/MultipleFileUpload/UI/Uploadify/Controller.php index 95ca628..2d50fdf 100644 --- a/MultipleFileUpload/UI/Uploadify/Controller.php +++ b/MultipleFileUpload/UI/Uploadify/Controller.php @@ -68,7 +68,8 @@ public function handleUploads() */ public function render(MultipleFileUpload $upload) { - $template = $this->createTemplate(dirname(__FILE__) . "/html.latte"); + $template = parent::createTemplate(); + $template->setFile(__DIR__ . "/html.latte"); $template->uploadifyId = $upload->getHtmlId() . "-uploadifyBox"; return $template->__toString(TRUE); } @@ -79,14 +80,15 @@ public function render(MultipleFileUpload $upload) */ public function renderInitJavaScript(MultipleFileUpload $upload) { - $tpl = $this->createTemplate(dirname(__FILE__) . "/initJS.latte"); - $tpl->sizeLimit = $upload->maxFileSize; - $tpl->token = $upload->getToken(); - $tpl->maxFiles = $upload->maxFiles; - $tpl->backLink = (string) $upload->form->action; - $tpl->uploadifyId = $upload->getHtmlId() . "-uploadifyBox"; - $tpl->simUploadFiles = $upload->simUploadThreads; - return $tpl->__toString(TRUE); + $template = parent::createTemplate(); + $template->setFile(__DIR__ . "/initJS.latte"); + $template->sizeLimit = $upload->maxFileSize; + $template->token = $upload->getToken(); + $template->maxFiles = $upload->maxFiles; + $template->backLink = (string) $upload->form->action; + $template->uploadifyId = $upload->getHtmlId() . "-uploadifyBox"; + $template->simUploadFiles = $upload->simUploadThreads; + return $template->__toString(TRUE); } @@ -95,7 +97,8 @@ public function renderInitJavaScript(MultipleFileUpload $upload) */ public function renderDestructJavaScript(MultipleFileUpload $upload) { - return $this->createTemplate(dirname(__FILE__) . "/destructJS.js")->__toString(TRUE); + $template = parent::createTemplate(); + return $template->setFile(__DIR__ . "/destructJS.js")->__toString(TRUE); } @@ -104,7 +107,8 @@ public function renderDestructJavaScript(MultipleFileUpload $upload) */ public function renderHeadSection() { - return $this->createTemplate(dirname(__FILE__) . "/head.latte")->__toString(TRUE); + $template = parent::createTemplate(); + $template->setFile(__DIR__ . "/head.latte")->__toString(TRUE); } diff --git a/MultipleFileUpload/config.neon b/MultipleFileUpload/config.neon new file mode 100644 index 0000000..6a71320 --- /dev/null +++ b/MultipleFileUpload/config.neon @@ -0,0 +1,4 @@ +services: + - MultipleFileUpload\UI\Registrator(%productionMode%,) + - MultipleFileUpload\UI\HTML4SingleUpload\Controller + - MultipleFileUpload\UI\Plupload\Controller \ No newline at end of file diff --git a/composer.json b/composer.json index 93cc56a..955e424 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } }, "require": { - "php": ">= 5.3.0", - "nette/nette": ">= 2.0" + "php": ">= 5.6.0", + "nette/nette": "~2.4" } }