apimediaupload.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Upload an image via the API
  6. *
  7. * PHP version 5
  8. *
  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.
  13. *
  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.
  18. *
  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/>.
  21. *
  22. * @category API
  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/
  27. */
  28. if (!defined('GNUSOCIAL')) { exit(1); }
  29. /**
  30. * Upload an image via the API. Returns a shortened URL for the image
  31. * to the user.
  32. *
  33. * @category API
  34. * @package StatusNet
  35. * @author Zach Copley <zach@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://status.net/
  38. */
  39. class ApiMediaUploadAction extends ApiAuthAction
  40. {
  41. protected $needPost = true;
  42. /**
  43. * Handle the request
  44. *
  45. * Grab the file from the 'media' param, then store, and shorten
  46. *
  47. * @todo Upload throttle!
  48. *
  49. * @param array $args $_REQUEST data (unused)
  50. *
  51. * @return void
  52. */
  53. protected function handle()
  54. {
  55. parent::handle();
  56. // Workaround for PHP returning empty $_POST and $_FILES when POST
  57. // length > post_max_size in php.ini
  58. if (empty($_FILES)
  59. && empty($_POST)
  60. && ($_SERVER['CONTENT_LENGTH'] > 0)
  61. ) {
  62. // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
  63. // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
  64. $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
  65. 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
  66. intval($_SERVER['CONTENT_LENGTH']));
  67. $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  68. }
  69. // we could catch "NoUploadedMediaException" as "no media uploaded", but here we _always_ want an upload
  70. $upload = MediaFile::fromUpload('media', $this->scoped);
  71. // Thumbnails will be generated/cached on demand when accessed (such as with /attachment/:id/thumbnail)
  72. $this->showResponse($upload);
  73. }
  74. /**
  75. * Show a Twitpic-like response with the ID of the media file
  76. * and a (hopefully) shortened URL for it.
  77. *
  78. * @param MediaFile $upload the uploaded file
  79. *
  80. * @return void
  81. */
  82. function showResponse(MediaFile $upload)
  83. {
  84. $this->initDocument();
  85. $this->elementStart('rsp', array('stat' => 'ok'));
  86. $this->element('mediaid', null, $upload->fileRecord->id);
  87. $this->element('mediaurl', null, $upload->shortUrl());
  88. $this->elementEnd('rsp');
  89. $this->endDocument();
  90. }
  91. /**
  92. * Overrided clientError to show a more Twitpic-like error
  93. *
  94. * @param String $msg an error message
  95. */
  96. function clientError($msg)
  97. {
  98. $this->initDocument();
  99. $this->elementStart('rsp', array('stat' => 'fail'));
  100. // @todo add in error code
  101. $errAttr = array('msg' => $msg);
  102. $this->element('err', $errAttr, null);
  103. $this->elementEnd('rsp');
  104. $this->endDocument();
  105. }
  106. }