Implement media upload in the API

This commit is contained in:
Zach Copley 2009-10-27 21:45:56 -07:00
parent 93a6e83d5d
commit 2d0aba49d9
2 changed files with 106 additions and 72 deletions

View File

@ -38,6 +38,7 @@ if (!defined('STATUSNET')) {
} }
require_once INSTALLDIR . '/lib/apiauth.php'; require_once INSTALLDIR . '/lib/apiauth.php';
require_once INSTALLDIR . '/lib/mediafile.php';
/** /**
* Updates the authenticating user's status (posts a notice). * Updates the authenticating user's status (posts a notice).
@ -60,7 +61,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction
var $source = null; var $source = null;
var $status = null; var $status = null;
var $in_reply_to_status_id = null; var $in_reply_to_status_id = null;
static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api'); static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api');
/** /**
@ -77,24 +77,7 @@ class ApiStatusesUpdateAction extends ApiAuthAction
parent::prepare($args); parent::prepare($args);
$this->user = $this->auth_user; $this->user = $this->auth_user;
if (empty($this->user)) {
$this->clientError(_('No such user!'), 404, $this->format);
return false;
}
$this->status = $this->trimmed('status'); $this->status = $this->trimmed('status');
if (empty($this->status)) {
$this->clientError(
'Client must provide a \'status\' parameter with a value.',
400,
$this->format
);
return false;
}
$this->source = $this->trimmed('source'); $this->source = $this->trimmed('source');
if (empty($this->source) || in_array($source, $this->reserved_sources)) { if (empty($this->source) || in_array($source, $this->reserved_sources)) {
@ -129,6 +112,27 @@ class ApiStatusesUpdateAction extends ApiAuthAction
return; return;
} }
if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
$this->clientError(sprintf(_('The server was unable to handle ' .
'that much POST data (%s bytes) due to its current configuration.'),
$_SERVER['CONTENT_LENGTH']));
return;
}
if (empty($this->user)) {
$this->clientError(_('No such user!'), 404, $this->format);
return;
}
if (empty($this->status)) {
$this->clientError(
'Client must provide a \'status\' parameter with a value.',
400,
$this->format
);
return;
}
$status_shortened = common_shorten_links($this->status); $status_shortened = common_shorten_links($this->status);
if (Notice::contentTooLong($status_shortened)) { if (Notice::contentTooLong($status_shortened)) {
@ -187,14 +191,42 @@ class ApiStatusesUpdateAction extends ApiAuthAction
} }
} }
$upload = null;
common_debug('looking for attachment');
$upload = MediaFile::fromUpload('media', $this->user);
common_debug("uploaded file = " . var_export($upload, true));
if (isset($upload)) {
common_debug('newNotice: found an upload');
$status_shortened .= ' ' . $upload->shortUrl();
common_debug('content w/upload = ' . $status_shortened);
if (Notice::contentTooLong($status_shortened)) {
$upload->delete();
$this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'),
Notice::maxContent()));
} else {
common_debug('content not too long');
}
}
$this->notice = Notice::saveNew( $this->notice = Notice::saveNew(
$this->user->id, $this->user->id,
html_entity_decode($this->status, ENT_NOQUOTES, 'UTF-8'), html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'),
$this->source, $this->source,
1, 1,
$reply_to $reply_to
); );
if (isset($upload)) {
$upload->attachToNotice($this->notice);
}
common_broadcast_notice($this->notice); common_broadcast_notice($this->notice);
} }

View File

@ -158,10 +158,14 @@ class MediaFile
} }
} }
static function fromUpload($param = 'media') static function fromUpload($param = 'media', $user = null)
{ {
common_debug("fromUpload: param = $param"); common_debug("fromUpload: param = $param");
if (empty($user)) {
$user = common_current_user();
}
if (!isset($_FILES[$param]['error'])){ if (!isset($_FILES[$param]['error'])){
common_debug('no file found'); common_debug('no file found');
return; return;
@ -198,8 +202,6 @@ class MediaFile
return; return;
} }
$user = common_current_user();
if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) { if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) {
// Should never actually get here // Should never actually get here
@ -236,7 +238,7 @@ class MediaFile
return new MediaFile($user, $filename, $mimetype); return new MediaFile($user, $filename, $mimetype);
} }
static function fromFilehandle($user, $fh) { static function fromFilehandle($fh, $user) {
$stream = stream_get_meta_data($fh); $stream = stream_get_meta_data($fh);