Skip to content
Snippets Groups Projects
Commit 31250e44 authored by Tim Steiner's avatar Tim Steiner
Browse files

Adds support for storing files too large for the database.

parent 0ea3509c
No related branches found
No related tags found
No related merge requests found
<?php
/**
*
* @tableClass Files
*
*/
class File extends Nmc_Db_Table_Row
{
public function __construct($config = array())
{
parent::__construct($config);
if (strlen($this->data) == 0
&& file_exists($this->_getPath() . DIRECTORY_SEPARATOR . $this->hash))
{
$this->data = file_get_contents($this->_getPath() . DIRECTORY_SEPARATOR . $this->hash);
}
}
public function _save()
{
$oldHash = $this->hash;
$this->hash = hash('whirlpool', $this->data);
if(strlen($this->data) > ($this->_getMaxPacketSize() / 2)
/* && $oldHash != $this->hash*/)
{
@mkdir($this->_getPath(), 0755, true);
file_put_contents($this->_getPath() . DIRECTORY_SEPARATOR . $this->hash, $this->data);
$this->data = '';
}
parent::_save();
}
protected function _getMaxPacketSize()
{
return 1024 * 1024;
$db = Files::getInstance()->getAdapter();
$max_packet = $db->query('SELECT @@max_allowed_packet')->fetchAll();
$max_packet = $max_packet[0]['@@max_allowed_packet'];
return $max_packet;
}
protected function _getPath()
{
$path = APPLICATION_PATH
. DIRECTORY_SEPARATOR
. 'uploads'
. DIRECTORY_SEPARATOR
. substr($this->hash, 0, 1)
. DIRECTORY_SEPARATOR
. substr($this->hash, 1, 2);
return $path;
}
}
\ No newline at end of file
<?php
class RequestFile extends Nmc_Db_Table_Row
/**
*
* @tableClass RequestFiles
* @foreignKey file
*
*/
class RequestFile extends File
{
const SYLLABUS_TYPE = 'syllabus';
const CROSSLIST_MEMO_TYPE = 'crosslist memo';
const IS_NARRATIVE_TYPE = 'is_narrative';
const OTHER_TYPE = 'other';
public function __construct($config = array())
{
parent::__construct($config);
$this->_registerRelation(new Nmc_Db_Table_Relation_Extend(Files::getInstance(), $this, 'file'));
}
}
?>
\ No newline at end of file
......@@ -3,6 +3,7 @@
/**
*
* @primary file_id
* @rowClass File
*
*/
class Files extends Nmc_Db_Table
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment