Created
September 26, 2012 14:45
-
-
Save mrvdb/3788463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 68257ce4f4005233eb60ced038d4b43244912ad1 | |
Author: Marcel van der Boom <[email protected]> | |
Date: Thu Jul 19 14:29:40 2012 +0200 | |
Upload support for clients which issue update_with_media | |
- normalize the media parameter, its structure depends on how POST data is collected | |
- add router entyr point for statuses/update_with_media as alias for statuses/update | |
diff --git a/lib/mediafile.php b/lib/mediafile.php | |
index caa902d..b4c71a4 100644 | |
--- a/lib/mediafile.php | |
+++ b/lib/mediafile.php | |
@@ -176,17 +176,41 @@ class MediaFile | |
} | |
} | |
+ private static function normalize_files_param($files, $param) { | |
+ // This function exists because the media parameter has two | |
+ // appearances depending how clients submit their data, not | |
+ // whose fault this is, but we try to cope. As we support only | |
+ // one upload item for now we can keep things simple and | |
+ // complicate matters later if we want. | |
+ if ( is_array($files[$param]['name']) ) { | |
+ // Get all the first elements of the array | |
+ $files[$param]['name'] = $files[$param]['name'][0]; | |
+ $files[$param]['size'] = $files[$param]['size'][0]; | |
+ $files[$param]['type'] = $files[$param]['type'][0]; | |
+ $files[$param]['error'] = $files[$param]['error'][0]; | |
+ $files[$param]['tmp_name'] = $files[$param]['tmp_name'][0]; | |
+ } | |
+ return $files; | |
+ } | |
+ | |
static function fromUpload($param = 'media', $user = null) | |
{ | |
if (empty($user)) { | |
$user = common_current_user(); | |
} | |
- if (!isset($_FILES[$param]['error'])){ | |
+ // The media array that comes in can have two forms, make sure | |
+ // we only have to deal with one format. See | |
+ // http://php.net/manual/en/reserved.variables.files.php for | |
+ // details. | |
+ $filedata = self::normalize_files_param($_FILES, $param); | |
+ | |
+ if (!isset($filedata[$param]['error'])){ | |
return; | |
} | |
- switch ($_FILES[$param]['error']) { | |
+ common_log(LOG_ERR, __METHOD__ . ": FILES data: " . print_r($filedata,true)); | |
+ switch ($filedata[$param]['error']) { | |
case UPLOAD_ERR_OK: // success, jump out | |
break; | |
case UPLOAD_ERR_INI_SIZE: | |
@@ -201,14 +225,11 @@ class MediaFile | |
' that was specified in the HTML form.')); | |
return; | |
case UPLOAD_ERR_PARTIAL: | |
- @unlink($_FILES[$param]['tmp_name']); | |
+ @unlink($filedata[$param]['tmp_name']); | |
// TRANS: Client exception. | |
throw new ClientException(_('The uploaded file was only' . | |
' partially uploaded.')); | |
return; | |
- case UPLOAD_ERR_NO_FILE: | |
- // No file; probably just a non-AJAX submission. | |
- return; | |
case UPLOAD_ERR_NO_TMP_DIR: | |
// TRANS: Client exception thrown when a temporary folder is not present to store a file upload. | |
throw new ClientException(_('Missing a temporary folder.')); | |
@@ -221,36 +242,38 @@ class MediaFile | |
// TRANS: Client exception thrown when a file upload operation has been stopped by an extension. | |
throw new ClientException(_('File upload stopped by extension.')); | |
return; | |
+ case UPLOAD_ERR_NO_FILE: | |
+ // No file; probably just a non-AJAX submission. | |
default: | |
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . | |
- $_FILES[$param]['error']); | |
+ $filedata[$param]['error']); | |
// TRANS: Client exception thrown when a file upload operation has failed with an unknown reason. | |
throw new ClientException(_('System error uploading file.')); | |
return; | |
} | |
- if (!MediaFile::respectsQuota($user, $_FILES[$param]['size'])) { | |
+ if (!MediaFile::respectsQuota($user, $filedata[$param]['size'])) { | |
// Should never actually get here | |
- @unlink($_FILES[$param]['tmp_name']); | |
+ @unlink($filedata[$param]['tmp_name']); | |
// TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota. | |
throw new ClientException(_('File exceeds user\'s quota.')); | |
return; | |
} | |
- $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name'], | |
- $_FILES[$param]['name']); | |
+ $mimetype = MediaFile::getUploadedFileType($filedata[$param]['tmp_name'], | |
+ $filedata[$param]['name']); | |
$filename = null; | |
if (isset($mimetype)) { | |
- $basename = basename($_FILES[$param]['name']); | |
+ $basename = basename($filedata[$param]['name']); | |
$filename = File::filename($user->getProfile(), $basename, $mimetype); | |
$filepath = File::path($filename); | |
- $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); | |
+ $result = move_uploaded_file($filedata[$param]['tmp_name'], $filepath); | |
if (!$result) { | |
// TRANS: Client exception thrown when a file upload operation fails because the file could | |
diff --git a/lib/router.php b/lib/router.php | |
index 25c436a..c3885d5 100644 | |
--- a/lib/router.php | |
+++ b/lib/router.php | |
@@ -445,6 +445,10 @@ class Router | |
array('action' => 'ApiStatusesUpdate', | |
'format' => '(xml|json)')); | |
+ $m->connect('api/statuses/update_with_media.:format', | |
+ array('action' => 'ApiStatusesUpdate', | |
+ 'format' => '(xml|json)')); | |
+ | |
$m->connect('api/statuses/destroy/:id.:format', | |
array('action' => 'ApiStatusesDestroy', | |
'id' => '[0-9]+', |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment