Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nette 2.4 compatibility #42

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
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
Owner Author

Choose a reason for hiding this comment

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

@akyn84 why?

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
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
162 changes: 162 additions & 0 deletions MultipleFileUpload/Model/MySQL/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php
Copy link
Owner Author

Choose a reason for hiding this comment

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

@akyn84 what was wrong with original nette\database + MySQL implementation? https://github.com/jkuchar/MultipleFileUpload/tree/master/MultipleFileUpload/Model/NetteDatabase

Choose a reason for hiding this comment

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

Nette\Database implementation was not in last release (1.1.0) donwloaded by composer thus I did not notice it is in master branch by Zdeněk Jurka. Probably it solve most of issues with nette 2.4? (Nette\Environment, etc.)

Choose a reason for hiding this comment

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

I check just right now and for sure it is not compatible with PHP 7 so I will continue in update.

Choose a reason for hiding this comment

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

Nette\Database implementation was not in last release use Nette\Environments, I am reverting to my orignal solution -> when it will be approved, i will refactor all Models but firs i need to confirm MySQL namespace (later it can be removed)

Copy link
Owner Author

Choose a reason for hiding this comment

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

namespace should be names as the target for the bridge. You you name namespace mysql, I would expect to that this driver will use mysql_* functions


/**
* This file is part of the MultipleFileUpload (https://github.com/jkuchar/MultipleFileUpload/)
*
* Copyright (c) 2014 Ciki (https://github.com/Ciki)
* Copyright (c) 2014 Jan Kuchař (http://www.jankuchar.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace MultipleFileUpload\Model\MySQL;

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

class Queue extends BaseQueue
{

/**
* Execute query
* @param string $sql
* @return SQLite3Result
*/
function query($sql)
{
return $this->getQueuesModel()->query($sql);
}

/**
* Add file to queue
* @param FileUpload $file
*/
function addFile(FileUpload $file)
{
$file->move($this->getUniqueFilePath());
$this->getQueuesModel()->getConnection()->query("
INSERT INTO files (queueID, created, data, name) VALUES (
?,?,?,?)", $this->getQueueID(), time(), "X'" . bin2hex(serialize($file)) . "'", $file->getName());
}


// TODO: rename!!!
/**
* Add file to queue manually
* @param FileUpload $file
* @param int $chunk
* @param int $chunks
*/
function addFileManually($name, $chunk, $chunks)
{
$this->getQueuesModel()->getConnection()->query("
INSERT INTO files (queueID, created, name, chunk, chunks) VALUES (?, ?, ?, ?, ?)
", $this->getQueueID(), time(), $name, $chunk, $chunks);
}


/**
* Update file
* @param string $name
* @param int $chunk
* @param FileUpload $file
*/
function updateFile($name, $chunk, FileUpload $file)
{
$this->query("START TRANSACTION");
Copy link
Owner Author

Choose a reason for hiding this comment

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

@akyn84 why not use Nette database ->startTransaction() ?

Choose a reason for hiding this comment

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

sure

$this->getQueuesModel()->getConnection()->query("UPDATE files SET chunk = ? WHERE queueID = ? AND name = ?", $chunk, $this->getQueueID(), $name);
if ($file) {
// use blob for storing serialized object, see https://bugs.php.net/bug.php?id=63419 and https://bugs.php.net/bug.php?id=62361
//$blob = fopen($file->getTemporaryFile(), 'rb');
$blob = serialize($file);
$pdo = $this->getQueuesModel()->getConnection()->getPdo();
$stmt = $pdo->prepare("UPDATE files SET data = :data WHERE queueID = :queueID AND name = :name;");
$stmt->bindParam(':data', $blob, \PDO::PARAM_LOB);
$queueID = $this->getQueueID();
$stmt->bindParam(':queueID', $queueID);
$stmt->bindParam(':name', $name);
$stmt->execute();
}
$this->query("COMMIT WORK");
}
Copy link
Owner Author

Choose a reason for hiding this comment

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

@akyn84 mixing of spaces + tabs



/**
* Get upload directory path
* @return string
*/
function getUploadedFilesTemporaryPath()
{
if (!Queues::$uploadsTempDir) {
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)) {
throw new InvalidStateException("Directory for temp files is not writable!");
}

return Queues::$uploadsTempDir;
}


/**
* Get files
* @return FileUpload[]
*/
function getFiles()
{
$files = [];
foreach($this->getQueuesModel()->getConnection()->query("SELECT * FROM files WHERE queueID = ?", $this->getQueueID())->fetchPairs('id', 'data') as $row) {
if(($file = unserialize($row)) instanceof FileUpload) {
$files[] = $file;
}
};
return $files;
}


/**
* Delete temporary 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->getQueuesModel()->getConnection()->query("DELETE FROM files WHERE queueID = ?", $this->getQueueID());
}


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


}
137 changes: 137 additions & 0 deletions MultipleFileUpload/Model/MySQL/Queues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* This file is part of the MultipleFileUpload (https://github.com/jkuchar/MultipleFileUpload/)
*
* Copyright (c) 2014 Ciki (https://github.com/Ciki)
* Copyright (c) 2014 Jan Kuchař (http://www.jankuchar.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace MultipleFileUpload\Model\MySQL;

use MultipleFileUpload\Model\BaseQueues;
use MultipleFileUpload\Model\IQueue;
use Nette\DI\Container;
use Nette\InvalidStateException;

class Queues extends BaseQueues
{
/**
* @var Nette\Database\Connection
*/
private $connection;

/**
* Path to directory of uploaded files (temp)
* @var string
*/
public static $uploadsTempDir;

/** @var Container */
private static $container;

public function __construct(Container $container, \Nette\Database\Connection $connection)
{
$parameters = $container->getParameters();
self::$container = $container;
self::$uploadsTempDir = $parameters['tempDir'] . DIRECTORY_SEPARATOR . "uploads-MFU";
$this->connection = $connection;
}


/**
* Initialize driver
*/
function initialize()
{

}


function getConnection()
{
return $this->connection;
}


/**
* Execute query
* @param string $sql
* @return Nette\Database\ResultSet
* @throws InvalidStateException
*/
function query($sql)
{
$res = $this->getConnection()->query($sql);
if (!$res) {
throw new InvalidStateException("Can't execute query: '" . $sql . "'. error: " . $this->getConnection()->lastErrorMsg());
}
return $res;
}

/**
* Get queue (create if needed)
* @param string $id
* @return Queue
*/
function getQueue($id)
{
return $this->createQueueObj($id);
}


/**
* Factory for Queue
* @param string $queueID
* @return Queue
*/
function createQueueObj($queueID)
{
$parameters = self::$container->getParameters();
$queue = new Queue($parameters['tempDir']);
$queue->setQueuesModel($this);
$queue->setQueueID($queueID);
$queue->initialize();
return $queue;
}


/**
* Execute cleanup
*/
function cleanup()
{
$this->query('START TRANSACTION');
foreach ($this->getQueues() AS $queue) {
if ($queue->getLastAccess() < time() - $this->getLifeTime()) {
//$queue->delete();
}
}
$this->query('COMMIT WORK');
}


/**
* Get all queues
* @return IQueue[]
*/
function getQueues()
{
$queuesOut = array();
$res = $this->query('
SELECT queueID
FROM files
GROUP BY queueID
');

foreach($res->fetchPairs('queueID') as $row) {
$queuesOut[] = $this->createQueueObj($row['queueID']);
}

return $queuesOut;
}

}
17 changes: 17 additions & 0 deletions MultipleFileUpload/Model/MySQL/setupDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--Table: files

--DROP TABLE files;

CREATE TABLE files (
id integer AUTO_INCREMENT PRIMARY KEY,
queueID char(255) NOT NULL,
created integer NOT NULL,
data blob,
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);
Loading