update profile banner (twidere support)

This commit is contained in:
Hannes Mannerheim 2016-01-28 21:12:34 +01:00
parent e49964458c
commit 1b7a9c507b
5 changed files with 118 additions and 50 deletions

View File

@ -195,8 +195,8 @@ class QvitterPlugin extends Plugin {
$m->connect('api/qvitter/allfollowing/:id.json', $m->connect('api/qvitter/allfollowing/:id.json',
array('action' => 'apiqvitterallfollowing', array('action' => 'apiqvitterallfollowing',
'id' => Nickname::INPUT_FMT)); 'id' => Nickname::INPUT_FMT));
$m->connect('api/qvitter/update_cover_photo.json', $m->connect('api/account/update_profile_banner.json',
array('action' => 'ApiUpdateCoverPhoto')); array('action' => 'ApiAccountUpdateProfileBanner'));
$m->connect('api/qvitter/update_background_image.json', $m->connect('api/qvitter/update_background_image.json',
array('action' => 'ApiUpdateBackgroundImage')); array('action' => 'ApiUpdateBackgroundImage'));
$m->connect('api/qvitter/update_avatar.json', $m->connect('api/qvitter/update_avatar.json',

View File

@ -1,7 +1,7 @@
<?php <?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· · · ·
· Update the cover photo · · Update the profile banner ·
· · · ·
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
· · · ·
@ -42,7 +42,7 @@ if (!defined('GNUSOCIAL')) {
exit(1); exit(1);
} }
class ApiUpdateCoverPhotoAction extends ApiAuthAction class ApiAccountUpdateProfileBannerAction extends ApiAuthAction
{ {
protected $needPost = true; protected $needPost = true;
@ -59,11 +59,11 @@ class ApiUpdateCoverPhotoAction extends ApiAuthAction
$this->user = $this->auth_user; $this->user = $this->auth_user;
$this->cropW = $this->trimmed('cropW'); $this->cropW = $this->trimmed('width');
$this->cropH = $this->trimmed('cropH'); $this->cropH = $this->trimmed('height');
$this->cropX = $this->trimmed('cropX'); $this->cropX = $this->trimmed('offset_left');
$this->cropY = $this->trimmed('cropY'); $this->cropY = $this->trimmed('offset_top');
$this->img = $this->trimmed('img'); $this->img = $this->trimmed('banner');
return true; return true;
} }
@ -76,33 +76,75 @@ class ApiUpdateCoverPhotoAction extends ApiAuthAction
protected function handle() protected function handle()
{ {
parent::handle(); parent::handle();
$profile = $this->user->getProfile(); $profile = $this->user->getProfile();
$base64img = $this->img;
if(stristr($base64img, 'image/jpeg')) { // see if we have regular uploaded image data
$base64img_mime = 'image/jpeg'; try {
$mediafile = MediaFile::fromUpload('banner', $profile);
} catch (NoUploadedMediaException $e) {
// if not we may have base64 data
$img = $this->img;
if(stristr($img, 'image/jpeg')) {
$img_mime = 'image/jpeg';
} }
elseif(stristr($base64img, 'image/png')) { elseif(stristr($img, 'image/png')) {
// should convert to jpg here!! // should convert to jpg here!!
$base64img_mime = 'image/png'; $img_mime = 'image/png';
} }
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
$base64img = str_replace('data:image/png;base64,', '', $base64img); // i don't remember why we had to do this
$base64img = str_replace(' ', '+', $base64img); $img = str_replace('data:image/jpeg;base64,', '', $img);
$base64img_hash = md5($base64img); $img = str_replace('data:image/png;base64,', '', $img);
$base64img = base64_decode($base64img); $img = str_replace(' ', '+', $img);
$base64img_basename = basename('cover'); $img = base64_decode($img, true);
$base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime);
$base64img_path = File::path($base64img_filename); try {
$base64img_success = file_put_contents($base64img_path, $base64img); $img_filename = File::filename($profile, 'cover', $img_mime);
$base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename); $img_path = File::path($img_filename);
$mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype); $img_success = file_put_contents($img_path, $img);
$img_mimetype = MediaFile::getUploadedMimeType($img_path, $img_filename);
$mediafile = new MediaFile($profile, $img_filename, $img_mimetype);
} catch (Exception $e) {
$this->clientError($e, 400);
}
}
if(!$mediafile instanceof MediaFile) {
$this->clientError(_('Could not process image data.'), 400);
}
// maybe resize
$width = $this->cropW;
$height = $this->cropH;
$scale = 1;
if($width > 1200) {
$scale = 1200/$width;
} elseif($height > 600) {
$scale = 600/$height;
}
$width = round($width*$scale);
$height = round($height*$scale);
// crop
try {
$imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
$imagefile->resizeTo(File::path($mediafile->filename), array('width'=>$this->cropW, 'height'=>$this->cropH, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH)); $imagefile->resizeTo(File::path($mediafile->filename), array('width'=>$width, 'height'=>$height, 'x'=>$this->cropX, 'y'=>$this->cropY, 'w'=>$this->cropW, 'h'=>$this->cropH));
$result['url'] = File::url($mediafile->filename); $result['url'] = File::url($mediafile->filename);
} catch (Exception $e) {
$this->clientError(_('The image could not be resized and cropped. '.$e), 422);
}
// save in profile_prefs
try {
Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']); Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']);
} catch (ServerException $e) {
$this->clientError(_('The image could not be resized and cropped. '.$e), 422);
}
// return json
$this->initDocument('json'); $this->initDocument('json');
$this->showJsonObjects($result); $this->showJsonObjects($result);
$this->endDocument('json'); $this->endDocument('json');

View File

@ -5687,7 +5687,7 @@ body.rtl #feed-header-inner h2 {
} }
.modal-draggable { .modal-draggable {
width:95%; width:100%;
} }

View File

@ -657,6 +657,11 @@ function setNewCurrentStream(streamObject,setLocation,fallbackId,actionOnSuccess
weAreReloading = true; weAreReloading = true;
} }
// show hidden items when we reload
if(weAreReloading) {
$('#feed-body').children('.stream-item').removeClass('hidden');
}
// remember the most recent stream object // remember the most recent stream object
window.currentStreamObject = streamObject; window.currentStreamObject = streamObject;
@ -2256,8 +2261,10 @@ function buildAttachmentHTML(attachments){
&& this.oembed.type != 'photo') { && this.oembed.type != 'photo') {
var oembedImage = ''; var oembedImage = '';
// not if stripped from html it's the same as the title (wordpress does this..) // only local images
if(typeof this.thumb_url != 'undefined' && this.thumb_url !== null) { if(typeof this.thumb_url != 'undefined'
&& this.thumb_url !== null
&& isLocalURL(this.thumb_url)) {
oembedImage = '<div class="oembed-img-container" style="background-image:url(\'' + this.thumb_url + '\')"><img class="oembed-img" src="' + this.thumb_url + '" /></div>'; oembedImage = '<div class="oembed-img-container" style="background-image:url(\'' + this.thumb_url + '\')"><img class="oembed-img" src="' + this.thumb_url + '" /></div>';
} }

View File

@ -165,6 +165,15 @@ $('body').on({
tooltip_data = $(e.target).closest('.queet').find('.thumb-container[href="' + $(e.target).attr('data-tooltip') + '"]').outerHTML(); tooltip_data = $(e.target).closest('.queet').find('.thumb-container[href="' + $(e.target).attr('data-tooltip') + '"]').outerHTML();
tooltipClass = 'thumb'; tooltipClass = 'thumb';
} }
// sometimes the attachment link in the queet text does not give us any clue to
// which attachment it is referring to. but if it is the only link and there is
// exactly one attachment, we can safely assume that the link is referring to
// that attachment
else if($(e.target).closest('.queet').find('.thumb-container').length == 1
&& $(e.target).closest('.queet-text').find('a.attachment').length == 1) {
tooltip_data = $(e.target).closest('.queet').find('.thumb-container').outerHTML();
tooltipClass = 'thumb';
}
} }
else if($('#feed-body').hasClass('quotes-hidden-by-user') else if($('#feed-body').hasClass('quotes-hidden-by-user')
&& !$(e.target).is('.quote-link-container') && !$(e.target).is('.quote-link-container')
@ -2461,13 +2470,13 @@ $('body').on('click','.action-reply-container',function(){
var queetHtmlWithoutFooter = $queetHtml.html(); var queetHtmlWithoutFooter = $queetHtml.html();
popUpAction('popup-reply-' + this_stream_item_id, window.sL.replyTo + ' ' + this_stream_item.children('.queet').find('.screen-name').html(),replyFormHtml(this_stream_item,this_stream_item_id),queetHtmlWithoutFooter); popUpAction('popup-reply-' + this_stream_item_id, window.sL.replyTo + ' ' + this_stream_item.children('.queet').find('.screen-name').html(),replyFormHtml(this_stream_item,this_stream_item_id),queetHtmlWithoutFooter);
$('#popup-reply-' + this_stream_item_id).find('.modal-body').find('.queet-box').trigger('click'); // expand
// fix the width of the queet box, otherwise the syntax highlighting break // fix the width of the queet box, otherwise the syntax highlighting break
var queetBox = $('#popup-reply-' + this_stream_item_id).find('.modal-body').find('.inline-reply-queetbox'); var queetBox = $('#popup-reply-' + this_stream_item_id).find('.modal-body').find('.inline-reply-queetbox');
var queetBoxWidth = queetBox.width()-20; var queetBoxWidth = queetBox.width()-20;
queetBox.children('.queet-box-syntax, .syntax-middle, .syntax-two').width(queetBoxWidth); queetBox.children('.queet-box-syntax, .syntax-middle, .syntax-two').width(queetBoxWidth);
$('#popup-reply-' + this_stream_item_id).find('.modal-body').find('.queet-box').trigger('click'); // expand
maybePrefillQueetBoxWithCachedText(queetBox.children('.queet-box')); maybePrefillQueetBoxWithCachedText(queetBox.children('.queet-box'));
}); });
@ -3582,17 +3591,29 @@ $('body').on('click','.crop-and-save-button',function(){
// if this is the cover photo // if this is the cover photo
if($('#edit-profile-popup .jwc_frame.cover-photo-to-crop').length>0) { if($('#edit-profile-popup .jwc_frame.cover-photo-to-crop').length>0) {
$.ajax({ url: window.apiRoot + 'qvitter/update_cover_photo.json',
var coverImgFormData = new FormData();
coverImgFormData.append('banner', $('#cover-photo-input')[0].files[0]);
coverImgFormData.append('height', window.jwc.result.cropH);
coverImgFormData.append('width', window.jwc.result.cropW);
coverImgFormData.append('offset_left', window.jwc.result.cropX);
coverImgFormData.append('offset_top', window.jwc.result.cropY);
$.ajax({
url: window.apiRoot + 'account/update_profile_banner.json',
type: "POST", type: "POST",
data: { data: coverImgFormData,
cropH: window.jwc.result.cropH, processData: false,
cropW: window.jwc.result.cropW, contentType: false,
cropX: window.jwc.result.cropX, cache: false,
cropY: window.jwc.result.cropY, dataType: "json",
img: $('#cover-photo-to-crop').attr('src') error: function(data){
console.log('error saving profile banner'); console.log(data);
$('.crop-and-save-button').removeAttr('disabled');
$('.crop-and-save-button').removeClass('disabled');
cleanUpAfterCropping();
remove_spinner();
}, },
dataType:"json",
error: function(data){ console.log('error'); console.log(data); },
success: function(data) { success: function(data) {
remove_spinner(); remove_spinner();
if(typeof data.error == 'undefined') { if(typeof data.error == 'undefined') {
@ -3744,8 +3765,6 @@ function coverPhotoAndAvatarSelectAndCrop(e, coverOrAvatar) {
if(coverOrAvatar == 'upload-cover-photo') { if(coverOrAvatar == 'upload-cover-photo') {
var targetWidth = 588; var targetWidth = 588;
var targetHeight = 260; var targetHeight = 260;
var maxWidth = 1040;
var minWidth = 1040;
var cropId = 'cover-photo-to-crop'; var cropId = 'cover-photo-to-crop';
} }
else if(coverOrAvatar == 'upload-avatar') { else if(coverOrAvatar == 'upload-avatar') {