]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/apimediaupload.php
DocAction now extends ManagedAction
[quix0rs-gnu-social.git] / actions / apimediaupload.php
index 7aa88c186bcc58c3592393e6d90bda8f7596feaf..8e59fec59c963c728b4c96456bcbd5def2247a86 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
-
-require_once INSTALLDIR . '/lib/apiauth.php';
-require_once INSTALLDIR . '/lib/mediafile.php';
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Upload an image via the API.  Returns a shortened URL for the image
@@ -43,9 +38,10 @@ require_once INSTALLDIR . '/lib/mediafile.php';
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-
 class ApiMediaUploadAction extends ApiAuthAction
 {
+    protected $needPost = true;
+
     /**
      * Handle the request
      *
@@ -57,18 +53,9 @@ class ApiMediaUploadAction extends ApiAuthAction
      *
      * @return void
      */
-
-    function handle($args)
+    protected function handle()
     {
-        parent::handle($args);
-
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-            $this->clientError(
-                _('This method requires a POST.'),
-                400, $this->format
-            );
-            return;
-        }
+        parent::handle();
 
         // Workaround for PHP returning empty $_POST and $_FILES when POST
         // length > post_max_size in php.ini
@@ -77,39 +64,31 @@ class ApiMediaUploadAction extends ApiAuthAction
             && empty($_POST)
             && ($_SERVER['CONTENT_LENGTH'] > 0)
         ) {
-             $msg = _('The server was unable to handle that much POST ' .
-                    'data (%s bytes) due to its current configuration.');
-
+            // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
+            // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
+            $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
+                      'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
+                      intval($_SERVER['CONTENT_LENGTH']));
             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
-            return;
         }
 
-        $upload = null;
-
-        try {
-            $upload = MediaFile::fromUpload('media', $this->auth_user);
-        } catch (Exception $e) {
-            $this->clientError($e->getMessage(), $e->getCode());
-            return;
-        }
+        // we could catch "NoUploadedMediaException" as "no media uploaded", but here we _always_ want an upload
+        $upload = MediaFile::fromUpload('media', $this->scoped);
+        
+        // Thumbnails will be generated/cached on demand when accessed (such as with /attachment/:id/thumbnail)
 
-        if (isset($upload)) {
-            $this->showResponse($upload);
-        } else {
-            $this->clientError(_('Upload failed.'));
-            return;
-        }
+        $this->showResponse($upload);
     }
 
     /**
      * Show a Twitpic-like response with the ID of the media file
      * and a (hopefully) shortened URL for it.
      *
-     * @param File $upload  the uploaded file
+     * @param MediaFile $upload  the uploaded file
      *
      * @return void
      */
-    function showResponse($upload)
+    function showResponse(MediaFile $upload)
     {
         $this->initDocument();
         $this->elementStart('rsp', array('stat' => 'ok'));
@@ -123,7 +102,6 @@ class ApiMediaUploadAction extends ApiAuthAction
      * Overrided clientError to show a more Twitpic-like error
      *
      * @param String $msg an error message
-     *
      */
     function clientError($msg)
     {
@@ -137,5 +115,4 @@ class ApiMediaUploadAction extends ApiAuthAction
         $this->elementEnd('rsp');
         $this->endDocument();
     }
-
 }