From c533b8068292e2b265c3c73f3ad9e5de0e98a81d Mon Sep 17 00:00:00 2001
From: kondou <kondou@ts.unde.re>
Date: Thu, 29 Aug 2013 16:56:32 +0200
Subject: [PATCH] Use OC_Cache and finish cropper functionality

---
 core/avatar/controller.php | 37 ++++++++-----------------------------
 core/routes.php            |  3 +--
 settings/js/personal.js    | 11 ++++-------
 3 files changed, 13 insertions(+), 38 deletions(-)

diff --git a/core/avatar/controller.php b/core/avatar/controller.php
index 64d9eafe52b..b4ee791130c 100644
--- a/core/avatar/controller.php
+++ b/core/avatar/controller.php
@@ -58,17 +58,8 @@ class OC_Core_Avatar_Controller {
 			\OC_JSON::success();
 		} catch (\OC\NotSquareException $e) {
 			$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_Cache::set('tmpavatar', $image->data());
 			\OC_JSON::error(array("data" => array("message" => "notsquare") ));
 		} catch (\Exception $e) {
 			\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
@@ -90,47 +81,35 @@ class OC_Core_Avatar_Controller {
 	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 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();
 
-		$view = new \OC\Files\View('/'.$user);
-		if ($view->file_exists('tmpavatar.png')) {
-			$ext = 'png';
-		} elseif ($view->file_exists('tmpavatar.jpg')) {
-			$ext = 'jpg';
-		} else {
+		$tmpavatar = \OC_Cache::get('tmpavatar');
+		if ($tmpavatar === false) {
 			\OC_JSON::error();
 			return;
 		}
 
-		$image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext));
-		$image->resize($args['size']);
+		$image = new \OC_Image($tmpavatar);
 		$image->show();
 	}
 
 	public static function postCroppedAvatar($args) {
 		$user = OC_User::getUser();
-		$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 {
+		$tmpavatar = \OC_Cache::get('tmpavatar');
+		if ($tmpavatar === false) {
 			\OC_JSON::error();
 			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']);
 		try {
 			$avatar = new \OC_Avatar();
 			$avatar->set($user, $image->data());
 			// Clean up
-			$view->unlink('tmpavatar.png');
-			$view->unlink('tmpavatar.jpg');
+			\OC_Cache::remove('tmpavatar');
 			\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 25f64a18830..640c8fb1bb8 100644
--- a/core/routes.php
+++ b/core/routes.php
@@ -68,8 +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', '/avatartmp/{size}') //TODO better naming, so it doesn't conflict with core_avatar_get
-	->defaults(array('size' => 64))
+$this->create('core_avatar_get_tmp', '/avatartmp/') //TODO better naming, so it doesn't conflict with core_avatar_get
 	->get()
 	->action('OC_Core_Avatar_Controller', 'getTmpAvatar');
 $this->create('core_avatar_post_cropped', '/avatar/cropped')
diff --git a/settings/js/personal.js b/settings/js/personal.js
index 03f4fbc9da9..abb085fac06 100644
--- a/settings/js/personal.js
+++ b/settings/js/personal.js
@@ -72,9 +72,11 @@ function showAvatarCropper() {
 		$(this).Jcrop({
 			onChange: 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() {
@@ -190,11 +192,6 @@ $(document).ready(function(){
 		}
 	});
 	
-	$('#uploadavatar').click(function(){
-		alert('To be done');
-		updateAvatar();
-	});
-
 	var uploadparms = {
 		done: function(e, data) {
 			avatarResponseHandler(data.result);
-- 
GitLab