#89 extended to work with rotation and thumbnails

This commit is contained in:
Hannes Mannerheim 2015-01-23 16:28:41 +01:00
parent 50aab959ad
commit dab96e99a9
6 changed files with 67 additions and 140 deletions

View File

@ -476,14 +476,14 @@ class QvitterPlugin extends Plugin {
// find all thumbs
$attachments = $notice->attachments();
$attachment_url_to_thumb = array();
$attachment_url_to_id = array();
if (!empty($attachments)) {
foreach ($attachments as $attachment) {
try {
$enclosure_o = $attachment->getEnclosure();
$thumb = File_thumbnail::getKV('file_id', $attachment->id);
if(isset($thumb->url)) {
$attachment_url_to_thumb[$enclosure_o->url] = $thumb->url;
$attachment_url_to_id[$enclosure_o->url] = $attachment->id;
}
} catch (ServerException $e) {
// There was not enough metadata available
@ -494,8 +494,8 @@ class QvitterPlugin extends Plugin {
// add thumbs to $twitter_status
if (!empty($twitter_status['attachments'])) {
foreach ($twitter_status['attachments'] as &$attachment) {
if (!empty($attachment_url_to_thumb[$attachment['url']])) {
$attachment['thumb_url'] = $attachment_url_to_thumb[$attachment['url']];
if (!empty($attachment_url_to_id[$attachment['url']])) {
$attachment['id'] = $attachment_url_to_id[$attachment['url']];
}
}
}

View File

@ -32,11 +32,13 @@ are running the latest GNU Social nightly!)
4. Add `addPlugin('Qvitter');` to your /config.php file.
5. There are a few settings in /plugins/Qvitter/QvitterPlugin.php. By default Qvitter is
5. It's recommended to set this setting in your /config.php file: `$config['thumbnail']['maxsize'] = 3000;`
6. There are a few settings in /plugins/Qvitter/QvitterPlugin.php. By default Qvitter is
opt-out for users. If you set `$settings['enabledbydefault'] = false;` Qvitter will
be opt-in instead.
6. Users can go to ://{instance}/settings/qvitter and enable or disable Qvitter.
7. Users can go to ://{instance}/settings/qvitter and enable or disable Qvitter.
Optional
-----
@ -52,7 +54,8 @@ $config['site']['qvitter']['defaultlinkcolor'] = '#0084B4';
$config['site']['qvitter']['timebetweenpolling'] = 5000;
$config['site']['qvitter']['urlshortenerapiurl'] = 'http://qttr.at/yourls-api.php';
$config['site']['qvitter']['urlshortenersignature'] = 'b6afeec983';
$config['site']['qvitter']['sitebackground'] = 'img/vagnsmossen.jpg';
$config['site']['qvitter']['sitebackground'] = 'img/vagnsmossen.jpg';
$config['thumbnail']['maxsize'] = 3000;
````
Note: Qvitter is tested with GNU Social version 1.1.1-alpha2 (7e47026085fa4f2071e694d9c3e3fe2aa5142135).

View File

@ -1,109 +0,0 @@
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· ·
· Upload image ·
· ·
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· ·
· ·
· Q V I T T E R ·
· ·
· http://github.com/hannesmannerheim/qvitter ·
· ·
· ·
· ·
· <o) ·
· /_//// ·
· (____/ ·
· (o< ·
· o> \\\\_\ ·
· \\) \____) ·
· ·
· ·
· Qvitter is free software: you can redistribute it and / or modify it ·
· under the terms of the GNU Affero General Public License as published by ·
· the Free Software Foundation, either version three of the License or (at ·
· your option) any later version. ·
· ·
· Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
· WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
· FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
· more details. ·
· ·
· You should have received a copy of the GNU Affero General Public License ·
· along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
· ·
· Contact h@nnesmannerhe.im if you have any questions. ·
· ·
· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
if (!defined('GNUSOCIAL')) {
exit(1);
}
class ApiUploadImageAction extends ApiAuthAction
{
protected $needPost = true;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*/
protected function prepare(array $args=array())
{
parent::prepare($args);
$this->user = $this->auth_user;
$this->img = $this->trimmed('img');
return true;
}
/**
* Handle the request
*
* @return void
*/
protected function handle()
{
parent::handle();
$profile = $this->user->getProfile();
$base64img = $this->img;
if(stristr($base64img, 'image/jpeg')) {
$base64img_mime = 'image/jpeg';
}
elseif(stristr($base64img, 'image/png')) {
// should convert to jpg here!!
$base64img_mime = 'image/png';
}
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
$base64img = str_replace('data:image/png;base64,', '', $base64img);
$base64img = str_replace(' ', '+', $base64img);
$base64img_hash = md5($base64img);
$base64img = base64_decode($base64img);
$base64img_basename = basename('qvitterupload');
$base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime);
$base64img_path = File::path($base64img_filename);
$base64img_success = file_put_contents($base64img_path, $base64img);
$base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename);
$mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype);
$return['shorturl'] = $mediafile->shortUrl();
// create thumb
$file = File::getKV('filename',$base64img_filename);
$file->getThumbnail();
if(strlen($return['shorturl']) < 1) {
$return['error'] = true;
}
$this->initDocument('json');
$this->showJsonObjects($return);
$this->endDocument('json');
}
}

View File

@ -79,6 +79,7 @@ class QvitterAction extends ApiAction
$siterootdomain = common_config('site','server');
$qvitterpath = Plugin::staticPath('Qvitter', '');
$apiroot = common_path('api/', StatusNet::isHTTPS());
$attachmentroot = common_path('attachment/', StatusNet::isHTTPS());
$instanceurl = common_path('', StatusNet::isHTTPS());
common_set_returnto(''); // forget this
@ -184,7 +185,10 @@ class QvitterAction extends ApiAction
window.siteBackground = '<?php print QvitterPlugin::settings("sitebackground"); ?>';
window.urlShortenerAPIURL = '<?php print QvitterPlugin::settings("urlshortenerapiurl"); ?>';
window.urlShortenerSignature = '<?php print QvitterPlugin::settings("urlshortenersignature"); ?>';
window.commonSessionToken = '<?php print common_session_token(); ?>';
window.commonSessionToken = '<?php print common_session_token(); ?>';
window.siteMaxThumbnailSize = <?php print common_config('thumbnail', 'maxsize'); ?>;
window.siteAttachmentURLBase = '<?php print $attachmentroot; ?>';
</script>
<style>
a, a:visited, a:active,

View File

@ -948,10 +948,9 @@ function expand_queet(q,doScrolling) {
// maybe show images or videos
$.each($('#' + q.children('.queet').attr('id') + ' .queet-text, #' + q.children('.queet').attr('id') + ' > .attachments').find('a'), function() {
var attachment_title = $(this).attr('title');
console.log('attachment: ' + attachment_title);
// attachments in the .attachments container don't have a title, their full url is in the href
if(typeof attachment_title == 'undefined') {
attachment_title = $(this).attr('href');
@ -965,13 +964,27 @@ function expand_queet(q,doScrolling) {
attachment_title.substr(attachment_title.length - 4) == '.gif' ||
attachment_title.substr(attachment_title.length - 4) == '.jpg') {
if(q.children('.queet').find('.expanded-content').children('.media').children('a[href="' + attachment_title + '"]').length < 1) { // not if already showed
// local attachment with a thumbnail
if(typeof $(this).find('img').attr('data-big-thumbnail') != 'undefined') {
var attachment_src = $(this).find('img').attr('data-big-thumbnail');
}
// external "deep linked" images
else {
var attachment_src = attachment_title;
}
// don't show thumbnails for gifs
if(attachment_title.substr(attachment_title.length - 4) == '.gif') {
var attachment_src = attachment_title;
}
if(q.children('.queet').find('.expanded-content').children('.media').length > 0) {
q.children('.queet').find('.media').last().after('<div class="media"><a href="' + attachment_title + '" target="_blank"><img src="' + attachment_title + '" /></a></div>');
console.log('2' + attachment_title);
q.children('.queet').find('.media').last().after('<div class="media"><a href="' + attachment_title + '" target="_blank"><img src="' + attachment_src + '" /></a></div>');
}
else {
q.children('.queet').find('.expanded-content').prepend('<div class="media"><a href="' + attachment_title + '" target="_blank"><img src="' + attachment_title + '" /></a></div>');
console.log('1' + attachment_title);
q.children('.queet').find('.expanded-content').prepend('<div class="media"><a href="' + attachment_title + '" target="_blank"><img src="' + attachment_src + '" /></a></div>');
}
}
}
@ -1808,8 +1821,16 @@ function buildQueetHtml(obj, idInStream, extraClassesThisRun, requeeted_by) {
if(typeof obj.attachments != "undefined") {
obj.attachments.reverse(); // last on top
$.each(obj.attachments, function(){
if(this.thumb_url != null) {
attachment_html = attachment_html + '<a href="' + this.url + '"><img src="' + this.thumb_url + '"/></a>';
if(this.id != null) {
var bigThumbW = 1000;
var bigThumbH = 3000;
if(bigThumbW > window.siteMaxThumbnailSize) {
bigThumbW = window.siteMaxThumbnailSize;
}
if(bigThumbH > window.siteMaxThumbnailSize) {
bigThumbH = window.siteMaxThumbnailSize;
}
attachment_html = attachment_html + '<a href="' + this.url + '"><img data-big-thumbnail="' + window.siteAttachmentURLBase + this.id + '/thumbnail?w=' + bigThumbW + '&h=' + bigThumbH + '" src="' + window.siteAttachmentURLBase + this.id + '/thumbnail?w=200&h=200"/></a>';
}
});
}

View File

@ -2446,7 +2446,6 @@ $('body').on('click','.upload-image',function () {
});
function uploadImage(e, thisUploadButton) {
// get orientation
loadImage.parseMetaData(e.target.files[0], function (data) {
if (data.exif) {
@ -2464,24 +2463,33 @@ function uploadImage(e, thisUploadButton) {
// clean up
cleanUpAfterCropping();
// create image
loadImage(e.target.files[0],
function (img) {
if(typeof img.target == 'undefined') {
// The preview image below queet box.
var appendedImg = thisUploadButton.closest('.queet-toolbar').before('<span class="upload-image-container"><img class="to-upload" src="' + img.toDataURL('image/jpeg') + '" /></span>');
var imgFormData = new FormData();
imgFormData.append('media', $('#upload-image-input')[0].files[0]);
// upload
$.ajax({ url: window.apiRoot + 'qvitter/upload_image.json',
$.ajax({ url: window.apiRoot + 'statusnet/media/upload',
type: "POST",
data: {
img: appendedImg.siblings('.upload-image-container').children('img.to-upload').attr('src')
},
dataType:"json",
data: imgFormData,
contentType: false,
processData: false,
dataType: "xml",
error: function(data){ console.log('error'); console.log(data); $('.queet-box-loading-cover').remove(); },
success: function(data) {
if(typeof data.error == 'undefined') {
var rsp = $(data).find('rsp');
if (rsp.attr('stat') == 'ok') {
cleanUpAfterCropping();
// If doing 'multiple' input element, maybe reply with many mediaurl elements
// and then rsp.find('mediaurl').each(...)?
var mediaurl = rsp.find('mediaurl').text();
var uploadButton = $('img.to-upload').parent().siblings('.queet-toolbar').find('.upload-image');
var queetBox = $('img.to-upload').parent().siblings('.queet-box-syntax');
var caretPos = uploadButton.attr('data-caret-pos').split(',');
@ -2489,10 +2497,10 @@ function uploadImage(e, thisUploadButton) {
// if this site is like quitter.se, we have to do this, otherwise
// gnusocial will not recognize the link to the image as a local attachment
if(window.thisSiteThinksItIsHttpButIsActuallyHttps) {
data.shorturl = data.shorturl.replace('https://','http://');
mediaurl = mediaurl.replace('https://','http://');
}
$('img.to-upload').attr('data-shorturl', data.shorturl);
$('img.to-upload').attr('data-shorturl', mediaurl);
$('img.to-upload').addClass('uploaded');
$('img.to-upload').removeClass('to-upload');
@ -2502,21 +2510,21 @@ function uploadImage(e, thisUploadButton) {
if(typeof range == 'undefined') {
// if queetbox is empty no range is returned, and inserting will fail,
// so we insert a space and try to get range again...
queetBox.html(' ');
queetBox.html('&nbsp;');
range = createRangeFromCharacterIndices(queetBox[0], caretPos[0], caretPos[0]);
}
range.insertNode(document.createTextNode(' ' + data.shorturl + ' '));
range.insertNode(document.createTextNode(' ' + mediaurl + ' '));
// put caret after
queetBox.focus();
var putCaretAt = parseInt(caretPos[0],10)+data.shorturl.length+2;
var putCaretAt = parseInt(caretPos[0],10)+mediaurl.length+2;
setSelectionRange(queetBox[0], putCaretAt, putCaretAt);
queetBox.trigger('input'); // avoid some flickering
setTimeout(function(){ queetBox.trigger('input');},1); // make sure chars are counted and shorten-button activated
$('.queet-box-loading-cover').remove();
}
else {
alert('Try again! ' + data.error);
alert('Try again! ' + rsp.find('err').attr('msg'));
$('.save-profile-button').removeAttr('disabled');
$('.save-profile-button').removeClass('disabled');
$('img.to-upload').parent().remove();