Use OC_Cache and finish cropper functionality
This commit is contained in:
parent
0c708be76b
commit
c533b80682
|
@ -58,17 +58,8 @@ class OC_Core_Avatar_Controller {
|
||||||
\OC_JSON::success();
|
\OC_JSON::success();
|
||||||
} catch (\OC\NotSquareException $e) {
|
} catch (\OC\NotSquareException $e) {
|
||||||
$image = new \OC_Image($avatar);
|
$image = new \OC_Image($avatar);
|
||||||
$ext = substr($image->mimeType(), -3);
|
|
||||||
if ($ext === 'peg') {
|
|
||||||
$ext = 'jpg';
|
|
||||||
} elseif ($ext !== 'png') {
|
|
||||||
\OC_JSON::error();
|
|
||||||
}
|
|
||||||
|
|
||||||
$view = new \OC\Files\View('/'.$user);
|
\OC_Cache::set('tmpavatar', $image->data());
|
||||||
$view->unlink('tmpavatar.png');
|
|
||||||
$view->unlink('tmpavatar.jpg');
|
|
||||||
$view->file_put_contents('tmpavatar.'.$ext, $image->data());
|
|
||||||
\OC_JSON::error(array("data" => array("message" => "notsquare") ));
|
\OC_JSON::error(array("data" => array("message" => "notsquare") ));
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
||||||
|
@ -90,47 +81,35 @@ class OC_Core_Avatar_Controller {
|
||||||
public static function getTmpAvatar($args) {
|
public static function getTmpAvatar($args) {
|
||||||
// TODO deliver actual size here as well, so Jcrop can do its magic and we have the actual coordinates here again
|
// TODO deliver actual size here as well, so Jcrop can do its magic and we have the actual coordinates here again
|
||||||
// TODO or don't have a size parameter and only resize client sided (looks promising)
|
// TODO or don't have a size parameter and only resize client sided (looks promising)
|
||||||
//
|
|
||||||
// TODO move the tmpavatar to the cache instead, so it's cleaned up after some time
|
|
||||||
$user = OC_User::getUser();
|
$user = OC_User::getUser();
|
||||||
|
|
||||||
$view = new \OC\Files\View('/'.$user);
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
||||||
if ($view->file_exists('tmpavatar.png')) {
|
if ($tmpavatar === false) {
|
||||||
$ext = 'png';
|
|
||||||
} elseif ($view->file_exists('tmpavatar.jpg')) {
|
|
||||||
$ext = 'jpg';
|
|
||||||
} else {
|
|
||||||
\OC_JSON::error();
|
\OC_JSON::error();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext));
|
$image = new \OC_Image($tmpavatar);
|
||||||
$image->resize($args['size']);
|
|
||||||
$image->show();
|
$image->show();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function postCroppedAvatar($args) {
|
public static function postCroppedAvatar($args) {
|
||||||
$user = OC_User::getUser();
|
$user = OC_User::getUser();
|
||||||
$view = new \OC\Files\View('/'.$user);
|
|
||||||
$crop = $_POST['crop'];
|
$crop = $_POST['crop'];
|
||||||
|
|
||||||
if ($view->file_exists('tmpavatar.png')) {
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
||||||
$ext = 'png';
|
if ($tmpavatar === false) {
|
||||||
} elseif ($view->file_exists('tmpavatar.jpg')) {
|
|
||||||
$ext = 'jpg';
|
|
||||||
} else {
|
|
||||||
\OC_JSON::error();
|
\OC_JSON::error();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext));
|
$image = new \OC_Image($tmpavatar);
|
||||||
$image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
|
$image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
|
||||||
try {
|
try {
|
||||||
$avatar = new \OC_Avatar();
|
$avatar = new \OC_Avatar();
|
||||||
$avatar->set($user, $image->data());
|
$avatar->set($user, $image->data());
|
||||||
// Clean up
|
// Clean up
|
||||||
$view->unlink('tmpavatar.png');
|
\OC_Cache::remove('tmpavatar');
|
||||||
$view->unlink('tmpavatar.jpg');
|
|
||||||
\OC_JSON::success();
|
\OC_JSON::success();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
||||||
|
|
|
@ -68,8 +68,7 @@ $this->create('core_avatar_post', '/avatar/')
|
||||||
$this->create('core_avatar_delete', '/avatar/')
|
$this->create('core_avatar_delete', '/avatar/')
|
||||||
->delete()
|
->delete()
|
||||||
->action('OC_Core_Avatar_Controller', 'deleteAvatar');
|
->action('OC_Core_Avatar_Controller', 'deleteAvatar');
|
||||||
$this->create('core_avatar_get_tmp', '/avatartmp/{size}') //TODO better naming, so it doesn't conflict with core_avatar_get
|
$this->create('core_avatar_get_tmp', '/avatartmp/') //TODO better naming, so it doesn't conflict with core_avatar_get
|
||||||
->defaults(array('size' => 64))
|
|
||||||
->get()
|
->get()
|
||||||
->action('OC_Core_Avatar_Controller', 'getTmpAvatar');
|
->action('OC_Core_Avatar_Controller', 'getTmpAvatar');
|
||||||
$this->create('core_avatar_post_cropped', '/avatar/cropped')
|
$this->create('core_avatar_post_cropped', '/avatar/cropped')
|
||||||
|
|
|
@ -72,9 +72,11 @@ function showAvatarCropper() {
|
||||||
$(this).Jcrop({
|
$(this).Jcrop({
|
||||||
onChange: saveCoords,
|
onChange: saveCoords,
|
||||||
onSelect: saveCoords,
|
onSelect: saveCoords,
|
||||||
aspectRatio: 1
|
aspectRatio: 1,
|
||||||
|
boxHeight: 500,
|
||||||
|
boxWidth: 500
|
||||||
});
|
});
|
||||||
}).attr('src', OC.Router.generate('core_avatar_get_tmp', {size: 512}));
|
}).attr('src', OC.Router.generate('core_avatar_get_tmp'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendCropData() {
|
function sendCropData() {
|
||||||
|
@ -190,11 +192,6 @@ $(document).ready(function(){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#uploadavatar').click(function(){
|
|
||||||
alert('To be done');
|
|
||||||
updateAvatar();
|
|
||||||
});
|
|
||||||
|
|
||||||
var uploadparms = {
|
var uploadparms = {
|
||||||
done: function(e, data) {
|
done: function(e, data) {
|
||||||
avatarResponseHandler(data.result);
|
avatarResponseHandler(data.result);
|
||||||
|
|
Loading…
Reference in New Issue