From 93a6e83d5ddb48ccae80ba9f5c62c63888724083 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 27 Oct 2009 19:11:18 -0700 Subject: Extract media upload stuff into its own library class. --- lib/mediafile.php | 322 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 lib/mediafile.php (limited to 'lib/mediafile.php') diff --git a/lib/mediafile.php b/lib/mediafile.php new file mode 100644 index 000000000..be101cdca --- /dev/null +++ b/lib/mediafile.php @@ -0,0 +1,322 @@ +. + * + * @category Media + * @package StatusNet + * @author Zach Copley + * @copyright 2008-2009 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET') && !defined('LACONICA')) { + exit(1); +} + +class MediaFile +{ + + var $filename = null; + var $fileRecord = null; + var $user = null; + var $fileurl = null; + var $short_fileurl = null; + var $mimetype = null; + + function __construct($user = null, $filename = null, $mimetype = null) + { + if ($user == null) { + $this->user = common_current_user(); + } + + common_debug('in MediaFile constructor'); + + $this->filename = $filename; + $this->mimetype = $mimetype; + + common_debug('storing file'); + $this->fileRecord = $this->storeFile(); + common_debug('finished storing file'); + + $this->fileurl = common_local_url('attachment', + array('attachment' => $this->fileRecord->id)); + + common_debug('$this->fileurl() = ' . $this->fileurl); + + // not sure this is necessary -- Zach + $this->maybeAddRedir($this->fileRecord->id, $this->fileurl); + + common_debug('shortening file url'); + $this->short_fileurl = common_shorten_url($this->fileurl); + common_debug('shortened file url = ' . $short_fileurl); + + // Also, not sure this is necessary -- Zach + $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); + + common_debug("MediaFile: end of constructor"); + } + + function attachToNotice($notice) + { + common_debug('MediaFile::attachToNotice() -- doing File_to_post'); + File_to_post::processNew($this->fileRecord->id, $notice->id); + common_debug('MediaFile done doing File_to_post'); + + $this->maybeAddRedir($this->fileRecord->id, + common_local_url('file', array('notice' => $notice->id))); + } + + function shortUrl() + { + return $this->short_fileurl; + } + + function delete() + { + $filepath = File::path($this->filename); + @unlink($filepath); + } + + function storeFile() { + + $file = new File; + $file->filename = $this->filename; + + common_debug('storing ' . $this->filename); + + $file->url = File::url($this->filename); + common_debug('file->url = ' . $file->url); + + $filepath = File::path($this->filename); + common_debug('filepath = ' . $filepath); + + $file->size = filesize($filepath); + $file->date = time(); + $file->mimetype = $this->mimetype; + + $file_id = $file->insert(); + + if (!$file_id) { + + common_debug("storeFile: problem inserting new file"); + common_log_db_error($file, "INSERT", __FILE__); + throw new ClientException(_('There was a database error while saving your file. Please try again.')); + } + + common_debug('finished storing file'); + + return $file; + } + + function rememberFile($file, $short) + { + $this->maybeAddRedir($file->id, $short); + } + + function maybeAddRedir($file_id, $url) + { + + common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); + + $file_redir = File_redirection::staticGet('url', $url); + + if (empty($file_redir)) { + + common_debug("maybeAddRedir: $url is not in the db"); + + $file_redir = new File_redirection; + $file_redir->url = $url; + $file_redir->file_id = $file_id; + + $result = $file_redir->insert(); + + if (!$result) { + common_log_db_error($file_redir, "INSERT", __FILE__); + throw new ClientException(_('There was a database error while saving your file. Please try again.')); + } + } else { + + common_debug("maybeAddRedir: no need to add $url, it's already in the db"); + } + } + + static function fromUpload($param = 'media') + { + common_debug("fromUpload: param = $param"); + + if (!isset($_FILES[$param]['error'])){ + common_debug('no file found'); + return; + } + + switch ($_FILES[$param]['error']) { + case UPLOAD_ERR_OK: // success, jump out + break; + case UPLOAD_ERR_INI_SIZE: + throw new ClientException(_('The uploaded file exceeds the ' . + 'upload_max_filesize directive in php.ini.')); + return; + case UPLOAD_ERR_FORM_SIZE: + throw new ClientException( + _('The uploaded file exceeds the MAX_FILE_SIZE directive' . + ' that was specified in the HTML form.')); + return; + case UPLOAD_ERR_PARTIAL: + @unlink($_FILES[$param]['tmp_name']); + throw new ClientException(_('The uploaded file was only' . + ' partially uploaded.')); + return; + case UPLOAD_ERR_NO_TMP_DIR: + throw new ClientException(_('Missing a temporary folder.')); + return; + case UPLOAD_ERR_CANT_WRITE: + throw new ClientException(_('Failed to write file to disk.')); + return; + case UPLOAD_ERR_EXTENSION: + throw new ClientException(_('File upload stopped by extension.')); + return; + default: + throw new ClientException(_('System error uploading file.')); + return; + } + + $user = common_current_user(); + + if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) { + + // Should never actually get here + + @unlink($_FILES[$param]['tmp_name']); + throw new ClientException(_('File exceeds user\'s quota!')); + return; + } + + $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']); + + $filename = null; + + if (isset($mimetype)) { + + $basename = basename($_FILES[$param]['name']); + $filename = File::filename($user->getProfile(), $basename, $mimetype); + $filepath = File::path($filename); + + common_debug("filepath = " . $filepath); + + $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); + + if (!$result) { + throw new ClientException(_('File could not be moved to destination directory.')); + return; + } + + } else { + throw new ClientException(_('Could not determine file\'s mime-type!')); + return; + } + + return new MediaFile($user, $filename, $mimetype); + } + + static function fromFilehandle($user, $fh) { + + $stream = stream_get_meta_data($fh); + + if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { + + // Should never actually get here + + throw new ClientException(_('File exceeds user\'s quota!')); + return; + } + + $mimetype = MediaFile::getUploadedFileType($fh); + + $filename = null; + + if (isset($mimetype)) { + + $filename = File::filename($user->getProfile(), "email", $mimetype); + + $filepath = File::path($filename); + + $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); + + if (!$result) { + throw new ClientException(_('File could not be moved to destination directory.' . + $stream['uri'] . ' ' . $filepath)); + } + } else { + throw new ClientException(_('Could not determine file\'s mime-type!')); + return; + } + + return new MediaFile($user, $filename, $mimetype); + } + + static function getUploadedFileType($f) { + require_once 'MIME/Type.php'; + + common_debug("in getUploadedFileType"); + + $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); + $cmd = common_config('attachments', 'filecommand'); + + $filetype = null; + + if (is_string($f)) { + + // assuming a filename + + $filetype = MIME_Type::autoDetect($f); + } else { + + // assuming a filehandle + + $stream = stream_get_meta_data($f); + $filetype = MIME_Type::autoDetect($stream['uri']); + } + + if (in_array($filetype, common_config('attachments', 'supported'))) { + return $filetype; + } + $media = MIME_Type::getMedia($filetype); + if ('application' !== $media) { + $hint = sprintf(_(' Try using another %s format.'), $media); + } else { + $hint = ''; + } + throw new ClientException(sprintf( + _('%s is not a supported filetype on this server.'), $filetype) . $hint); + } + + static function respectsQuota($user, $filesize) + { + $file = new File; + $result = $file->isRespectsQuota($user, $filesize); + if ($result === true) { + return true; + } else { + throw new ClientException($result); + } + } + +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 2d0aba49d913b7d4598073d5ff56237f736731d0 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 27 Oct 2009 21:45:56 -0700 Subject: Implement media upload in the API --- actions/apistatusesupdate.php | 72 ++++++++++++++++++++-------- lib/mediafile.php | 106 +++++++++++++++++++++--------------------- 2 files changed, 106 insertions(+), 72 deletions(-) (limited to 'lib/mediafile.php') diff --git a/actions/apistatusesupdate.php b/actions/apistatusesupdate.php index 0d71e1512..b7128d084 100644 --- a/actions/apistatusesupdate.php +++ b/actions/apistatusesupdate.php @@ -38,6 +38,7 @@ if (!defined('STATUSNET')) { } require_once INSTALLDIR . '/lib/apiauth.php'; +require_once INSTALLDIR . '/lib/mediafile.php'; /** * Updates the authenticating user's status (posts a notice). @@ -60,7 +61,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction var $source = null; var $status = null; var $in_reply_to_status_id = null; - static $reserved_sources = array('web', 'omb', 'mail', 'xmpp', 'api'); /** @@ -76,25 +76,8 @@ class ApiStatusesUpdateAction extends ApiAuthAction { parent::prepare($args); - $this->user = $this->auth_user; - - if (empty($this->user)) { - $this->clientError(_('No such user!'), 404, $this->format); - return false; - } - + $this->user = $this->auth_user; $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'); if (empty($this->source) || in_array($source, $this->reserved_sources)) { @@ -129,6 +112,27 @@ class ApiStatusesUpdateAction extends ApiAuthAction 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); 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->user->id, - html_entity_decode($this->status, ENT_NOQUOTES, 'UTF-8'), + html_entity_decode($status_shortened, ENT_NOQUOTES, 'UTF-8'), $this->source, 1, $reply_to ); + if (isset($upload)) { + $upload->attachToNotice($this->notice); + } + common_broadcast_notice($this->notice); } diff --git a/lib/mediafile.php b/lib/mediafile.php index be101cdca..31868daec 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -31,9 +31,9 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } -class MediaFile +class MediaFile { - + var $filename = null; var $fileRecord = null; var $user = null; @@ -46,16 +46,16 @@ class MediaFile if ($user == null) { $this->user = common_current_user(); } - + common_debug('in MediaFile constructor'); - + $this->filename = $filename; $this->mimetype = $mimetype; - + common_debug('storing file'); $this->fileRecord = $this->storeFile(); common_debug('finished storing file'); - + $this->fileurl = common_local_url('attachment', array('attachment' => $this->fileRecord->id)); @@ -67,24 +67,24 @@ class MediaFile common_debug('shortening file url'); $this->short_fileurl = common_shorten_url($this->fileurl); common_debug('shortened file url = ' . $short_fileurl); - + // Also, not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); - + common_debug("MediaFile: end of constructor"); } - + function attachToNotice($notice) { common_debug('MediaFile::attachToNotice() -- doing File_to_post'); File_to_post::processNew($this->fileRecord->id, $notice->id); common_debug('MediaFile done doing File_to_post'); - + $this->maybeAddRedir($this->fileRecord->id, - common_local_url('file', array('notice' => $notice->id))); + common_local_url('file', array('notice' => $notice->id))); } - - function shortUrl() + + function shortUrl() { return $this->short_fileurl; } @@ -115,12 +115,12 @@ class MediaFile $file_id = $file->insert(); if (!$file_id) { - + common_debug("storeFile: problem inserting new file"); common_log_db_error($file, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - + common_debug('finished storing file'); return $file; @@ -133,15 +133,15 @@ class MediaFile function maybeAddRedir($file_id, $url) { - + common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); $file_redir = File_redirection::staticGet('url', $url); if (empty($file_redir)) { - + common_debug("maybeAddRedir: $url is not in the db"); - + $file_redir = new File_redirection; $file_redir->url = $url; $file_redir->file_id = $file_id; @@ -153,20 +153,24 @@ class MediaFile throw new ClientException(_('There was a database error while saving your file. Please try again.')); } } else { - + common_debug("maybeAddRedir: no need to add $url, it's already in the db"); } } - static function fromUpload($param = 'media') + static function fromUpload($param = 'media', $user = null) { common_debug("fromUpload: param = $param"); - + + if (empty($user)) { + $user = common_current_user(); + } + if (!isset($_FILES[$param]['error'])){ common_debug('no file found'); return; } - + switch ($_FILES[$param]['error']) { case UPLOAD_ERR_OK: // success, jump out break; @@ -183,7 +187,7 @@ class MediaFile @unlink($_FILES[$param]['tmp_name']); throw new ClientException(_('The uploaded file was only' . ' partially uploaded.')); - return; + return; case UPLOAD_ERR_NO_TMP_DIR: throw new ClientException(_('Missing a temporary folder.')); return; @@ -197,24 +201,22 @@ class MediaFile throw new ClientException(_('System error uploading file.')); return; } - - $user = common_current_user(); - + if (!MediaFile::respectsQuota($user, $_FILES['attach']['size'])) { - + // Should never actually get here - + @unlink($_FILES[$param]['tmp_name']); throw new ClientException(_('File exceeds user\'s quota!')); return; } $mimetype = MediaFile::getUploadedFileType($_FILES[$param]['tmp_name']); - + $filename = null; - + if (isset($mimetype)) { - + $basename = basename($_FILES[$param]['name']); $filename = File::filename($user->getProfile(), $basename, $mimetype); $filepath = File::path($filename); @@ -222,44 +224,44 @@ class MediaFile common_debug("filepath = " . $filepath); $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); - - if (!$result) { + + if (!$result) { throw new ClientException(_('File could not be moved to destination directory.')); return; } - + } else { throw new ClientException(_('Could not determine file\'s mime-type!')); return; } - + return new MediaFile($user, $filename, $mimetype); } - static function fromFilehandle($user, $fh) { + static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { - + // Should never actually get here - + throw new ClientException(_('File exceeds user\'s quota!')); return; } $mimetype = MediaFile::getUploadedFileType($fh); - + $filename = null; - + if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); - + $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); - + if (!$result) { throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); @@ -268,33 +270,33 @@ class MediaFile throw new ClientException(_('Could not determine file\'s mime-type!')); return; } - + return new MediaFile($user, $filename, $mimetype); } static function getUploadedFileType($f) { require_once 'MIME/Type.php'; - + common_debug("in getUploadedFileType"); $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = common_config('attachments', 'filecommand'); - + $filetype = null; - + if (is_string($f)) { - + // assuming a filename - + $filetype = MIME_Type::autoDetect($f); } else { - + // assuming a filehandle - + $stream = stream_get_meta_data($f); $filetype = MIME_Type::autoDetect($stream['uri']); } - + if (in_array($filetype, common_config('attachments', 'supported'))) { return $filetype; } @@ -308,7 +310,7 @@ class MediaFile _('%s is not a supported filetype on this server.'), $filetype) . $hint); } - static function respectsQuota($user, $filesize) + static function respectsQuota($user, $filesize) { $file = new File; $result = $file->isRespectsQuota($user, $filesize); -- cgit v1.2.3-54-g00ecf From b9ce23d0c2c1266b57997bd0d19891aa653d694c Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 28 Oct 2009 22:24:20 +0000 Subject: Rework MailDaemon to use the MediaFile class for uploads --- lib/mediafile.php | 2 +- scripts/maildaemon.php | 140 ++++++------------------------------------------- 2 files changed, 18 insertions(+), 124 deletions(-) (limited to 'lib/mediafile.php') diff --git a/lib/mediafile.php b/lib/mediafile.php index 31868daec..4aa8211c3 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -242,7 +242,7 @@ class MediaFile $stream = stream_get_meta_data($fh); - if (MediaFile::respectsQuota($user, filesize($stream['uri']))) { + if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { // Should never actually get here diff --git a/scripts/maildaemon.php b/scripts/maildaemon.php index 84dff8912..b4e4d9f08 100755 --- a/scripts/maildaemon.php +++ b/scripts/maildaemon.php @@ -29,6 +29,7 @@ END_OF_HELP; require_once INSTALLDIR.'/scripts/commandline.inc'; require_once(INSTALLDIR . '/lib/mail.php'); +require_once(INSTALLDIR . '/lib/mediafile.php'); require_once('Mail/mimeDecode.php'); # FIXME: we use both Mail_mimeDecode and mailparse @@ -71,43 +72,27 @@ class MailerDaemon 'Max notice size is %d chars.'), Notice::maxContent())); } - $fileRecords = array(); - foreach($attachments as $attachment){ - $mimetype = $this->getUploadedFileType($attachment); - $stream = stream_get_meta_data($attachment); - if (!$this->isRespectsQuota($user,filesize($stream['uri']))) { - die('error() should trigger an exception before reaching here.'); - } - $filename = $this->saveFile($user, $attachment,$mimetype); - - fclose($attachment); - - if (empty($filename)) { - $this->error($from,_('Couldn\'t save file.')); - } - $fileRecord = $this->storeFile($filename, $mimetype); - $fileRecords[] = $fileRecord; - $fileurl = common_local_url('attachment', - array('attachment' => $fileRecord->id)); + $mediafiles = array(); - // not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $fileurl); + foreach($attachments as $attachment){ - $short_fileurl = common_shorten_url($fileurl); - $msg .= ' ' . $short_fileurl; + $mf = null; - if (Notice::contentTooLong($msg)) { - $this->deleteFile($filename); - $this->error($from, sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); + try { + $mf = MediaFile::fromFileHandle($attachment, $user); + } catch(ClientException $ce) { + $this->error($from, $ce->getMessage()); } - // Also, not sure this is necessary -- Zach - $this->maybeAddRedir($fileRecord->id, $short_fileurl); + $msg .= ' ' . $mf->shortUrl(); + + array_push($mediafiles, $mf); + fclose($attachment); } - $err = $this->add_notice($user, $msg, $fileRecords); + $err = $this->add_notice($user, $msg, $mediafiles); + if (is_string($err)) { $this->error($from, $err); return false; @@ -116,89 +101,6 @@ class MailerDaemon } } - function saveFile($user, $attachment, $mimetype) { - - $filename = File::filename($user->getProfile(), "email", $mimetype); - - $filepath = File::path($filename); - - $stream = stream_get_meta_data($attachment); - if (copy($stream['uri'], $filepath) && chmod($filepath,0664)) { - return $filename; - } else { - $this->error(null,_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); - } - } - - function storeFile($filename, $mimetype) { - - $file = new File; - $file->filename = $filename; - - $file->url = File::url($filename); - - $filepath = File::path($filename); - - $file->size = filesize($filepath); - $file->date = time(); - $file->mimetype = $mimetype; - - $file_id = $file->insert(); - - if (!$file_id) { - common_log_db_error($file, "INSERT", __FILE__); - $this->error(null,_('There was a database error while saving your file. Please try again.')); - } - - return $file; - } - - function maybeAddRedir($file_id, $url) - { - $file_redir = File_redirection::staticGet('url', $url); - - if (empty($file_redir)) { - $file_redir = new File_redirection; - $file_redir->url = $url; - $file_redir->file_id = $file_id; - - $result = $file_redir->insert(); - - if (!$result) { - common_log_db_error($file_redir, "INSERT", __FILE__); - $this->error(null,_('There was a database error while saving your file. Please try again.')); - } - } - } - - function getUploadedFileType($fileHandle) { - require_once 'MIME/Type.php'; - - $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); - $cmd = common_config('attachments', 'filecommand'); - - $stream = stream_get_meta_data($fileHandle); - $filetype = MIME_Type::autoDetect($stream['uri']); - if (in_array($filetype, common_config('attachments', 'supported'))) { - return $filetype; - } - $media = MIME_Type::getMedia($filetype); - if ('application' !== $media) { - $hint = sprintf(_(' Try using another %s format.'), $media); - } else { - $hint = ''; - } - $this->error(null,sprintf( - _('%s is not a supported filetype on this server.'), $filetype) . $hint); - } - - function isRespectsQuota($user,$fileSize) { - $file = new File; - $ret = $file->isRespectsQuota($user,$fileSize); - if (true === $ret) return true; - $this->error(null,$ret); - } - function error($from, $msg) { file_put_contents("php://stderr", $msg . "\n"); @@ -258,7 +160,7 @@ class MailerDaemon common_log($level, 'MailDaemon: '.$msg); } - function add_notice($user, $msg, $fileRecords) + function add_notice($user, $msg, $mediafiles) { try { $notice = Notice::saveNew($user->id, $msg, 'mail'); @@ -266,8 +168,8 @@ class MailerDaemon $this->log(LOG_ERR, $e->getMessage()); return $e->getMessage(); } - foreach($fileRecords as $fileRecord){ - $this->attachFile($notice, $fileRecord); + foreach($mediafiles as $mf){ + $mf->attachToNotice($notice); } common_broadcast_notice($notice); $this->log(LOG_INFO, @@ -275,14 +177,6 @@ class MailerDaemon return true; } - function attachFile($notice, $filerec) - { - File_to_post::processNew($filerec->id, $notice->id); - - $this->maybeAddRedir($filerec->id, - common_local_url('file', array('notice' => $notice->id))); - } - function parse_message($fname) { $contents = file_get_contents($fname); -- cgit v1.2.3-54-g00ecf From e5a2f895a074a6eaaf8184f101503b1520ed780b Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 28 Oct 2009 17:12:22 -0700 Subject: Rearanged a couple things & removed debugging statements --- actions/apistatusesupdate.php | 46 +++++++++++++++------------------- actions/newnotice.php | 26 ++++++++------------ lib/mediafile.php | 57 ++++++------------------------------------- 3 files changed, 36 insertions(+), 93 deletions(-) (limited to 'lib/mediafile.php') diff --git a/actions/apistatusesupdate.php b/actions/apistatusesupdate.php index b7128d084..3a030f0fe 100644 --- a/actions/apistatusesupdate.php +++ b/actions/apistatusesupdate.php @@ -112,10 +112,12 @@ class ApiStatusesUpdateAction extends ApiAuthAction 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'])); + if (empty($this->status)) { + $this->clientError( + 'Client must provide a \'status\' parameter with a value.', + 400, + $this->format + ); return; } @@ -124,12 +126,10 @@ class ApiStatusesUpdateAction extends ApiAuthAction return; } - if (empty($this->status)) { - $this->clientError( - 'Client must provide a \'status\' parameter with a value.', - 400, - $this->format - ); + // Workaround for PHP returning empty $_FILES when POST length > PHP settings + + if (empty($_POST) && ($_SERVER['CONTENT_LENGTH'] > 0)) { + $this->clientError(_('Unable to handle that much POST data!')); return; } @@ -192,27 +192,19 @@ 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); + $status_shortened .= ' ' . $upload->shortUrl(); - 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'); - } + if (Notice::contentTooLong($status_shortened)) { + $upload->delete(); + $msg = _( + 'Max notice size is %d chars, ' . + 'including attachment URL.' + ); + $this->clientError(sprintf($msg, Notice::maxContent())); + } } $this->notice = Notice::saveNew( diff --git a/actions/newnotice.php b/actions/newnotice.php index 5100e79e1..59fb9f461 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -164,27 +164,21 @@ class NewnoticeAction extends Action } $upload = null; - - common_debug('looking for attachment'); - $upload = MediaFile::fromUpload('attach'); - common_debug("uploaded file = " . var_export($upload, true)); - if (isset($upload)) { - common_debug('newNotice: found an upload'); - $content_shortened .= ' ' . $upload->shortUrl(); + $content_shortened .= ' ' . $upload->shortUrl(); - common_debug('content w/upload = ' . $content_shortened); - - if (Notice::contentTooLong($content_shortened)) { - $upload->delete(); - $this->clientError(sprintf(_('Max notice size is %d chars, including attachment URL.'), - Notice::maxContent())); - } else { - common_debug('content not too long'); - } + if (Notice::contentTooLong($content_shortened)) { + $upload->delete(); + $this->clientError( + sprintf( + _('Max notice size is %d chars, including attachment URL.'), + Notice::maxContent() + ) + ); + } } $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, diff --git a/lib/mediafile.php b/lib/mediafile.php index 4aa8211c3..40f37ba61 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -47,39 +47,21 @@ class MediaFile $this->user = common_current_user(); } - common_debug('in MediaFile constructor'); - - $this->filename = $filename; - $this->mimetype = $mimetype; - - common_debug('storing file'); + $this->filename = $filename; + $this->mimetype = $mimetype; $this->fileRecord = $this->storeFile(); - common_debug('finished storing file'); $this->fileurl = common_local_url('attachment', array('attachment' => $this->fileRecord->id)); - common_debug('$this->fileurl() = ' . $this->fileurl); - - // not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->fileurl); - - common_debug('shortening file url'); $this->short_fileurl = common_shorten_url($this->fileurl); - common_debug('shortened file url = ' . $short_fileurl); - - // Also, not sure this is necessary -- Zach $this->maybeAddRedir($this->fileRecord->id, $this->short_fileurl); - - common_debug("MediaFile: end of constructor"); } function attachToNotice($notice) { - common_debug('MediaFile::attachToNotice() -- doing File_to_post'); File_to_post::processNew($this->fileRecord->id, $notice->id); - common_debug('MediaFile done doing File_to_post'); - $this->maybeAddRedir($this->fileRecord->id, common_local_url('file', array('notice' => $notice->id))); } @@ -98,31 +80,21 @@ class MediaFile function storeFile() { $file = new File; - $file->filename = $this->filename; - - common_debug('storing ' . $this->filename); - - $file->url = File::url($this->filename); - common_debug('file->url = ' . $file->url); - - $filepath = File::path($this->filename); - common_debug('filepath = ' . $filepath); - $file->size = filesize($filepath); - $file->date = time(); + $file->filename = $this->filename; + $file->url = File::url($this->filename); + $filepath = File::path($this->filename); + $file->size = filesize($filepath); + $file->date = time(); $file->mimetype = $this->mimetype; $file_id = $file->insert(); if (!$file_id) { - - common_debug("storeFile: problem inserting new file"); common_log_db_error($file, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - common_debug('finished storing file'); - return $file; } @@ -133,15 +105,10 @@ class MediaFile function maybeAddRedir($file_id, $url) { - - common_debug("maybeAddRedir: looking up url: $url for file id $file_id"); - $file_redir = File_redirection::staticGet('url', $url); if (empty($file_redir)) { - common_debug("maybeAddRedir: $url is not in the db"); - $file_redir = new File_redirection; $file_redir->url = $url; $file_redir->file_id = $file_id; @@ -152,22 +119,16 @@ class MediaFile common_log_db_error($file_redir, "INSERT", __FILE__); throw new ClientException(_('There was a database error while saving your file. Please try again.')); } - } else { - - common_debug("maybeAddRedir: no need to add $url, it's already in the db"); } } static function fromUpload($param = 'media', $user = null) { - common_debug("fromUpload: param = $param"); - if (empty($user)) { $user = common_current_user(); } if (!isset($_FILES[$param]['error'])){ - common_debug('no file found'); return; } @@ -221,8 +182,6 @@ class MediaFile $filename = File::filename($user->getProfile(), $basename, $mimetype); $filepath = File::path($filename); - common_debug("filepath = " . $filepath); - $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath); if (!$result) { @@ -277,8 +236,6 @@ class MediaFile static function getUploadedFileType($f) { require_once 'MIME/Type.php'; - common_debug("in getUploadedFileType"); - $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); $cmd = common_config('attachments', 'filecommand'); -- cgit v1.2.3-54-g00ecf From e0dbc47f8e9ef86dbb652d816173e1bfc8188807 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 28 Oct 2009 17:47:14 -0700 Subject: Fixed header comment. --- lib/mediafile.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/mediafile.php') diff --git a/lib/mediafile.php b/lib/mediafile.php index 40f37ba61..d4d184dd0 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -1,8 +1,10 @@ * @author Zach Copley * @copyright 2008-2009 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 -- cgit v1.2.3-54-g00ecf From 73b9e531bf7cab8ba6642f9cb85e4b37b48706d7 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 2 Nov 2009 09:08:26 -0800 Subject: Detect "no file" upload error and, for now at least, ignore it gracefully. This was triggering on non-AJAX form submissions for new notices, preventing them from being posted when they contained no attachments. --- lib/imagefile.php | 5 +++++ lib/mediafile.php | 5 +++++ 2 files changed, 10 insertions(+) (limited to 'lib/mediafile.php') diff --git a/lib/imagefile.php b/lib/imagefile.php index 88f461481..cd2f87e6b 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -79,7 +79,12 @@ class ImageFile @unlink($_FILES[$param]['tmp_name']); throw new Exception(_('Partial upload.')); return; + case UPLOAD_ERR_NO_FILE: + // No file; probably just a non-AJAX submission. + return; default: + common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . + $_FILES[$param]['error']); throw new Exception(_('System error uploading file.')); return; } diff --git a/lib/mediafile.php b/lib/mediafile.php index d4d184dd0..29d752f0c 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -152,6 +152,9 @@ class MediaFile 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: throw new ClientException(_('Missing a temporary folder.')); return; @@ -162,6 +165,8 @@ class MediaFile throw new ClientException(_('File upload stopped by extension.')); return; default: + common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . + $_FILES[$param]['error']); throw new ClientException(_('System error uploading file.')); return; } -- cgit v1.2.3-54-g00ecf