From 22e4c00e23bd56c3a52b86111f0c57a8845575c6 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Fri, 23 Jan 2015 02:08:21 +0100 Subject: [PATCH] Use GNU social upload API instead of selfmade for images etc. --- QvitterPlugin.php | 2 - actions/apiuploadimage.php | 109 ------------------------------------- js/qvitter.js | 36 +++++++----- 3 files changed, 22 insertions(+), 125 deletions(-) delete mode 100644 actions/apiuploadimage.php diff --git a/QvitterPlugin.php b/QvitterPlugin.php index a61ee6f..efc73d4 100644 --- a/QvitterPlugin.php +++ b/QvitterPlugin.php @@ -126,8 +126,6 @@ class QvitterPlugin extends Plugin { array('action' => 'ApiUpdateBackgroundImage')); $m->connect('api/qvitter/update_avatar.json', array('action' => 'ApiUpdateAvatar')); - $m->connect('api/qvitter/upload_image.json', - array('action' => 'ApiUploadImage')); $m->connect('api/qvitter/external_user_show.json', array('action' => 'ApiExternalUserShow')); $m->connect('api/qvitter/toggle_qvitter.json', diff --git a/actions/apiuploadimage.php b/actions/apiuploadimage.php deleted file mode 100644 index d1935a2..0000000 --- a/actions/apiuploadimage.php +++ /dev/null @@ -1,109 +0,0 @@ - \\\\_\ · - · \\) \____) · - · · - · · - · 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 . · - · · - · 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'); - } -} diff --git a/js/qvitter.js b/js/qvitter.js index d052b01..14a193d 100644 --- a/js/qvitter.js +++ b/js/qvitter.js @@ -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(''); + 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'); @@ -2505,18 +2513,18 @@ function uploadImage(e, thisUploadButton) { queetBox.html(' '); 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();