Use GNU social upload API instead of selfmade for images etc.

This commit is contained in:
Mikael Nordfeldth 2015-01-23 02:08:21 +01:00
parent 28c146d448
commit 22e4c00e23
3 changed files with 22 additions and 125 deletions

View File

@ -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',

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

@ -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');
@ -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();