Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e53eac8
composer: bumped compatibility with nette ~2.4 (thanks @akyn84, @lubo)
jkuchar Sep 20, 2016
9c46056
got rid of Environment (introduced dependency on Container) (thanks @…
jkuchar Sep 20, 2016
570f0d6
latter: {? replaced with {php (thanks @akyn84, @lubo)
jkuchar Sep 20, 2016
6b06f4c
model: introduced second implementation of driver for MySQL over Nett…
jkuchar Sep 20, 2016
fb01df6
.gitignore: vendor, .idea
jkuchar Sep 20, 2016
27d2bc9
MySQL driver: removed
landrisek Sep 24, 2016
db98b25
NetteDatabaseModel: removed magic configuration from container (+ ref…
jkuchar Oct 4, 2016
d4da69a
NetteDatabase/Queues.php: added depencency on Container
jkuchar Oct 4, 2016
30a814a
MultipleFileUpload: added dependency on container
jkuchar Oct 4, 2016
bd73dcb
DI extension: introduced simple extension
landrisek Sep 25, 2016
91614d6
MultipleFileUpload: explicit register() function parameters
jkuchar Oct 4, 2016
7c2d610
Queues: ???
jkuchar Oct 4, 2016
683510a
UI\Registrator: requires HttpRequest insted of container
landrisek Sep 25, 2016
4cbaf8b
UI\AbstractInterface: requires HttpRequest instead of Container
jkuchar Oct 4, 2016
d3a6884
Queues: fixed edge case
Oct 3, 2016
6f9352f
notes
jkuchar Oct 4, 2016
c763cf5
MultipleFileUpload: formatting
jkuchar Oct 4, 2016
e53bc11
formatting
jkuchar Oct 4, 2016
27ef5c5
refactoring: removed wrongly used ternary operator
jkuchar Oct 4, 2016
d30cb32
DI model
landrisek Oct 16, 2016
9aa59f3
[Extension]
landrisek Oct 22, 2016
adf2552
[Nette\Environment][DI connection]
landrisek Nov 4, 2016
3f573d9
[Nette\Environment][DI connection][sqlite][swf]
landrisek Nov 5, 2016
3820012
[NetteDatabase php 5.6 cmopatibility]
Nov 14, 2016
1494fbf
todos and comments
jkuchar Jan 28, 2017
9ac871b
DibiDriver: removed static queues variable
landrisek Feb 4, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/nbproject/
/composer.lock
/Nette/
/Nette/
.idea/
vendor/
24 changes: 24 additions & 0 deletions MultipleFileUpload/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MultipleFileUpload;

use Nette;

class Extension extends Nette\DI\CompilerExtension {

public function loadConfiguration() {
}

public function beforeCompile() {
parent::beforeCompile();
}

public function afterCompile(Nette\PhpGenerator\ClassType $class) {
$initialize = $class->methods['initialize'];
$initialize->addBody('MultipleFileUpload\MultipleFileUpload::init($this->getByType(?), $this->getParameters());', ['Nette\Http\IRequest']);
$initialize->addBody('MultipleFileUpload\MultipleFileUpload::register($this->getService(?));', ['mfuStorage']);
$initialize->addBody('$this->getService(?)->onStartup[] = [?, ?];', ['application', 'MultipleFileUpload\MultipleFileUpload', 'handleUploads']);
$initialize->addBody('$this->getService(?)->onShutdown[] = [?, ?];', ['application', 'MultipleFileUpload\MultipleFileUpload', 'cleanCache']);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completely missing configuration of storage driver + UIs

}

}
2 changes: 1 addition & 1 deletion MultipleFileUpload/Model/IQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akyn84 why?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this question conversely: is there special reason to have optional parameter?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. :)


/**
* Gets all files in queue
Expand Down
16 changes: 6 additions & 10 deletions MultipleFileUpload/Model/NetteDatabase/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace MultipleFileUpload\Model\NetteDatabase;

use MultipleFileUpload\Model\BaseQueue,
Nette\Environment,
Nette\Http\FileUpload,
Nette\InvalidStateException;

Expand Down Expand Up @@ -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){
Expand All @@ -79,7 +79,7 @@ function updateFile($name, $chunk, FileUpload $file = null)
foreach ($selection as $row){
$row->update($data);
}
$this->getConnection()->commit();
$connection->commit();
}


Expand All @@ -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!");
}
Expand Down Expand Up @@ -152,4 +148,4 @@ function getLastAccess()
}


}
}
14 changes: 11 additions & 3 deletions MultipleFileUpload/Model/NetteDatabase/Queues.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use MultipleFileUpload\Model\BaseQueues,
MultipleFileUpload\Model\IQueue,
Nette\InvalidStateException;
Nette;

/**
* Multiple File Uploader driver for Nette\Database
Expand Down Expand Up @@ -48,9 +48,17 @@ public function getFilesTable(){
return self::$filesTable;
}

public function __construct(\Nette\Database\Context $database)
public function __construct($database, $tempDir)
{
$this->database = $database;
// TODO: wrong; there must be not dependency on container here
$connection = new Nette\Database\Connection($database['dsn'], $database['user'], $database['password']);
self::$uploadsTempDir = $tempDir . DIRECTORY_SEPARATOR . "uploads-MFU";
if(!file_exists(self::$uploadsTempDir)) {
mkdir(self::$uploadsTempDir, 0775, TRUE);
};
$cacheStorage = new Nette\Caching\Storages\FileStorage(self::$uploadsTempDir);
$structure = new Nette\Database\Structure($connection, $cacheStorage);
$this->database = new Nette\Database\Context($connection, $structure);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whole Database\Context should be injected. IT is responsibility of DI container to create connection.

}

// <editor-fold defaultstate="collapsed" desc="Database functions">
Expand Down
64 changes: 29 additions & 35 deletions MultipleFileUpload/MultipleFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use MultipleFileUpload\Model\IQueue,
MultipleFileUpload\Model\IQueues,
MultipleFileUpload\UI\Registrator,
Nette\Environment,
Latte,
Nette,
Nette\Bridges,
Nette\Forms,
Nette\Forms\Container,
Nette\Forms\Controls\UploadControl,
Nette\Http\FileUpload,
Nette\InvalidStateException,
Nette\NotSupportedException,
Nette\Utils\Callback,
Nette\Utils\Html,
Nette\Utils\Strings;

Expand Down Expand Up @@ -62,35 +63,35 @@ class MultipleFileUpload extends UploadControl
*/
public static $baseWWWRoot = null;

/** @var Nette\Http\IRequest */
private static $request;

/** @var bool */
private static $productionMode;

/**
* Initialize MFU
*/
public static function init()
public static function init(Nette\Http\IRequest $request, Array $parameters)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static methods from this class init(), register(), ... needs to be split into another class that will be created and registered in constructor. MFU will then take this object as a dependency.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when this functionality is removed to separate class and injected in constructor, dependencies are missing in MultipleFileUpload::handleUploads called on startup of application. If handleUploads is dynamic and called on Nette\Application\Application->startUp[], class itself is initialized before attached to component (too soon). can be logic of MultipleFileUpload::handleUploads solved outside of class with completly independent service?

{
// Init UI registrator
$uiReg = self::$interfaceRegistrator = new Registrator();
$uiReg->register("MultipleFileUpload\\UI\\HTML4SingleUpload");
$uiReg->register("MultipleFileUpload\\UI\\Plupload");

self::$request = $request;
self::$productionMode = $parameters['productionMode'];
// Set default check callback
self::$validateFileCallback = callback(__CLASS__, "validateFile");

// TODO: remove this magic
self::$baseWWWRoot = Environment::getHttpRequest()->url->baseUrl . "MultipleFileUpload/";
self::$validateFileCallback = [__CLASS__, "validateFile"];
self::$baseWWWRoot = self::$request->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");
public static function register(Model\IQueues $queuesModel)
{
self::$queuesModel = $queuesModel;
}


Expand Down Expand Up @@ -133,8 +134,7 @@ public static function handleUploads()
self::$handleUploadsCalled = true;
}

$req = Environment::getHttpRequest();

$req = self::$request;
// 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"])) {
Expand All @@ -147,7 +147,7 @@ public static function handleUploads()

self::getQueuesModel()->initialize();

foreach (self::getUIRegistrator()->getInterfaces() AS $interface) {
foreach (self::getUIRegistrator()->getInterfaces(self::$request) AS $interface) {
// \Nette\Diagnostics\Debugger::log($interface->getReflection()->getName().": is this your upload? ".$interface->isThisYourUpload());
if ($interface->isThisYourUpload()) {
$ret = $interface->handleUploads();
Expand All @@ -174,7 +174,7 @@ public static function validateFile(FileUpload $file)
*/
public static function cleanCache()
{
if (!Environment::isProduction() or rand(1, 100) < 5) {
if (FALSE == self::$productionMode or rand(1, 100) < 5) {
self::getQueuesModel()->cleanup();
}
}
Expand All @@ -186,12 +186,8 @@ public static function cleanCache()
*/
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!");
throw new InvalidStateException("Queues model was not set or is not instance of Model\IQueues!");
}
return self::$queuesModel;
}
Expand Down Expand Up @@ -258,15 +254,13 @@ public static function getHead()
*/
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();");
};
Expand All @@ -291,12 +285,11 @@ public function getControl()

// <section token field>
$tokenField = Html::el('input type=hidden')->name($this->getHtmlName() . '[token]')->value($this->getToken());
$control->add($tokenField);
$control->addHtml($tokenField);
// </section token field>

$fallbacks = array();

$interfaces = self::getUIRegistrator()->getInterfaces();
$interfaces = self::getUIRegistrator()->getInterfaces(self::$request);
$num = count($interfaces);
$cnt = 1;
foreach ($interfaces AS $interface) {
Expand Down Expand Up @@ -324,14 +317,15 @@ public function getControl()
}
$cnt++;

$control->add($container);
$control->addHtml($container);
}

$template = new UI\Template();
$template->setFile(dirname(__FILE__) . DIRECTORY_SEPARATOR . "RegisterJS.latte");
$template->id = $this->getHtmlId();
$template->fallbacks = $fallbacks;
$control->add($template->__toString(TRUE));
$latte = new Latte\Engine();
$template = new Bridges\ApplicationLatte\Template($latte);
$template->setFile(__DIR__ . DIRECTORY_SEPARATOR . "RegisterJS.latte");
$template->id = $this->getHtmlId();
$template->fallbacks = $fallbacks;
$control->addHtml($template->__toString(TRUE));
/*
// <section without JavaScript>
$withoutJS = Html::el("div class=withoutJS");
Expand Down
23 changes: 10 additions & 13 deletions MultipleFileUpload/UI/AbstractInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace MultipleFileUpload\UI;

use MultipleFileUpload\MultipleFileUpload,
Nette\Environment,
Nette,
Latte,
Nette\Bridges,
Nette\Http\FileUpload,
Nette\Http\Request,
Nette\Object,
Nette\Utils\Callback;
Nette\Object;

/**
* Abstract UI Controller
Expand All @@ -27,9 +28,9 @@ abstract class AbstractInterface extends Object implements IUserInterface
protected $httpRequest;


public function __construct()
public function __construct(Nette\Http\Request $httpRequest)
{
$this->httpRequest = Environment::getHttpRequest();
$this->httpRequest = $httpRequest;
}


Expand Down Expand Up @@ -72,18 +73,14 @@ function processFile($token, $file)
}


/**
* @return Template
*/
protected function createTemplate($file = null)
protected function createTemplate()
{
$template = new Template($file);

$latte = new Latte\Engine();
$template = new Bridges\ApplicationLatte\Template($latte);
$template->baseUrl = $this->httpRequest->url->baseUrl;
$template->basePath = rtrim($template->baseUrl, '/');
$template->interface = $this;

$template->registerHelperLoader('Nette\Templating\Helpers::loader');
$template->addFilter(NULL, 'Latte\Macros\CoreMacros::install');

return $template;
}
Expand Down
6 changes: 4 additions & 2 deletions MultipleFileUpload/UI/HTML4SingleUpload/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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->maxFiles = $upload->maxFiles;
$template->mfu = $upload;
return $template->__toString(TRUE);
Expand All @@ -101,7 +102,8 @@ public function render(MultipleFileUpload $upload)
*/
public function renderInitJavaScript(MultipleFileUpload $upload)
{
return $this->createTemplate(dirname(__FILE__) . "/initJS.latte")->__toString(TRUE);
$template = parent::createTemplate();
return $template->setFile(__DIR__ . "/initJS.latte")->__toString(TRUE);
}


Expand Down
2 changes: 1 addition & 1 deletion MultipleFileUpload/UI/HTML4SingleUpload/html.latte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @link http://nettephp.com/cs/extras/multiplefileupload
*}

{?
{php
$input = \Nette\Utils\Html::el("input type=file")->name($mfu->getHtmlName().'[files][]');
}

Expand Down
Loading