Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciki committed Jul 10, 2014
1 parent e4a449d commit d925cb4
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 353 deletions.
38 changes: 25 additions & 13 deletions MultipleFileUpload/Model/BaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,95 @@
* the file license.txt that was distributed with this source code.
*/


namespace MultipleFileUpload\Model;

use Nette\InvalidStateException,
Nette\Object;


/**
* @property IMFUQueuesModel $queuesModel
* @property string $queueID
*/
abstract class BaseQueue extends Object implements IQueue {

abstract class BaseQueue extends Object implements IQueue
{
/**
* Queues model
* @var IQueues
*/
private $queuesModel;


/**
* Gets queues model
* @return IQueues
*/
function getQueuesModel() {
function getQueuesModel()
{
if (!$this->queuesModel)
throw new InvalidStateException("Queues model is not set!");
return $this->queuesModel;
}


/**
* Sets queues model
* @param IQueues $model
*/
function setQueuesModel(IQueues $model) {
function setQueuesModel(IQueues $model)
{
$this->queuesModel = $model;
return $this;
}


/**
* Queue ID (token)
* @var string
*/
private $queueID;


/**
* Gets queue ID
* @return string
*/
function getQueueID() {
function getQueueID()
{
return $this->queueID;
}


/**
* Sets queue ID
* @param string $queueID
*/
function setQueueID($queueID) {
function setQueueID($queueID)
{
$this->queueID = $queueID;
return $this;
}


/**
* Returns unique file name
* @return string
*/
protected function getUniqueFilePath() {
return $this->getUploadedFilesTemporaryPath() . DIRECTORY_SEPARATOR
protected function getUniqueFilePath()
{
return $this->getUploadedFilesTemporaryPath() . DIRECTORY_SEPARATOR
. "upload-" . $this->getQueueID() . "-" . uniqid() . ".tmp";
}


/**
* Initialization
*/
function initialize() {
if (!$this->queueID or !$this->queuesModel) {
function initialize()
{
if (!$this->queueID or ! $this->queuesModel) {
throw new InvalidStateException("queueID and queuesModel must be setup before call initialize()!");
}
}

}

}
25 changes: 16 additions & 9 deletions MultipleFileUpload/Model/BaseQueues.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,39 @@
* the file license.txt that was distributed with this source code.
*/


namespace MultipleFileUpload\Model;

use Nette\Object;


/**
* @property int $lifeTime Life time
* @property int $cleanInterval Clean interval
*/
abstract class BaseQueues extends Object implements IQueues {

abstract class BaseQueues extends Object implements IQueues
{
/**
* Life time
* @var int
*/
private $lifeTime;


/**
* Gets life time of file
* @return int
*/
function getLifeTime() {
function getLifeTime()
{
return $this->lifeTime;
}


/**
* Sets life time of file
* @param int $time
*/
function setLifeTime($time) {
function setLifeTime($time)
{
$this->lifeTime = $time;
return $this;
}
Expand All @@ -51,21 +53,26 @@ function setLifeTime($time) {
*/
private $cleanInterval;


/**
* Gets cleaning interval
* @return int
*/
function getCleanInterval() {
function getCleanInterval()
{
return $this->cleanInterval;
}


/**
* Sets cleaning interval
* @param int $interval
*/
function setCleanInterval($interval) {
function setCleanInterval($interval)
{
$this->cleanInterval = $interval;
return $this;
}

}

}
102 changes: 60 additions & 42 deletions MultipleFileUpload/Model/Dibi/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,120 +9,136 @@
* the file license.txt that was distributed with this source code.
*/


namespace MultipleFileUpload\Model\Dibi;

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

/**
* Multiple File Uploader driver for Dibi
*
* @author Martin Sadový (SodaE), Jan Kuchař (honzakuchar)
* @license New BSD License
*/
class Queue extends BaseQueue {
class Queue extends BaseQueue
{

/**
* Executes query
* @return DibiResult
*/
function query() {
$params = func_get_args(); // arguments
function query()
{
$params = func_get_args();
return call_user_func_array(
array($this->getQueuesModel(), 'query'), // what
array($this->getQueuesModel(), 'query'),
$params
);
}


/**
* Adds file to queue
* @param FileUpload $file
*/
function addFile(FileUpload $file) {
function addFile(FileUpload $file)
{
$file->move($this->getUniqueFilePath());
$data = array(
'queueID%s' => $this->getQueueID(),
'created%i' => time(),
'data%s' => base64_encode(serialize($file)), // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu
'name%s' => $file->getName()
'data%s' => base64_encode(serialize($file)), // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu
'name%s' => $file->getName()
);

$this->query('INSERT INTO [files]', $data);
}

function addFileManually($name, $chunk,$chunks) {

function addFileManually($name, $chunk, $chunks)
{

$data = array(
'queueID%s' => $this->getQueueID(),
'created%i' => time(),
'name%s' => $name,
'chunk%i' => $chunk,
'chunks%i' => $chunks
'name%s' => $name,
'chunk%i' => $chunk,
'chunks%i' => $chunks
);

$this->query('INSERT INTO [files]', $data);
}

function updateFile($name, $chunk, FileUpload $file = null) {

function updateFile($name, $chunk, FileUpload $file = null)
{
dibi::begin();
$where = array(
"queueID%s" => $this->getQueueID(),
"name%s" => $name
"queueID%s" => $this->getQueueID(),
"name%s" => $name
);
$this->query('UPDATE files SET ',array("chunk"=>$chunk),'WHERE %and',$where);
if($file) {
$this->query('UPDATE files SET ',array("data"=>base64_encode(serialize($file))),'WHERE %and',$where); // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu
$this->query('UPDATE files SET ', array("chunk" => $chunk), 'WHERE %and', $where);
if ($file) {
$this->query('UPDATE files SET ', array("data" => base64_encode(serialize($file))), 'WHERE %and', $where); // workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu
}
dibi::commit();
}


/**
* Gets upload directory path
* @return string
*/
function getUploadedFilesTemporaryPath() {
if(!Queues::$uploadsTempDir) {
Queues::$uploadsTempDir =\Nette\Environment::expand("%tempDir%".DIRECTORY_SEPARATOR."uploads-MFU");
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 (!file_exists(Queues::$uploadsTempDir)) {
mkdir(Queues::$uploadsTempDir, 0777, true);
}

if(!is_writable(Queues::$uploadsTempDir)) {
Queues::$uploadsTempDir =\Nette\Environment::expand("%tempDir%");
if (!is_writable(Queues::$uploadsTempDir)) {
Queues::$uploadsTempDir = Environment::expand("%tempDir%");
}

if(!is_writable(Queues::$uploadsTempDir)) {
if (!is_writable(Queues::$uploadsTempDir)) {
throw new InvalidStateException("Directory for temp files is not writable!");
}

return Queues::$uploadsTempDir;
}


/**
* Gets files
* @return array of FileUpload
* @return FileUpload[]
*/
function getFiles() {
function getFiles()
{
$files = array();

foreach($this->query('SELECT * FROM [files] WHERE [queueID] = %s', $this->getQueueID())->fetchAll() as $row) {
foreach ($this->query('SELECT * FROM [files] WHERE [queueID] = %s', $this->getQueueID())->fetchAll() as $row) {
$f = unserialize(base64_decode($row["data"]));
if(!$f instanceof FileUpload) continue;
if (!$f instanceof FileUpload)
continue;
$files[] = $f;
}

return $files;
}

function delete() {

function delete()
{
$dir = realpath($this->getUploadedFilesTemporaryPath());
foreach($this->getFiles() AS $file) {
foreach ($this->getFiles() AS $file) {
$fileDir = dirname($file->getTemporaryFile());
if(realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) {
if (realpath($fileDir) == $dir and file_exists($file->getTemporaryFile())) {
// Delete only files left in temp directory [not moved by user]
@unlink($file->getTemporaryFile()); // intentionally @
}
Expand All @@ -131,18 +147,20 @@ function delete() {
$this->query("DELETE FROM [files] WHERE [queueID] = %s", $this->getQueueID());
}


/**
* When was queue last accessed?
* @return int timestamp
*/
function getLastAccess() {
$lastAccess = (int)$this->query(
"SELECT [created]
function getLastAccess()
{
$lastAccess = (int) $this->query(
"SELECT [created]
FROM [files]
WHERE [queueID] = %s", $this->getQueueID(),
"ORDER BY [created] DESC"
WHERE [queueID] = %s", $this->getQueueID(), "ORDER BY [created] DESC"
)->fetchSingle();
return $lastAccess;
}

}

}
Loading

0 comments on commit d925cb4

Please sign in to comment.