-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
e53eac8
9c46056
570f0d6
6b06f4c
fb01df6
27d2bc9
db98b25
d4da69a
30a814a
bd73dcb
91614d6
7c2d610
683510a
4cbaf8b
d3a6884
6f9352f
c763cf5
e53bc11
27ef5c5
d30cb32
9aa59f3
adf2552
3f573d9
3820012
1494fbf
9ac871b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @akyn84 why not use Nette database ->startTransaction() ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
||
} |
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; | ||
} | ||
|
||
} |
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akyn84 why?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. :)