3 * StatusNet, the distributed open-source microblogging tool
5 * Upload an image via the API
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @author Zach Copley <zach@status.net>
24 * @copyright 2010 StatusNet, Inc.
25 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26 * @link http://status.net/
29 if (!defined('STATUSNET')) {
33 require_once INSTALLDIR . '/lib/apiauth.php';
34 require_once INSTALLDIR . '/lib/mediafile.php';
37 * Upload an image via the API. Returns a shortened URL for the image
42 * @author Zach Copley <zach@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
47 class ApiMediaUploadAction extends ApiAuthAction
52 * Grab the file from the 'media' param, then store, and shorten
54 * @todo Upload throttle!
56 * @param array $args $_REQUEST data (unused)
61 function handle($args)
63 parent::handle($args);
65 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
67 // TRANS: Client error. POST is a HTTP command. It should not be translated.
68 _('This method requires a POST.'),
74 // Workaround for PHP returning empty $_POST and $_FILES when POST
75 // length > post_max_size in php.ini
79 && ($_SERVER['CONTENT_LENGTH'] > 0)
81 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
82 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
83 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
84 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
85 intval($_SERVER['CONTENT_LENGTH']));
86 $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
93 $upload = MediaFile::fromUpload('media', $this->auth_user);
94 } catch (Exception $e) {
95 $this->clientError($e->getMessage(), $e->getCode());
100 $this->showResponse($upload);
102 $this->clientError(_('Upload failed.'));
108 * Show a Twitpic-like response with the ID of the media file
109 * and a (hopefully) shortened URL for it.
111 * @param File $upload the uploaded file
115 function showResponse($upload)
117 $this->initDocument();
118 $this->elementStart('rsp', array('stat' => 'ok'));
119 $this->element('mediaid', null, $upload->fileRecord->id);
120 $this->element('mediaurl', null, $upload->shortUrl());
121 $this->elementEnd('rsp');
122 $this->endDocument();
126 * Overrided clientError to show a more Twitpic-like error
128 * @param String $msg an error message
131 function clientError($msg)
133 $this->initDocument();
134 $this->elementStart('rsp', array('stat' => 'fail'));
136 // @todo add in error code
137 $errAttr = array('msg' => $msg);
139 $this->element('err', $errAttr, null);
140 $this->elementEnd('rsp');
141 $this->endDocument();