From 1317b7c03dbbd98165ef29b50aa26bb1dd283cba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <thomas.mueller@tmit.eu>
Date: Wed, 23 Oct 2013 18:39:37 +0200
Subject: [PATCH] pass the name of the item source from the browser to the
 server - no need to get the data via complicated db queries

---
 apps/files_sharing/js/share.js |  4 ++--
 core/ajax/share.php            |  3 ++-
 core/js/share.js               | 24 +++++++++++++++----
 lib/public/share.php           | 44 ++++++++++++++++------------------
 4 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js
index 68f6f3ba76..340e093944 100644
--- a/apps/files_sharing/js/share.js
+++ b/apps/files_sharing/js/share.js
@@ -35,14 +35,14 @@ $(document).ready(function() {
 				if ($(tr).data('id') != $('#dropdown').attr('data-item-source')) {
 					OC.Share.hideDropDown(function () {
 						$(tr).addClass('mouseOver');
-						OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions);
+						OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions, filename);
 					});
 				} else {
 					OC.Share.hideDropDown();
 				}
 			} else {
 				$(tr).addClass('mouseOver');
-				OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions);
+				OC.Share.showDropDown(itemType, $(tr).data('id'), appendTo, true, possiblePermissions, filename);
 			}
 		});
 	}
diff --git a/core/ajax/share.php b/core/ajax/share.php
index 0dacc17d3a..be02c05635 100644
--- a/core/ajax/share.php
+++ b/core/ajax/share.php
@@ -41,7 +41,8 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSo
 						$_POST['itemSource'],
 						$shareType,
 						$shareWith,
-						$_POST['permissions']
+						$_POST['permissions'],
+						$_POST['itemSourceName']
 					);
 
 					if (is_string($token)) {
diff --git a/core/js/share.js b/core/js/share.js
index 281cccaaef..352ad4d4ca 100644
--- a/core/js/share.js
+++ b/core/js/share.js
@@ -136,8 +136,17 @@ OC.Share={
 
 		return data;
 	},
-	share:function(itemType, itemSource, shareType, shareWith, permissions, callback) {
-		$.post(OC.filePath('core', 'ajax', 'share.php'), { action: 'share', itemType: itemType, itemSource: itemSource, shareType: shareType, shareWith: shareWith, permissions: permissions }, function(result) {
+	share:function(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, callback) {
+		$.post(OC.filePath('core', 'ajax', 'share.php'),
+			{
+				action: 'share',
+				itemType: itemType,
+				itemSource: itemSource,
+				shareType: shareType,
+				shareWith: shareWith,
+				permissions: permissions,
+				itemSourceName: itemSourceName
+			}, function (result) {
 			if (result && result.status === 'success') {
 				if (callback) {
 					callback(result.data);
@@ -170,9 +179,9 @@ OC.Share={
 			}
 		});
 	},
-	showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions) {
+	showDropDown:function(itemType, itemSource, appendTo, link, possiblePermissions, filename) {
 		var data = OC.Share.loadItem(itemType, itemSource);
-		var html = '<div id="dropdown" class="drop" data-item-type="'+itemType+'" data-item-source="'+itemSource+'">';
+		var html = '<div id="dropdown" class="drop" data-item-type="'+itemType+'" data-item-source="'+itemSource+'"" data-item-source-name="'+filename+'">';
 		if (data !== false && data.reshare !== false && data.reshare.uid_owner !== undefined) {
 			if (data.reshare.share_type == OC.Share.SHARE_TYPE_GROUP) {
 				html += '<span class="reshare">'+t('core', 'Shared with you and the group {group} by {owner}', {group: escapeHTML(data.reshare.share_with), owner: escapeHTML(data.reshare.displayname_owner)})+'</span>';
@@ -276,12 +285,13 @@ OC.Share={
 				event.stopPropagation();
 				var itemType = $('#dropdown').data('item-type');
 				var itemSource = $('#dropdown').data('item-source');
+				var itemSourceName = $('#dropdown').data('item-source-name');
 				var shareType = selected.item.value.shareType;
 				var shareWith = selected.item.value.shareWith;
 				$(this).val(shareWith);
 				// Default permissions are Edit (CRUD) and Share
 				var permissions = OC.PERMISSION_ALL;
-				OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, function() {
+				OC.Share.share(itemType, itemSource, shareType, shareWith, permissions, itemSourceName, function() {
 					OC.Share.addShareWith(shareType, shareWith, selected.item.label, permissions, possiblePermissions);
 					$('#shareWith').val('');
 					OC.Share.updateIcon(itemType, itemSource);
@@ -588,6 +598,7 @@ $(document).ready(function() {
 	$(document).on('change', '#dropdown #linkCheckbox', function() {
 		var itemType = $('#dropdown').data('item-type');
 		var itemSource = $('#dropdown').data('item-source');
+		var itemSourceName = $('#dropdown').data('item-source-name');
 		if (this.checked) {
 			// Create a link
 			OC.Share.share(itemType, itemSource, OC.Share.SHARE_TYPE_LINK, '', OC.PERMISSION_READ, function(data) {
@@ -619,6 +630,7 @@ $(document).ready(function() {
 		var allowPublicUpload = $(this).is(':checked');
 		var itemType = $('#dropdown').data('item-type');
 		var itemSource = $('#dropdown').data('item-source');
+		var itemSourceName = $('#dropdown').data('item-source-name');
 		var permissions = 0;
 
 		// Calculate permissions
@@ -638,6 +650,7 @@ $(document).ready(function() {
 		if (!$('#showPassword').is(':checked') ) {
 			var itemType = $('#dropdown').data('item-type');
 			var itemSource = $('#dropdown').data('item-source');
+			var itemSourceName = $('#dropdown').data('item-source-name');
 			var allowPublicUpload = $('#sharingDialogAllowPublicUpload').is(':checked');
 			var permissions = 0;
 
@@ -663,6 +676,7 @@ $(document).ready(function() {
 			var dropDown = $('#dropdown');
 			var itemType = dropDown.data('item-type');
 			var itemSource = dropDown.data('item-source');
+			var itemSourceName = $('#dropdown').data('item-source-name');
 			var permissions = 0;
 
 			// Calculate permissions
diff --git a/lib/public/share.php b/lib/public/share.php
index dec79d998e..c62f964fe1 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -428,23 +428,26 @@ class Share {
 	}
 
 	/**
-	* Share an item with a user, group, or via private link
-	* @param string Item type
-	* @param string Item source
-	* @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
-	* @param string User or group the item is being shared with
-	* @param int CRUDS permissions
-	* @return bool|string Returns true on success or false on failure, Returns token on success for links
-	*/
-	public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) {
+	 * Share an item with a user, group, or via private link
+	 * @param string $itemType
+	 * @param string $itemSource
+	 * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
+	 * @param string $shareWith User or group the item is being shared with
+	 * @param int $permissions CRUDS
+	 * @param null $itemSourceName
+	 * @throws \Exception
+	 * @internal param \OCP\Item $string type
+	 * @internal param \OCP\Item $string source
+	 * @internal param \OCP\SHARE_TYPE_USER $int , SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
+	 * @internal param \OCP\User $string or group the item is being shared with
+	 * @internal param \OCP\CRUDS $int permissions
+	 * @return bool|string Returns true on success or false on failure, Returns token on success for links
+	 */
+	public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null) {
 		$uidOwner = \OC_User::getUser();
 		$sharingPolicy = \OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global');
 
-		//retrieve name of file
-		$fileData = \OC\Files\Filesystem::getFileInfo(\OC\Files\Filesystem::getPath($itemSource));
-		if(!is_null($fileData)) {
-			$itemSourceName = $fileData['name'];
-		} else {
+		if (is_null($itemSourceName)) {
 			$itemSourceName = $itemSource;
 		}
 
@@ -543,7 +546,7 @@ class Share {
 					$token = \OC_Util::generateRandomBytes(self::TOKEN_LENGTH);
 				}
 				$result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions,
-					null, $token);
+					null, $token, $itemSourceName);
 				if ($result) {
 					return $token;
 				} else {
@@ -609,7 +612,7 @@ class Share {
 // 			return false;
 // 		} else {
 			// Put the item into the database
-			return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions);
+			return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName);
 // 		}
 	}
 
@@ -1329,18 +1332,11 @@ class Share {
 	* @return bool Returns true on success or false on failure
 	*/
 	private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner,
-		$permissions, $parentFolder = null, $token = null) {
+		$permissions, $parentFolder = null, $token = null, $itemSourceName = null) {
 		$backend = self::getBackend($itemType);
 
 		// Check if this is a reshare
 		if ($checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true)) {
-			//retrieve name of file
-			$fileData = \OC\Files\Filesystem::getFileInfo(\OC\Files\Filesystem::getPath($itemSource));
-			if(!is_null($fileData)) {
-				$itemSourceName = $fileData['name'];
-			} else {
-				$itemSourceName = $itemSource;
-			}
 
 			// Check if attempting to share back to owner
 			if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) {
-- 
GitLab