MediaFile upload simplifying

This commit is contained in:
Mikael Nordfeldth 2015-01-21 17:32:57 +01:00
parent 25911368c8
commit 8b585ff647
5 changed files with 56 additions and 44 deletions

View File

@ -26,9 +26,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
/** /**
* Upload an image via the API. Returns a shortened URL for the image * Upload an image via the API. Returns a shortened URL for the image
@ -42,6 +40,8 @@ if (!defined('STATUSNET')) {
*/ */
class ApiMediaUploadAction extends ApiAuthAction class ApiMediaUploadAction extends ApiAuthAction
{ {
protected $needPost = true;
/** /**
* Handle the request * Handle the request
* *
@ -53,17 +53,9 @@ class ApiMediaUploadAction extends ApiAuthAction
* *
* @return void * @return void
*/ */
function handle($args) protected function handle()
{ {
parent::handle($args); parent::handle();
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
);
}
// Workaround for PHP returning empty $_POST and $_FILES when POST // Workaround for PHP returning empty $_POST and $_FILES when POST
// length > post_max_size in php.ini // length > post_max_size in php.ini
@ -80,31 +72,21 @@ class ApiMediaUploadAction extends ApiAuthAction
$this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
} }
$upload = null; // we could catch "NoUploadedMediaException" as "no media uploaded", but here we _always_ want an upload
try {
$upload = MediaFile::fromUpload('media', $this->auth_user->getProfile()); $upload = MediaFile::fromUpload('media', $this->auth_user->getProfile());
} catch (Exception $e) {
$this->clientError($e->getMessage(), $e->getCode());
}
if (isset($upload)) {
$this->showResponse($upload); $this->showResponse($upload);
} else {
// TRANS: Client error displayed when uploading a media file has failed.
$this->clientError(_('Upload failed.'));
}
} }
/** /**
* Show a Twitpic-like response with the ID of the media file * Show a Twitpic-like response with the ID of the media file
* and a (hopefully) shortened URL for it. * and a (hopefully) shortened URL for it.
* *
* @param File $upload the uploaded file * @param MediaFile $upload the uploaded file
* *
* @return void * @return void
*/ */
function showResponse($upload) function showResponse(MediaFile $upload)
{ {
$this->initDocument(); $this->initDocument();
$this->elementStart('rsp', array('stat' => 'ok')); $this->elementStart('rsp', array('stat' => 'ok'));

View File

@ -245,24 +245,19 @@ class ApiStatusesUpdateAction extends ApiAuthAction
} }
$upload = null; $upload = null;
try { try {
$upload = MediaFile::fromUpload('media', $this->scoped); $upload = MediaFile::fromUpload('media', $this->scoped);
} catch (Exception $e) {
$this->clientError($e->getMessage(), $e->getCode());
}
if ($upload instanceof MediaFile) {
$this->status .= ' ' . $upload->shortUrl(); $this->status .= ' ' . $upload->shortUrl();
/* Do not call shortenlinks until the whole notice has been build */ /* Do not call shortenlinks until the whole notice has been build */
} catch (NoUploadedMediaException $e) {
// There was no uploaded media for us today.
} }
/* Do call shortenlinks here & check notice length since notice is about to be saved & sent */ /* Do call shortenlinks here & check notice length since notice is about to be saved & sent */
$status_shortened = $this->auth_user->shortenlinks($this->status); $status_shortened = $this->auth_user->shortenlinks($this->status);
if (Notice::contentTooLong($status_shortened)) { if (Notice::contentTooLong($status_shortened)) {
if (isset($upload)) { if ($upload instanceof MediaFile) {
$upload->delete(); $upload->delete();
} }
// TRANS: Client error displayed exceeding the maximum notice length. // TRANS: Client error displayed exceeding the maximum notice length.

View File

@ -129,10 +129,10 @@ class NewnoticeAction extends FormAction
$options['reply_to'] = $replyto; $options['reply_to'] = $replyto;
} }
$upload = null;
try {
// throws exception on failure
$upload = MediaFile::fromUpload('attach', $this->scoped); $upload = MediaFile::fromUpload('attach', $this->scoped);
if (isset($upload)) {
if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) { if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content_shortened, &$options))) {
$content_shortened .= ' ' . $upload->shortUrl(); $content_shortened .= ' ' . $upload->shortUrl();
} }
@ -147,8 +147,11 @@ class NewnoticeAction extends FormAction
Notice::maxContent()), Notice::maxContent()),
Notice::maxContent())); Notice::maxContent()));
} }
} catch (NoUploadedMediaException $e) {
// simply no attached media to the new notice
} }
if ($this->scoped->shareLocation()) { if ($this->scoped->shareLocation()) {
// use browser data if checked; otherwise profile data // use browser data if checked; otherwise profile data
if ($this->arg('notice_data-geo')) { if ($this->arg('notice_data-geo')) {
@ -179,7 +182,7 @@ class NewnoticeAction extends FormAction
$this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options); $this->stored = Notice::saveNew($this->scoped->id, $content_shortened, 'web', $options);
if (isset($upload)) { if ($upload instanceof MediaFile) {
$upload->attachToNotice($this->stored); $upload->attachToNotice($this->stored);
} }

View File

@ -147,14 +147,15 @@ class MediaFile
} }
} }
static function fromUpload($param = 'media', Profile $scoped) static function fromUpload($param='media', Profile $scoped=null)
{ {
if (is_null($scoped)) { if (is_null($scoped)) {
$scoped = Profile::current(); $scoped = Profile::current();
} }
if (!isset($_FILES[$param]['error'])){ // The existence of the "error" element means PHP has processed it properly even if it was ok.
return; if (!isset($_FILES[$param]) || !isset($_FILES[$param]['error'])) {
throw new NoUploadedMediaException($param);
} }
switch ($_FILES[$param]['error']) { switch ($_FILES[$param]['error']) {

View File

@ -0,0 +1,31 @@
<?php
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Parent class for an exception when a POST upload does not contain a file.
*
* @category Exception
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://gnu.io/
*/
class NoUploadedMediaException extends ClientException
{
public $fieldname = null;
public function __construct($fieldname, $msg=null)
{
$this->fieldname = $fieldname;
if ($msg === null) {
// TRANS: Exception text shown when no uploaded media was provided in POST
// TRANS: %s is the HTML input field name.
$msg = sprintf(_('There is no uploaded media for input field "%s".'), $this->fieldname);
}
parent::__construct($msg, 400);
}
}