Continue work on cropper

This commit is contained in:
kondou 2013-08-28 16:39:00 +02:00
parent 1b45683168
commit 8d8a57de7f
9 changed files with 92 additions and 25 deletions

@ -1 +1 @@
Subproject commit 2f3ae9f56a9838b45254393e13c14f8a8c380d6b
Subproject commit ea5e07f120177092cdb11ee16d7b54fb1ff16cb3

View File

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

View File

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

View File

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

13
lib/cleanupavatarjob.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class CleanUpAvatarJob extends \OC\BackgroundJob\TimedJob {
public function __construct () {
$this->setInterval(7200); // 2 hours
}
public function run ($argument) {
// TODO $view
// TODO remove ALL the tmpavatars
}
}

View File

@ -426,7 +426,7 @@ class OC_Installer{
'OC_API::',
'OC_App::',
'OC_AppConfig::',
'OC_Avatar::',
'OC_Avatar',
'OC_BackgroundJob::',
'OC_Config::',
'OC_DB::',

View File

@ -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);
}
}

View File

@ -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) {

View File

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