diff --git a/3rdparty b/3rdparty index 2f3ae9f56a..ea5e07f120 160000 --- a/3rdparty +++ b/3rdparty @@ -1 +1 @@ -Subproject commit 2f3ae9f56a9838b45254393e13c14f8a8c380d6b +Subproject commit ea5e07f120177092cdb11ee16d7b54fb1ff16cb3 diff --git a/core/avatar/controller.php b/core/avatar/controller.php index 17fe4270ff..c889385c21 100644 --- a/core/avatar/controller.php +++ b/core/avatar/controller.php @@ -25,7 +25,8 @@ class OC_Core_Avatar_Controller { $size = 64; } - $image = \OC_Avatar::get($user, $size); + $ava = new \OC_Avatar(); + $image = $ava->get($user, $size); if ($image instanceof \OC_Image) { $image->show(); @@ -39,7 +40,8 @@ class OC_Core_Avatar_Controller { if (isset($_POST['path'])) { $path = stripslashes($_POST['path']); - $avatar = OC::$SERVERROOT.'/data/'.$user.'/files'.$path; + $view = new \OC\Files\View('/'.$user.'/files'); + $avatar = $view->file_get_contents($path); } if (!empty($_FILES)) { @@ -51,10 +53,22 @@ class OC_Core_Avatar_Controller { } try { - \OC_Avatar::set($user, $avatar); + $ava = new \OC_Avatar(); + $ava->set($user, $avatar); \OC_JSON::success(); } catch (\OC\NotSquareException $e) { - // TODO move unfitting avatar to /datadir/$user/tmpavatar{png.jpg} here + $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); + $view->unlink('tmpavatar.png'); + $view->unlink('tmpavatar.jpg'); + $view->file_put_contents('tmpavatar.'.$ext, $image->data()); \OC_JSON::error(array("data" => array("message" => "notsquare") )); } catch (\Exception $e) { \OC_JSON::error(array("data" => array("message" => $e->getMessage()) )); @@ -65,7 +79,8 @@ class OC_Core_Avatar_Controller { $user = OC_User::getUser(); try { - \OC_Avatar::remove($user); + $avatar = new \OC_Avatar(); + $avatar->remove($user); \OC_JSON::success(); } catch (\Exception $e) { \OC_JSON::error(array("data" => array ("message" => $e->getMessage()) )); @@ -73,17 +88,52 @@ class OC_Core_Avatar_Controller { } public static function getTmpAvatar($args) { - // TODO deliver /datadir/$user/tmpavatar.{png|jpg} here, filename may include a timestamp + // 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 make a cronjob that cleans up the tmpavatar after it's older than 2 hours, should be run every hour $user = OC_User::getUser(); + + $view = new \OC\Files\View('/'.$user); + if ($view->file_exists('tmpavatar.png')) { + $ext = 'png'; + } elseif ($view->file_exists('tmpavatar.jpg')) { + $ext = 'jpg'; + } else { + \OC_JSON::error(); + return; + } + + $image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext)); + $image->resize($args['size']); + $image->show(); } public static function postCroppedAvatar($args) { $user = OC_User::getUser(); - $crop = json_decode($_POST['crop'], true); - $image = new \OC_Image($avatar); - $image->crop($x, $y, $w, $h); - $avatar = $image->data(); - $cropped = true; + $view = new \OC\Files\View('/'.$user); + $crop = $_POST['crop']; + + if ($view->file_exists('tmpavatar.png')) { + $ext = 'png'; + } elseif ($view->file_exists('tmpavatar.jpg')) { + $ext = 'jpg'; + } else { + \OC_JSON::error(); + return; + } + + $image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext)); + $image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']); + try { + $avatar = new \OC_Avatar(); + $avatar->set($user, $image->data()); + // Clean up + $view->unlink('tmpavatar.png'); + $view->unlink('tmpavatar.jpg'); + \OC_JSON::success(); + } catch (\Exception $e) { + \OC_JSON::error(array("data" => array("message" => $e->getMessage()) )); + } } } diff --git a/core/routes.php b/core/routes.php index 30c4bf544d..25f64a1883 100644 --- a/core/routes.php +++ b/core/routes.php @@ -68,7 +68,7 @@ $this->create('core_avatar_post', '/avatar/') $this->create('core_avatar_delete', '/avatar/') ->delete() ->action('OC_Core_Avatar_Controller', 'deleteAvatar'); -$this->create('core_avatar_get_tmp', '/avatar/tmp/{size}') +$this->create('core_avatar_get_tmp', '/avatartmp/{size}') //TODO better naming, so it doesn't conflict with core_avatar_get ->defaults(array('size' => 64)) ->get() ->action('OC_Core_Avatar_Controller', 'getTmpAvatar'); diff --git a/lib/avatar.php b/lib/avatar.php index 9ab905c852..3621b96e10 100644 --- a/lib/avatar.php +++ b/lib/avatar.php @@ -17,7 +17,7 @@ class OC_Avatar { * @param $size integer size in px of the avatar, defaults to 64 * @return mixed \OC_Image containing the avatar or false if there's no image */ - public static function get ($user, $size = 64) { + public function get ($user, $size = 64) { $view = new \OC\Files\View('/'.$user); if ($view->file_exists('avatar.jpg')) { @@ -42,7 +42,7 @@ class OC_Avatar { * @throws \OC\NotSquareException if the image is not square * @return true on success */ - public static function set ($user, $data) { + public function set ($user, $data) { $view = new \OC\Files\View('/'.$user); $img = new OC_Image($data); @@ -73,7 +73,7 @@ class OC_Avatar { * @param $user string user to delete the avatar from * @return void */ - public static function remove ($user) { + public function remove ($user) { $view = new \OC\Files\View('/'.$user); $view->unlink('avatar.jpg'); $view->unlink('avatar.png'); diff --git a/lib/cleanupavatarjob.php b/lib/cleanupavatarjob.php new file mode 100644 index 0000000000..16bf263d21 --- /dev/null +++ b/lib/cleanupavatarjob.php @@ -0,0 +1,13 @@ +setInterval(7200); // 2 hours + } + + public function run ($argument) { + // TODO $view + // TODO remove ALL the tmpavatars + } +} diff --git a/lib/installer.php b/lib/installer.php index 179b279c5b..a1d2173e22 100644 --- a/lib/installer.php +++ b/lib/installer.php @@ -426,7 +426,7 @@ class OC_Installer{ 'OC_API::', 'OC_App::', 'OC_AppConfig::', - 'OC_Avatar::', + 'OC_Avatar', 'OC_BackgroundJob::', 'OC_Config::', 'OC_DB::', diff --git a/lib/public/avatar.php b/lib/public/avatar.php index 55eff57d16..649f3240e9 100644 --- a/lib/public/avatar.php +++ b/lib/public/avatar.php @@ -10,6 +10,7 @@ namespace OCP; class Avatar { public static function get ($user, $size = 64) { - return \OC_Avatar::get($user, $size); + $avatar = new \OC_Avatar(); + return $avatar->get($user, $size); } } diff --git a/settings/js/personal.js b/settings/js/personal.js index e97d0d64c9..5d9219dd7e 100644 --- a/settings/js/personal.js +++ b/settings/js/personal.js @@ -74,10 +74,11 @@ function showAvatarCropper() { onSelect: saveCoords, aspectRatio: 1 }); - }).attr('src', OC.router_base_url+'/avatar/tmp/512'); + }).attr('src', OC.router_base_url+'/avatartmp/512'); } function sendCropData() { + $('#cropperbox').ocdialog('close'); var cropperdata = $('#cropper').data(); var data = { x: cropperdata.x, @@ -85,7 +86,7 @@ function sendCropData() { w: cropperdata.w, h: cropperdata.h }; - $.post(OC.router_base_url+'/avatar/', {crop: data}, avatarResponseHandler); + $.post(OC.router_base_url+'/avatar/cropped', {crop: data}, avatarResponseHandler); } function saveCoords(c) { diff --git a/tests/lib/avatar.php b/tests/lib/avatar.php index 76cbd85fc4..321bb771fb 100644 --- a/tests/lib/avatar.php +++ b/tests/lib/avatar.php @@ -9,14 +9,16 @@ class Test_Avatar extends PHPUnit_Framework_TestCase { public function testAvatar() { - $this->assertEquals(false, \OC_Avatar::get(\OC_User::getUser())); + $avatar = new \OC_Avatar(); + + $this->assertEquals(false, $avatar->get(\OC_User::getUser())); $expected = new OC_Image(\OC::$SERVERROOT.'/tests/data/testavatar.png'); - \OC_Avatar::set(\OC_User::getUser(), $expected->data()); + $avatar->set(\OC_User::getUser(), $expected->data()); $expected->resize(64); - $this->assertEquals($expected->data(), \OC_Avatar::get(\OC_User::getUser())->data()); + $this->assertEquals($expected->data(), $avatar->get(\OC_User::getUser())->data()); - \OC_Avatar::remove(\OC_User::getUser()); - $this->assertEquals(false, \OC_Avatar::get(\OC_User::getUser())); + $avatar->remove(\OC_User::getUser()); + $this->assertEquals(false, $avatar->get(\OC_User::getUser())); } }