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,36 +306,62 @@ 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;
$file = new File;
$file->filename = $filename;
$file->url = common_local_url('file', array('notice' => $notice->id));
common_debug("file->url =". $file->url);
$file->size = filesize($filepath);
$file->date = time();
$file->mimetype = $mimetype;
if ($file_id = $file->insert()) {
$file_redir = new File_redirection;
$file_redir->url = File::url($filename);
$file_redir->file_id = $file_id;
$file_redir->insert();
$f2p = new File_to_post;
$f2p->file_id = $file_id;
$f2p->post_id = $notice->id;
$f2p->insert();
} else {
$this->clientError(_('There was a database error while saving your file. Please try again.'));
}
} else { } else {
$this->clientError(_('File could not be moved to destination directory.')); $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->filename = $filename;
$file->url = common_local_url('file', array('notice' => $notice->id));
common_debug("file->url =". $file->url);
$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->clientError(_('There was a database error while saving your file. Please try again.'));
}
$file_redir = new File_redirection;
$file_redir->url = File::url($filename);
$file_redir->file_id = $file_id;
$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->file_id = $file_id;
$f2p->post_id = $notice->id;
$f2p->insert();
if (!$result) {
common_log_db_error($f2p, "INSERT", __FILE__);
$this->clientError(_('There was a database error while saving your file. Please try again.'));
}
}
/** /**
* Show an Ajax-y error message * Show an Ajax-y error message
* *

View File

@ -124,8 +124,8 @@ class File extends Memcached_DataObject
function isRespectsQuota($user) { function isRespectsQuota($user) {
if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) { if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) {
return sprintf(_('No file may be larger than %d bytes ' . return sprintf(_('No file may be larger than %d bytes ' .
'and the file you sent was %d bytes. Try to upload a smaller version.'), 'and the file you sent was %d bytes. Try to upload a smaller version.'),
common_config('attachments', 'file_quota'), $_FILES['attach']['size']); common_config('attachments', 'file_quota'), $_FILES['attach']['size']);
} }
$query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'"; $query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$user->id} and file.url like '%/notice/%/file'";
@ -148,44 +148,49 @@ 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)
{ {
$dir = common_config('attachments', 'dir'); $dir = common_config('attachments', 'dir');
if ($dir[strlen($dir)-1] != '/') { if ($dir[strlen($dir)-1] != '/') {
$dir .= '/'; $dir .= '/';
} }
return $dir . $filename; return $dir . $filename;
} }
static function url($filename) static function url($filename)
{ {
$path = common_config('attachments', 'path'); $path = common_config('attachments', 'path');
if ($path[strlen($path)-1] != '/') { if ($path[strlen($path)-1] != '/') {
$path .= '/'; $path .= '/';
} }
if ($path[0] != '/') { if ($path[0] != '/') {
$path = '/'.$path; $path = '/'.$path;
} }
$server = common_config('attachments', 'server'); $server = common_config('attachments', 'server');
if (empty($server)) { if (empty($server)) {
$server = common_config('site', 'server'); $server = common_config('site', 'server');
} }
// XXX: protocol // XXX: protocol
return 'http://'.$server.$path.$filename;
}
return 'http://'.$server.$path.$filename;
}
} }

View File

@ -25,21 +25,20 @@ 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
*/ */
class File_redirection extends Memcached_DataObject class File_redirection extends Memcached_DataObject
{ {
###START_AUTOCODE ###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */ /* the code below is auto generated do not remove the above tag */
public $__table = 'file_redirection'; // table name public $__table = 'file_redirection'; // table name
public $url; // varchar(255) primary_key not_null public $url; // varchar(255) primary_key not_null
public $file_id; // int(4) public $file_id; // int(4)
public $redirections; // int(4) public $redirections; // int(4)
public $httpcode; // int(4) public $httpcode; // int(4)
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */ /* Static get */
@ -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;
}