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,
($replyto == 'false') ? null : $replyto);
common_debug("newnotice.php - after Notice::saveNew()");
if (is_string($notice)) {
if (isset($filename)) {
$this->deleteFile($filename);
}
$this->clientError($notice);
}
common_debug("newnotice.php - after Notice::saveNew()");
if (isset($mimetype)) {
$this->storeFile($notice, $mimetype);
$this->attachFile($notice, $filename, $mimetype, $short_fileurl);
}
common_broadcast_notice($notice);
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()");
@ -267,7 +297,7 @@ class NewnoticeAction extends Action
common_debug("Basename: $basename");
$filename = File::filename($notice->id, $basename);
$filename = File::filename($cur->getProfile(), $basename, $mimetype);
common_debug("filename: $filename");
@ -276,36 +306,62 @@ class NewnoticeAction extends Action
common_debug("filepath: $filepath");
if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) {
$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.'));
}
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->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
*

View File

@ -124,8 +124,8 @@ class File extends Memcached_DataObject
function isRespectsQuota($user) {
if ($_FILES['attach']['size'] > common_config('attachments', 'file_quota')) {
return sprintf(_('No file may be larger than %d bytes ' .
'and the file you sent was %d bytes. Try to upload a smaller version.'),
common_config('attachments', 'file_quota'), $_FILES['attach']['size']);
'and the file you sent was %d bytes. Try to upload a smaller version.'),
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'";
@ -148,44 +148,49 @@ class File extends Memcached_DataObject
// where should the file go?
static function filename($notice_id, $basename)
{
return $notice_id . '-' . $basename;
}
static function filename($profile, $basename, $mimetype)
{
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)
{
$dir = common_config('attachments', 'dir');
static function path($filename)
{
$dir = common_config('attachments', 'dir');
if ($dir[strlen($dir)-1] != '/') {
$dir .= '/';
}
if ($dir[strlen($dir)-1] != '/') {
$dir .= '/';
}
return $dir . $filename;
}
return $dir . $filename;
}
static function url($filename)
{
$path = common_config('attachments', 'path');
static function url($filename)
{
$path = common_config('attachments', 'path');
if ($path[strlen($path)-1] != '/') {
$path .= '/';
}
if ($path[strlen($path)-1] != '/') {
$path .= '/';
}
if ($path[0] != '/') {
$path = '/'.$path;
}
if ($path[0] != '/') {
$path = '/'.$path;
}
$server = common_config('attachments', 'server');
$server = common_config('attachments', 'server');
if (empty($server)) {
$server = common_config('site', 'server');
}
if (empty($server)) {
$server = common_config('site', 'server');
}
// XXX: protocol
return 'http://'.$server.$path.$filename;
}
// XXX: protocol
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');
/**
* Table Definition for file_redirection
*/
class File_redirection extends Memcached_DataObject
class File_redirection extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'file_redirection'; // table name
public $url; // varchar(255) primary_key not_null
public $file_id; // int(4)
public $redirections; // int(4)
public $httpcode; // int(4)
public $file_id; // int(4)
public $redirections; // int(4)
public $httpcode; // int(4)
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
@ -48,8 +47,6 @@ class File_redirection extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
function _commonCurl($url, $redirs) {
$curlh = curl_init();
curl_setopt($curlh, CURLOPT_URL, $url);
@ -86,8 +83,6 @@ class File_redirection extends Memcached_DataObject
return $url;
}
$curlh = File_redirection::_commonCurl($short_url, $redirs);
// Don't include body in output
curl_setopt($curlh, CURLOPT_NOBODY, true);
@ -143,62 +138,7 @@ class File_redirection extends Memcached_DataObject
}
function _userMakeShort($long_url, $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
$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);
$short_url = common_shorten_url($long_url);
if ($short_url) {
$short_url = (string)$short_url;
// store it

View File

@ -336,10 +336,6 @@ class NoticeListItem extends Widget
// versions (>> 0.4.x)
$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');
}

View File

@ -1377,3 +1377,68 @@ function common_database_tablename($tablename)
//table prefixes could be added here later
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;
}