]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/newnotice.php
Notice class has methods to check content length
[quix0rs-gnu-social.git] / actions / newnotice.php
index 4a2c369f0f873de20b425ef30c3b3be2589a07fc..e254eac4999852389b8623b36db62d10826f5bb3 100644 (file)
@@ -135,7 +135,7 @@ class NewnoticeAction extends Action
 
     function isRespectsQuota($user) {
         $file = new File;
-        $ret = $file->isRespectsQuota($user);
+        $ret = $file->isRespectsQuota($user,$_FILES['attach']['size']);
         if (true === $ret) return true;
         $this->clientError($ret);
     }
@@ -229,13 +229,25 @@ class NewnoticeAction extends Action
             if (empty($filename)) {
                 $this->clientError(_('Couldn\'t save file.'));
             }
-            $fileurl = File::url($filename);
+
+            $fileRecord = $this->storeFile($filename, $mimetype);
+
+            $fileurl = common_local_url('attachment',
+                array('attachment' => $fileRecord->id));
+
+            // not sure this is necessary -- Zach
+            $this->maybeAddRedir($fileRecord->id, $fileurl);
+
             $short_fileurl = common_shorten_url($fileurl);
             $content_shortened .= ' ' . $short_fileurl;
+
             if (mb_strlen($content_shortened) > 140) {
                 $this->deleteFile($filename);
                 $this->clientError(_('Max notice size is 140 chars, including attachment URL.'));
             }
+
+            // Also, not sure this is necessary -- Zach
+            $this->maybeAddRedir($fileRecord->id, $short_fileurl);
         }
 
         $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
@@ -249,7 +261,7 @@ class NewnoticeAction extends Action
         }
 
         if (isset($mimetype)) {
-            $this->attachFile($notice, $filename, $mimetype, $short_fileurl);
+            $this->attachFile($notice, $fileRecord);
         }
 
         common_broadcast_notice($notice);
@@ -304,12 +316,12 @@ class NewnoticeAction extends Action
         @unlink($filepath);
     }
 
-    function attachFile($notice, $filename, $mimetype, $short)
-    {
+    function storeFile($filename, $mimetype) {
+
         $file = new File;
         $file->filename = $filename;
 
-        $file->url = common_local_url('file', array('notice' => $notice->id));
+        $file->url = File::url($filename);
 
         $filepath = File::path($filename);
 
@@ -324,28 +336,40 @@ class NewnoticeAction extends Action
             $this->clientError(_('There was a database error while saving your file. Please try again.'));
         }
 
-        $file_redir = new File_redirection;
-        $file_redir->url = File::url($filename);
-        $file_redir->file_id = $file_id;
+        return $file;
+    }
 
-        $result = $file_redir->insert();
+    function rememberFile($file, $short)
+    {
+        $this->maybeAddRedir($file->id, $short);
+    }
 
-        if (!$result) {
-            common_log_db_error($file_redir, "INSERT", __FILE__);
-            $this->clientError(_('There was a database error while saving your file. Please try again.'));
-        }
+    function maybeAddRedir($file_id, $url)
+    {
+        $file_redir = File_redirection::staticGet('url', $url);
 
-        $f2p = new File_to_post;
-        $f2p->file_id = $file_id;
-        $f2p->post_id = $notice->id;
-        $f2p->insert();
+        if (empty($file_redir)) {
+            $file_redir = new File_redirection;
+            $file_redir->url = $url;
+            $file_redir->file_id = $file_id;
 
-        if (!$result) {
-            common_log_db_error($f2p, "INSERT", __FILE__);
-            $this->clientError(_('There was a database error while saving your file. Please try again.'));
+            $result = $file_redir->insert();
+
+            if (!$result) {
+                common_log_db_error($file_redir, "INSERT", __FILE__);
+                $this->clientError(_('There was a database error while saving your file. Please try again.'));
+            }
         }
     }
 
+    function attachFile($notice, $filerec)
+    {
+        File_to_post::processNew($filerec->id, $notice->id);
+
+        $this->maybeAddRedir($filerec->id,
+            common_local_url('file', array('notice' => $notice->id)));
+    }
+
     /**
      * Show an Ajax-y error message
      *