This commit is contained in:
Hannes Mannerheim 2015-01-23 13:20:30 +01:00
parent c8373ed839
commit 50aab959ad
3 changed files with 125 additions and 22 deletions

View File

@ -126,6 +126,8 @@ class QvitterPlugin extends Plugin {
array('action' => 'ApiUpdateBackgroundImage')); array('action' => 'ApiUpdateBackgroundImage'));
$m->connect('api/qvitter/update_avatar.json', $m->connect('api/qvitter/update_avatar.json',
array('action' => 'ApiUpdateAvatar')); array('action' => 'ApiUpdateAvatar'));
$m->connect('api/qvitter/upload_image.json',
array('action' => 'ApiUploadImage'));
$m->connect('api/qvitter/external_user_show.json', $m->connect('api/qvitter/external_user_show.json',
array('action' => 'ApiExternalUserShow')); array('action' => 'ApiExternalUserShow'));
$m->connect('api/qvitter/toggle_qvitter.json', $m->connect('api/qvitter/toggle_qvitter.json',

109
actions/apiuploadimage.php Normal file
View File

@ -0,0 +1,109 @@
<?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

@ -2446,6 +2446,7 @@ $('body').on('click','.upload-image',function () {
}); });
function uploadImage(e, thisUploadButton) { function uploadImage(e, thisUploadButton) {
// get orientation // get orientation
loadImage.parseMetaData(e.target.files[0], function (data) { loadImage.parseMetaData(e.target.files[0], function (data) {
if (data.exif) { if (data.exif) {
@ -2463,33 +2464,24 @@ function uploadImage(e, thisUploadButton) {
// clean up // clean up
cleanUpAfterCropping(); cleanUpAfterCropping();
// create image // create image
loadImage(e.target.files[0], loadImage(e.target.files[0],
function (img) { function (img) {
if(typeof img.target == 'undefined') { 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 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 // upload
$.ajax({ url: window.apiRoot + 'statusnet/media/upload', $.ajax({ url: window.apiRoot + 'qvitter/upload_image.json',
type: "POST", type: "POST",
data: imgFormData, data: {
contentType: false, img: appendedImg.siblings('.upload-image-container').children('img.to-upload').attr('src')
processData: false, },
dataType: "xml", dataType:"json",
error: function(data){ console.log('error'); console.log(data); $('.queet-box-loading-cover').remove(); }, error: function(data){ console.log('error'); console.log(data); $('.queet-box-loading-cover').remove(); },
success: function(data) { success: function(data) {
var rsp = $(data).find('rsp'); if(typeof data.error == 'undefined') {
if (rsp.attr('stat') == 'ok') {
cleanUpAfterCropping(); 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 uploadButton = $('img.to-upload').parent().siblings('.queet-toolbar').find('.upload-image');
var queetBox = $('img.to-upload').parent().siblings('.queet-box-syntax'); var queetBox = $('img.to-upload').parent().siblings('.queet-box-syntax');
var caretPos = uploadButton.attr('data-caret-pos').split(','); var caretPos = uploadButton.attr('data-caret-pos').split(',');
@ -2497,10 +2489,10 @@ function uploadImage(e, thisUploadButton) {
// if this site is like quitter.se, we have to do this, otherwise // 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 // gnusocial will not recognize the link to the image as a local attachment
if(window.thisSiteThinksItIsHttpButIsActuallyHttps) { if(window.thisSiteThinksItIsHttpButIsActuallyHttps) {
mediaurl = mediaurl.replace('https://','http://'); data.shorturl = data.shorturl.replace('https://','http://');
} }
$('img.to-upload').attr('data-shorturl', mediaurl); $('img.to-upload').attr('data-shorturl', data.shorturl);
$('img.to-upload').addClass('uploaded'); $('img.to-upload').addClass('uploaded');
$('img.to-upload').removeClass('to-upload'); $('img.to-upload').removeClass('to-upload');
@ -2513,18 +2505,18 @@ function uploadImage(e, thisUploadButton) {
queetBox.html(' '); queetBox.html(' ');
range = createRangeFromCharacterIndices(queetBox[0], caretPos[0], caretPos[0]); range = createRangeFromCharacterIndices(queetBox[0], caretPos[0], caretPos[0]);
} }
range.insertNode(document.createTextNode(' ' + mediaurl + ' ')); range.insertNode(document.createTextNode(' ' + data.shorturl + ' '));
// put caret after // put caret after
queetBox.focus(); queetBox.focus();
var putCaretAt = parseInt(caretPos[0],10)+mediaurl.length+2; var putCaretAt = parseInt(caretPos[0],10)+data.shorturl.length+2;
setSelectionRange(queetBox[0], putCaretAt, putCaretAt); setSelectionRange(queetBox[0], putCaretAt, putCaretAt);
queetBox.trigger('input'); // avoid some flickering queetBox.trigger('input'); // avoid some flickering
setTimeout(function(){ queetBox.trigger('input');},1); // make sure chars are counted and shorten-button activated setTimeout(function(){ queetBox.trigger('input');},1); // make sure chars are counted and shorten-button activated
$('.queet-box-loading-cover').remove(); $('.queet-box-loading-cover').remove();
} }
else { else {
alert('Try again! ' + rsp.find('err').attr('msg')); alert('Try again! ' + data.error);
$('.save-profile-button').removeAttr('disabled'); $('.save-profile-button').removeAttr('disabled');
$('.save-profile-button').removeClass('disabled'); $('.save-profile-button').removeClass('disabled');
$('img.to-upload').parent().remove(); $('img.to-upload').parent().remove();