Media files are accepted and uploaded via Twitter API now

This commit is contained in:
Mikael Nordfeldth 2016-01-14 19:15:02 +01:00
parent 2af9de4f23
commit b570f0d3cb

View File

@ -42,25 +42,23 @@ class ApiMediaUploadAction extends ApiAuthAction
{ {
protected $needPost = true; protected $needPost = true;
/** protected function prepare(array $args=array())
* 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
*/
protected function handle()
{ {
parent::handle(); parent::prepare($args);
// fallback to xml for older clients etc // fallback to xml for older clients etc
if (empty($this->format)) { if (empty($this->format)) {
$this->format = 'xml'; $this->format = 'xml';
} }
if (!in_array($this->format, ['json', 'xml'])) {
throw new ClientException('This API call does not support the format '._ve($this->format));
}
return true;
}
protected function handle()
{
parent::handle();
// 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
@ -77,14 +75,28 @@ class ApiMediaUploadAction extends ApiAuthAction
throw new ClientException(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); throw new ClientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
} }
// we could catch "NoUploadedMediaException" as "no media uploaded", but here we _always_ want an upload try {
$upload = MediaFile::fromUpload('media', $this->scoped); $upload = MediaFile::fromUpload('media', $this->scoped);
} catch (NoUploadedMediaException $e) {
// Thumbnails will be generated/cached on demand when accessed (such as with /attachment/:id/thumbnail) common_debug('No media file was uploaded to the _FILES array');
$fh = tmpfile();
if (!in_array($this->format, ['json', 'xml'])) { if ($this->arg('media')) {
throw new ClientException('This API call does not support the format '._ve($this->format)); common_debug('Found media parameter which we hope contains a media file!');
fwrite($fh, $this->arg('media'));
} elseif ($this->arg('media_data')) {
common_debug('Found media_data parameter which we hope contains a base64-encoded media file!');
fwrite($fh, base64_decode($this->arg('media_data')));
} else {
common_debug('No media|media_data POST parameter was supplied');
fclose($fh);
throw $e;
}
common_debug('MediaFile importing the uploaded file with fromFilehandle');
$upload = MediaFile::fromFilehandle($fh, $this->scoped);
} }
common_debug('MediaFile completed and saved us fileRecord with id=='._ve($upload->fileRecord->id));
// Thumbnails will be generated/cached on demand when accessed (such as with /attachment/:id/thumbnail)
$this->showResponse($upload); $this->showResponse($upload);
} }