diff --git a/application/models/rows/File.php b/application/models/rows/File.php new file mode 100644 index 0000000000000000000000000000000000000000..a96ad019c65d37524fb4e9973be5a6f9933a9835 --- /dev/null +++ b/application/models/rows/File.php @@ -0,0 +1,55 @@ +<?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 diff --git a/application/models/rows/RequestFile.php b/application/models/rows/RequestFile.php index 05e499c9af3f8e82c6b793a9bc2866e9e9a6c420..7c1576dd647d66430cdfb5a4275d13df433aef86 100644 --- a/application/models/rows/RequestFile.php +++ b/application/models/rows/RequestFile.php @@ -1,18 +1,18 @@ <?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 diff --git a/application/models/tables/Files.php b/application/models/tables/Files.php index 3b92324214238f8cedc7b74b17188efd00f2435c..43a45f7269967f661aa16b42d8ae8b6c4de7e052 100644 --- a/application/models/tables/Files.php +++ b/application/models/tables/Files.php @@ -3,6 +3,7 @@ /** * * @primary file_id + * @rowClass File * */ class Files extends Nmc_Db_Table