diff --git a/apps/files/ajax/upload.php b/apps/files/ajax/upload.php
index fd6aa981542991a388b25ac0fb35432f2a2adaee..0a61ff72d41184df208e2957e5f005f124b46e0a 100644
--- a/apps/files/ajax/upload.php
+++ b/apps/files/ajax/upload.php
@@ -27,25 +27,24 @@ if (empty($_POST['dirToken'])) {
 	if (!($linkItem['permissions'] & OCP\PERMISSION_CREATE)) {
 		OCP\JSON::checkLoggedIn();
 	} else {
+		// resolve reshares
+		$rootLinkItem = OCP\Share::resolveReShare($linkItem);
+
+		// Setup FS with owner
+		OC_Util::setupFS($rootLinkItem['uid_owner']);
 
 		// The token defines the target directory (security reasons)
+		$path = \OC\Files\Filesystem::getPath($linkItem['file_source']);
 		$dir = sprintf(
 			"/%s/%s",
-			$linkItem['file_target'],
+			$path,
 			isset($_POST['subdir']) ? $_POST['subdir'] : ''
 		);
 
-		// handle reshare
-		if (!empty($linkItem['parent'])) {
-			$dir = '/Shared'.$dir;
-		}
-
 		if (!$dir || empty($dir) || $dir === false) {
 			OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
 			die();
 		}
-		// Setup FS with owner
-		OC_Util::setupFS($linkItem['uid_owner']);
 	}
 }
 
@@ -81,17 +80,17 @@ $files = $_FILES['files'];
 
 $error = '';
 
-$maxUploadFilesize = OCP\Util::maxUploadFilesize($dir);
-$maxHumanFilesize = OCP\Util::humanFileSize($maxUploadFilesize);
+$maxUploadFileSize = $storageStats['uploadMaxFilesize'];
+$maxHumanFileSize = OCP\Util::humanFileSize($maxUploadFileSize);
 
 $totalSize = 0;
 foreach ($files['size'] as $size) {
 	$totalSize += $size;
 }
-if ($maxUploadFilesize >= 0 and $totalSize > $maxUploadFilesize) {
+if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) {
 	OCP\JSON::error(array('data' => array('message' => $l->t('Not enough storage available'),
-		'uploadMaxFilesize' => $maxUploadFilesize,
-		'maxHumanFilesize' => $maxHumanFilesize)));
+		'uploadMaxFilesize' => $maxUploadFileSize,
+		'maxHumanFilesize' => $maxHumanFileSize)));
 	exit();
 }
 
@@ -113,8 +112,8 @@ if (strpos($dir, '..') === false) {
 				'id' => $meta['fileid'],
 				'name' => basename($target),
 				'originalname' => $files['name'][$i],
-				'uploadMaxFilesize' => $maxUploadFilesize,
-				'maxHumanFilesize' => $maxHumanFilesize
+				'uploadMaxFilesize' => $maxUploadFileSize,
+				'maxHumanFilesize' => $maxHumanFileSize
 			);
 		}
 	}
diff --git a/apps/files_sharing/public.php b/apps/files_sharing/public.php
index fb18bc2624894e53b827fc107595c03964fcaeea..7c9158d8002763faa8d291ccda3dc021cd453417 100644
--- a/apps/files_sharing/public.php
+++ b/apps/files_sharing/public.php
@@ -27,23 +27,9 @@ if (isset($_GET['t'])) {
 		$type = $linkItem['item_type'];
 		$fileSource = $linkItem['file_source'];
 		$shareOwner = $linkItem['uid_owner'];
-		$fileOwner = null;
 		$path = null;
-		if (isset($linkItem['parent'])) {
-			$parent = $linkItem['parent'];
-			while (isset($parent)) {
-				$query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
-				$item = $query->execute(array($parent))->fetchRow();
-				if (isset($item['parent'])) {
-					$parent = $item['parent'];
-				} else {
-					$fileOwner = $item['uid_owner'];
-					break;
-				}
-			}
-		} else {
-			$fileOwner = $shareOwner;
-		}
+		$rootLinkItem = OCP\Share::resolveReShare($linkItem);
+		$fileOwner = $rootLinkItem['uid_owner'];
 		if (isset($fileOwner)) {
 			OC_Util::tearDownFS();
 			OC_Util::setupFS($fileOwner);
diff --git a/lib/public/share.php b/lib/public/share.php
index de7025d7b15ad841cf5dfa4517c38dc837452480..69e6f35b473b3dc783b11afac9c99315f596e9a5 100644
--- a/lib/public/share.php
+++ b/lib/public/share.php
@@ -291,6 +291,29 @@ class Share {
 		return $result->fetchRow();
 	}
 
+	/**
+	 * @brief resolves reshares down to the last real share
+	 * @param $linkItem
+	 * @return $fileOwner
+	 */
+	public static function resolveReShare($linkItem)
+	{
+		if (isset($linkItem['parent'])) {
+			$parent = $linkItem['parent'];
+			while (isset($parent)) {
+				$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1);
+				$item = $query->execute(array($parent))->fetchRow();
+				if (isset($item['parent'])) {
+					$parent = $item['parent'];
+				} else {
+					return $item;
+				}
+			}
+		}
+		return $linkItem;
+	}
+
+
 	/**
 	* @brief Get the shared items of item type owned by the current user
 	* @param string Item type