From 31250e44a440a8e9162188fd526c9044d24e5299 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Mon, 11 Dec 2006 18:47:36 +0000
Subject: [PATCH] Adds support for storing files too large for the database.

---
 application/models/rows/File.php        | 55 +++++++++++++++++++++++++
 application/models/rows/RequestFile.php | 14 +++----
 application/models/tables/Files.php     |  1 +
 3 files changed, 63 insertions(+), 7 deletions(-)
 create mode 100644 application/models/rows/File.php

diff --git a/application/models/rows/File.php b/application/models/rows/File.php
new file mode 100644
index 00000000..a96ad019
--- /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 05e499c9..7c1576dd 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 3b923242..43a45f72 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
-- 
GitLab