append uploads to content rather than showing them double

This commit is contained in:
Evan Prodromou 2009-06-23 07:29:43 -07:00
parent e2becdb251
commit a21a9f26c5
5 changed files with 190 additions and 128 deletions

View File

@ -224,16 +224,40 @@ class NewnoticeAction extends Action
} }
} }
if (isset($mimetype)) {
$filename = $this->saveFile($mimetype);
if (empty($filename)) {
$this->clientError(_('Couldn\'t save file.'));
}
$fileurl = File::url($filename);
$short_fileurl = common_shorten_url($fileurl);
$content_shortened .= ' ' . $short_fileurl;
if (mb_strlen($content_shortened) > 140) {
$this->deleteFile($filename);
$this->clientError(_('Max notice size is 140 chars, including attachment URL.'));
}
}
common_debug("newnotice.php - before Notice::saveNew()");
$notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1,
($replyto == 'false') ? null : $replyto); ($replyto == 'false') ? null : $replyto);
common_debug("newnotice.php - after Notice::saveNew()");
if (is_string($notice)) { if (is_string($notice)) {
if (isset($filename)) {
$this->deleteFile($filename);
}
$this->clientError($notice); $this->clientError($notice);
} }
common_debug("newnotice.php - after Notice::saveNew()");
if (isset($mimetype)) { if (isset($mimetype)) {
$this->storeFile($notice, $mimetype); $this->attachFile($notice, $filename, $mimetype, $short_fileurl);
} }
common_broadcast_notice($notice); common_broadcast_notice($notice);
if ($this->boolean('ajax')) { if ($this->boolean('ajax')) {
@ -259,7 +283,13 @@ class NewnoticeAction extends Action
} }
} }
function storeFile($notice, $mimetype) { function saveFile($mimetype) {
$cur = common_current_user();
if (empty($cur)) {
$this->serverError(_('Somehow lost the login in saveFile'));
}
common_debug("NewnoticeAction::storeFile()"); common_debug("NewnoticeAction::storeFile()");
@ -267,7 +297,7 @@ class NewnoticeAction extends Action
common_debug("Basename: $basename"); common_debug("Basename: $basename");
$filename = File::filename($notice->id, $basename); $filename = File::filename($cur->getProfile(), $basename, $mimetype);
common_debug("filename: $filename"); common_debug("filename: $filename");
@ -276,7 +306,20 @@ class NewnoticeAction extends Action
common_debug("filepath: $filepath"); common_debug("filepath: $filepath");
if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) { if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) {
return $filename;
} else {
$this->clientError(_('File could not be moved to destination directory.'));
}
}
function deleteFile($filename)
{
$filepath = File::path($filename);
@unlink($filepath);
}
function attachFile($notice, $filename, $mimetype, $short)
{
$file = new File; $file = new File;
$file->filename = $filename; $file->filename = $filename;
@ -284,26 +327,39 @@ class NewnoticeAction extends Action
common_debug("file->url =". $file->url); common_debug("file->url =". $file->url);
$filepath = File::path($filename);
$file->size = filesize($filepath); $file->size = filesize($filepath);
$file->date = time(); $file->date = time();
$file->mimetype = $mimetype; $file->mimetype = $mimetype;
if ($file_id = $file->insert()) { $file_id = $file->insert();
if (!$file_id) {
common_log_db_error($file, "INSERT", __FILE__);
$this->clientError(_('There was a database error while saving your file. Please try again.'));
}
$file_redir = new File_redirection; $file_redir = new File_redirection;
$file_redir->url = File::url($filename); $file_redir->url = File::url($filename);
$file_redir->file_id = $file_id; $file_redir->file_id = $file_id;
$file_redir->insert();
$result = $file_redir->insert();
if (!$result) {
common_log_db_error($file_redir, "INSERT", __FILE__);
$this->clientError(_('There was a database error while saving your file. Please try again.'));
}
$f2p = new File_to_post; $f2p = new File_to_post;
$f2p->file_id = $file_id; $f2p->file_id = $file_id;
$f2p->post_id = $notice->id; $f2p->post_id = $notice->id;
$f2p->insert(); $f2p->insert();
} else {
if (!$result) {
common_log_db_error($f2p, "INSERT", __FILE__);
$this->clientError(_('There was a database error while saving your file. Please try again.')); $this->clientError(_('There was a database error while saving your file. Please try again.'));
} }
} else {
$this->clientError(_('File could not be moved to destination directory.'));
}
} }
/** /**

View File

@ -148,9 +148,15 @@ class File extends Memcached_DataObject
// where should the file go? // where should the file go?
static function filename($notice_id, $basename) static function filename($profile, $basename, $mimetype)
{ {
return $notice_id . '-' . $basename; require_once 'MIME/Type/Extension.php';
$mte = new MIME_Type_Extension();
$ext = $mte->getExtension($mimetype);
$nickname = $profile->nickname;
$datestamp = strftime('%Y%m%dT%H%M%S', time());
$random = strtolower(common_confirmation_code(32));
return "$nickname-$datestamp-$random.$ext";
} }
static function path($filename) static function path($filename)
@ -186,6 +192,5 @@ class File extends Memcached_DataObject
return 'http://'.$server.$path.$filename; return 'http://'.$server.$path.$filename;
} }
} }

View File

@ -25,7 +25,6 @@ require_once INSTALLDIR.'/classes/File_oembed.php';
define('USER_AGENT', 'Laconica user agent / file probe'); define('USER_AGENT', 'Laconica user agent / file probe');
/** /**
* Table Definition for file_redirection * Table Definition for file_redirection
*/ */
@ -48,8 +47,6 @@ class File_redirection extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
function _commonCurl($url, $redirs) { function _commonCurl($url, $redirs) {
$curlh = curl_init(); $curlh = curl_init();
curl_setopt($curlh, CURLOPT_URL, $url); curl_setopt($curlh, CURLOPT_URL, $url);
@ -86,8 +83,6 @@ class File_redirection extends Memcached_DataObject
return $url; return $url;
} }
$curlh = File_redirection::_commonCurl($short_url, $redirs); $curlh = File_redirection::_commonCurl($short_url, $redirs);
// Don't include body in output // Don't include body in output
curl_setopt($curlh, CURLOPT_NOBODY, true); curl_setopt($curlh, CURLOPT_NOBODY, true);
@ -143,62 +138,7 @@ class File_redirection extends Memcached_DataObject
} }
function _userMakeShort($long_url, $user) { function _userMakeShort($long_url, $user) {
if (empty($user)) { $short_url = common_shorten_url($long_url);
// common current user does not find a user when called from the XMPP daemon
// therefore we'll set one here fix, so that XMPP given URLs may be shortened
$user->urlshorteningservice = 'ur1.ca';
}
$curlh = curl_init();
curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica');
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
switch($user->urlshorteningservice) {
case 'ur1.ca':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new LilUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case '2tu.us':
$short_url_service = new TightUrl;
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url = $short_url_service->shorten($long_url);
break;
case 'ptiturl.com':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new PtitUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case 'bit.ly':
curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url));
$short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
break;
case 'is.gd':
curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'snipr.com':
curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'metamark.net':
curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'tinyurl.com':
curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
default:
$short_url = false;
}
curl_close($curlh);
if ($short_url) { if ($short_url) {
$short_url = (string)$short_url; $short_url = (string)$short_url;
// store it // store it

View File

@ -336,10 +336,6 @@ class NoticeListItem extends Widget
// versions (>> 0.4.x) // versions (>> 0.4.x)
$this->out->raw(common_render_content($this->notice->content, $this->notice)); $this->out->raw(common_render_content($this->notice->content, $this->notice));
} }
$uploaded = $this->notice->getUploadedAttachment();
if ($uploaded) {
$this->out->element('a', array('href' => $uploaded[0], 'class' => 'attachment', 'id' => 'attachment-' . $uploaded[1]), $uploaded[0]);
}
$this->out->elementEnd('p'); $this->out->elementEnd('p');
} }

View File

@ -1377,3 +1377,68 @@ function common_database_tablename($tablename)
//table prefixes could be added here later //table prefixes could be added here later
return $tablename; return $tablename;
} }
function common_shorten_url($long_url)
{
$user = common_current_user();
if (empty($user)) {
// common current user does not find a user when called from the XMPP daemon
// therefore we'll set one here fix, so that XMPP given URLs may be shortened
$svc = 'ur1.ca';
} else {
$svc = $user->urlshorteningservice;
}
$curlh = curl_init();
curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica');
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
switch($svc) {
case 'ur1.ca':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new LilUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case '2tu.us':
$short_url_service = new TightUrl;
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url = $short_url_service->shorten($long_url);
break;
case 'ptiturl.com':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new PtitUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case 'bit.ly':
curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url));
$short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
break;
case 'is.gd':
curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'snipr.com':
curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'metamark.net':
curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'tinyurl.com':
curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
default:
$short_url = false;
}
curl_close($curlh);
return $short_url;
}