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

Added \Nette\Database Queue backend #32

Merged
merged 1 commit into from
Jul 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
155 changes: 155 additions & 0 deletions MultipleFileUpload/Model/NetteDatabase/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

/**
* This file is part of the MultipleFileUpload (https://github.com/jkuchar/MultipleFileUpload/)
*
* Copyright (c) 2013 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\NetteDatabase;

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

/**
* Multiple File Uploader driver for Nette\Database
*
* @author Zdeněk Jurka
* @license New BSD License
*/
class Queue extends BaseQueue
{

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


/**
* @return \Nette\Database\Context
*/
protected function getConnection(){
return $this->getQueuesModel()->getConnection();
}

protected function getFilesTable(){
return $this->getQueuesModel()->getFilesTable();
}

function addFileManually($name, $chunk, $chunks)
{
$data = array(
'queueID' => $this->getQueueID(),
'created' => time(),
'name' => $name,
'chunk' => $chunk,
'chunks' => $chunks
);
$this->getQueuesModel()->getConnection()->table($this->getFilesTable())->insert($data);
}


function updateFile($name, $chunk, FileUpload $file = null)
{
$this->getConnection()->beginTransaction();
$selection = $this->getConnection()->table($this->getFilesTable())->where('queueID', $this->getQueueID())->where('name', $name);

$data = array("chunk" => $chunk);
if ($file){
// workaround: http://forum.dibiphp.com/cs/1003-pgsql-a-znak-x00-oriznuti-zbytku-vstupu
$data["data"] = base64_encode(serialize($file));
}
foreach ($selection as $row){
$row->update($data);
}
$this->getConnection()->commit();
}


/**
* 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->getConnection()->table($this->getFilesTable())->where('queueID', $this->getQueueID()) as $row) {
$f = unserialize(base64_decode($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 only files left in temp directory [not moved by user]
@unlink($file->getTemporaryFile()); // intentionally @
}
}
$this->getConnection()->table($this->getFilesTable())->where('queueID', $this->getQueueID())->delete();
}


/**
* When was queue last accessed?
* @return int timestamp
*/
function getLastAccess()
{
return $this->getConnection()->table($this->getFilesTable())->select('created')->where('queueID', $this->getQueueID())->order('created DESC')->fetch()->created;
}


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

/**
* This file is part of the MultipleFileUpload (https://github.com/jkuchar/MultipleFileUpload/)
*
* Copyright (c) 2013 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\NetteDatabase;

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

/**
* Multiple File Uploader driver for Nette\Database
*
* @author Zdeněk Jurka
* @license New BSD License
*/
class Queues extends BaseQueues
{

public static $filesTable = 'files';

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

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

}

/** @var Nette\Database\Context */
private $database;


public function getFilesTable(){
return self::$filesTable;
}

public function __construct(\Nette\Database\Context $database)
{
$this->database = $database;
}

// <editor-fold defaultstate="collapsed" desc="Database functions">

/**
* @return \Nette\Database\Context
*/
public function getConnection()
{
return $this->database;
}


// </editor-fold>

/**
* 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->getConnection()->beginTransaction();
foreach ($this->getQueues() AS $queue) {
if ($queue->getLastAccess() < time() - $this->getLifeTime()) {
$queue->delete();
}
}
$this->getConnection()->commit();
}


/**
* Gets all queues
* @return IQueue[]
*/
function getQueues()
{
$queuesOut = array();

foreach ($this->getConnection()->table($this->getFilesTable())->select('queueID')->group('queueID') AS $row) {
$queuesOut[] = $this->createQueueObj($row["queueID"]);
}
return $queuesOut;
}


}
21 changes: 21 additions & 0 deletions MultipleFileUpload/Model/NetteDatabase/setup/mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
---------------------------- Hey! READ ME! -----------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--- Warning! This table MUST be InnoDB! Dibi driver requires transaction support!
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

CREATE TABLE files (
id int(11) NOT NULL AUTO_INCREMENT,
queueID varchar(100) NOT NULL,
created int(11) NOT NULL,
data text NOT NULL,
name text NOT NULL,
chunk int(11) NOT NULL,
chunks int(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
15 changes: 15 additions & 0 deletions MultipleFileUpload/Model/NetteDatabase/setup/postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--Table: public.files

--DROP TABLE public.files;

CREATE TABLE public.files (
id serial NOT NULL PRIMARY KEY,
"queueID" varchar(100) NOT NULL,
created integer NOT NULL,
data text,
chunk integer,
chunks integer NOT NULL DEFAULT 1,
"name" varchar(255) NOT NULL
) WITH (
OIDS = FALSE
);