]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/apimediaupload.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / actions / apimediaupload.php
index 14c26d238f677f2cb0d92464e9d5613ae2db1655..c2b4a47b9a277d799acd799d5d1e2c4a597f22fb 100644 (file)
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Upload an image via the API.  Returns a shortened URL for the image
- * to the user.
+ * to the user. Apparently modelled after a former Twitpic API.
  *
  * @category API
  * @package  StatusNet
@@ -42,28 +40,19 @@ if (!defined('STATUSNET')) {
  */
 class ApiMediaUploadAction extends ApiAuthAction
 {
+    protected $needPost = true;
+
     /**
      * Handle the request
      *
      * Grab the file from the 'media' param, then store, and shorten
      *
      * @todo Upload throttle!
-     *
-     * @param array $args $_REQUEST data (unused)
-     *
      * @return void
      */
-    function handle(array $args=array())
+    protected function handle()
     {
-        parent::handle($args);
-
-        if ($_SERVER['REQUEST_METHOD'] != 'POST') {
-            $this->clientError(
-                // TRANS: Client error. POST is a HTTP command. It should not be translated.
-                _('This method requires a POST.'),
-                400, $this->format
-            );
-        }
+        parent::handle();
 
         // Workaround for PHP returning empty $_POST and $_FILES when POST
         // length > post_max_size in php.ini
@@ -80,36 +69,33 @@ class ApiMediaUploadAction extends ApiAuthAction
             $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
         }
 
-        $upload = null;
-
-        try {
-            $upload = MediaFile::fromUpload('media', $this->auth_user->getProfile());
-        } catch (Exception $e) {
-            $this->clientError($e->getMessage(), $e->getCode());
-        }
+        // 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 {
-            // TRANS: Client error displayed when uploading a media file has failed.
-            $this->clientError(_('Upload failed.'));
-        }
+        $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'));
+        $this->elementStart('rsp', array('stat' => 'ok', 'xmlns:atom'=>Activity::ATOM));
         $this->element('mediaid', null, $upload->fileRecord->id);
         $this->element('mediaurl', null, $upload->shortUrl());
+
+        $enclosure = $upload->fileRecord->getEnclosure();
+        $this->element('atom:link', array('rel'  => 'enclosure',
+                                          'href' => $enclosure->url,
+                                          'type' => $enclosure->mimetype));
         $this->elementEnd('rsp');
         $this->endDocument();
     }
@@ -119,7 +105,7 @@ class ApiMediaUploadAction extends ApiAuthAction
      *
      * @param String $msg an error message
      */
-    function clientError($msg)
+    function clientError($msg, $code=400, $format=null)
     {
         $this->initDocument();
         $this->elementStart('rsp', array('stat' => 'fail'));
@@ -130,5 +116,6 @@ class ApiMediaUploadAction extends ApiAuthAction
         $this->element('err', $errAttr, null);
         $this->elementEnd('rsp');
         $this->endDocument();
+        exit;
     }
 }