Completed
Push — master ( ad5a29...d5cba5 )
by Morris
12:35
created
apps/files/lib/Helper.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
 			'uploadMaxFilesize' => $maxUploadFileSize,
55 55
 			'maxHumanFilesize'  => $maxHumanFileSize,
56 56
 			'freeSpace' => $storageInfo['free'],
57
-			'usedSpacePercent'  => (int)$storageInfo['relative'],
57
+			'usedSpacePercent'  => (int) $storageInfo['relative'],
58 58
 			'owner' => $storageInfo['owner'],
59 59
 			'ownerDisplayName' => $storageInfo['ownerDisplayName'],
60 60
 		];
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 	 * @return string icon URL
68 68
 	 */
69 69
 	public static function determineIcon($file) {
70
-		if($file['type'] === 'dir') {
70
+		if ($file['type'] === 'dir') {
71 71
 			$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
72 72
 			// TODO: move this part to the client side, using mountType
73 73
 			if ($file->isShared()) {
@@ -75,11 +75,11 @@  discard block
 block discarded – undo
75 75
 			} elseif ($file->isMounted()) {
76 76
 				$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
77 77
 			}
78
-		}else{
78
+		} else {
79 79
 			$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
80 80
 		}
81 81
 
82
-		return substr($icon, 0, -3) . 'svg';
82
+		return substr($icon, 0, -3).'svg';
83 83
 	}
84 84
 
85 85
 	/**
Please login to merge, or discard this patch.
Indentation   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -38,230 +38,230 @@
 block discarded – undo
38 38
  * Helper class for manipulating file information
39 39
  */
40 40
 class Helper {
41
-	/**
42
-	 * @param string $dir
43
-	 * @return array
44
-	 * @throws \OCP\Files\NotFoundException
45
-	 */
46
-	public static function buildFileStorageStatistics($dir) {
47
-		// information about storage capacities
48
-		$storageInfo = \OC_Helper::getStorageInfo($dir);
49
-		$l = \OC::$server->getL10N('files');
50
-		$maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
51
-		$maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
52
-		$maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize));
41
+    /**
42
+     * @param string $dir
43
+     * @return array
44
+     * @throws \OCP\Files\NotFoundException
45
+     */
46
+    public static function buildFileStorageStatistics($dir) {
47
+        // information about storage capacities
48
+        $storageInfo = \OC_Helper::getStorageInfo($dir);
49
+        $l = \OC::$server->getL10N('files');
50
+        $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']);
51
+        $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize);
52
+        $maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize));
53 53
 
54
-		return [
55
-			'uploadMaxFilesize' => $maxUploadFileSize,
56
-			'maxHumanFilesize'  => $maxHumanFileSize,
57
-			'freeSpace' => $storageInfo['free'],
58
-			'usedSpacePercent'  => (int)$storageInfo['relative'],
59
-			'owner' => $storageInfo['owner'],
60
-			'ownerDisplayName' => $storageInfo['ownerDisplayName'],
61
-		];
62
-	}
54
+        return [
55
+            'uploadMaxFilesize' => $maxUploadFileSize,
56
+            'maxHumanFilesize'  => $maxHumanFileSize,
57
+            'freeSpace' => $storageInfo['free'],
58
+            'usedSpacePercent'  => (int)$storageInfo['relative'],
59
+            'owner' => $storageInfo['owner'],
60
+            'ownerDisplayName' => $storageInfo['ownerDisplayName'],
61
+        ];
62
+    }
63 63
 
64
-	/**
65
-	 * Determine icon for a given file
66
-	 *
67
-	 * @param \OCP\Files\FileInfo $file file info
68
-	 * @return string icon URL
69
-	 */
70
-	public static function determineIcon($file) {
71
-		if($file['type'] === 'dir') {
72
-			$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
73
-			// TODO: move this part to the client side, using mountType
74
-			if ($file->isShared()) {
75
-				$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
76
-			} elseif ($file->isMounted()) {
77
-				$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
78
-			}
79
-		}else{
80
-			$icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
81
-		}
64
+    /**
65
+     * Determine icon for a given file
66
+     *
67
+     * @param \OCP\Files\FileInfo $file file info
68
+     * @return string icon URL
69
+     */
70
+    public static function determineIcon($file) {
71
+        if($file['type'] === 'dir') {
72
+            $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir');
73
+            // TODO: move this part to the client side, using mountType
74
+            if ($file->isShared()) {
75
+                $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared');
76
+            } elseif ($file->isMounted()) {
77
+                $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external');
78
+            }
79
+        }else{
80
+            $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype());
81
+        }
82 82
 
83
-		return substr($icon, 0, -3) . 'svg';
84
-	}
83
+        return substr($icon, 0, -3) . 'svg';
84
+    }
85 85
 
86
-	/**
87
-	 * Comparator function to sort files alphabetically and have
88
-	 * the directories appear first
89
-	 *
90
-	 * @param \OCP\Files\FileInfo $a file
91
-	 * @param \OCP\Files\FileInfo $b file
92
-	 * @return int -1 if $a must come before $b, 1 otherwise
93
-	 */
94
-	public static function compareFileNames(FileInfo $a, FileInfo $b) {
95
-		$aType = $a->getType();
96
-		$bType = $b->getType();
97
-		if ($aType === 'dir' and $bType !== 'dir') {
98
-			return -1;
99
-		} elseif ($aType !== 'dir' and $bType === 'dir') {
100
-			return 1;
101
-		} else {
102
-			return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
103
-		}
104
-	}
86
+    /**
87
+     * Comparator function to sort files alphabetically and have
88
+     * the directories appear first
89
+     *
90
+     * @param \OCP\Files\FileInfo $a file
91
+     * @param \OCP\Files\FileInfo $b file
92
+     * @return int -1 if $a must come before $b, 1 otherwise
93
+     */
94
+    public static function compareFileNames(FileInfo $a, FileInfo $b) {
95
+        $aType = $a->getType();
96
+        $bType = $b->getType();
97
+        if ($aType === 'dir' and $bType !== 'dir') {
98
+            return -1;
99
+        } elseif ($aType !== 'dir' and $bType === 'dir') {
100
+            return 1;
101
+        } else {
102
+            return \OCP\Util::naturalSortCompare($a->getName(), $b->getName());
103
+        }
104
+    }
105 105
 
106
-	/**
107
-	 * Comparator function to sort files by date
108
-	 *
109
-	 * @param \OCP\Files\FileInfo $a file
110
-	 * @param \OCP\Files\FileInfo $b file
111
-	 * @return int -1 if $a must come before $b, 1 otherwise
112
-	 */
113
-	public static function compareTimestamp(FileInfo $a, FileInfo $b) {
114
-		$aTime = $a->getMTime();
115
-		$bTime = $b->getMTime();
116
-		return ($aTime < $bTime) ? -1 : 1;
117
-	}
106
+    /**
107
+     * Comparator function to sort files by date
108
+     *
109
+     * @param \OCP\Files\FileInfo $a file
110
+     * @param \OCP\Files\FileInfo $b file
111
+     * @return int -1 if $a must come before $b, 1 otherwise
112
+     */
113
+    public static function compareTimestamp(FileInfo $a, FileInfo $b) {
114
+        $aTime = $a->getMTime();
115
+        $bTime = $b->getMTime();
116
+        return ($aTime < $bTime) ? -1 : 1;
117
+    }
118 118
 
119
-	/**
120
-	 * Comparator function to sort files by size
121
-	 *
122
-	 * @param \OCP\Files\FileInfo $a file
123
-	 * @param \OCP\Files\FileInfo $b file
124
-	 * @return int -1 if $a must come before $b, 1 otherwise
125
-	 */
126
-	public static function compareSize(FileInfo $a, FileInfo $b) {
127
-		$aSize = $a->getSize();
128
-		$bSize = $b->getSize();
129
-		return ($aSize < $bSize) ? -1 : 1;
130
-	}
119
+    /**
120
+     * Comparator function to sort files by size
121
+     *
122
+     * @param \OCP\Files\FileInfo $a file
123
+     * @param \OCP\Files\FileInfo $b file
124
+     * @return int -1 if $a must come before $b, 1 otherwise
125
+     */
126
+    public static function compareSize(FileInfo $a, FileInfo $b) {
127
+        $aSize = $a->getSize();
128
+        $bSize = $b->getSize();
129
+        return ($aSize < $bSize) ? -1 : 1;
130
+    }
131 131
 
132
-	/**
133
-	 * Formats the file info to be returned as JSON to the client.
134
-	 *
135
-	 * @param \OCP\Files\FileInfo $i
136
-	 * @return array formatted file info
137
-	 */
138
-	public static function formatFileInfo(FileInfo $i) {
139
-		$entry = array();
132
+    /**
133
+     * Formats the file info to be returned as JSON to the client.
134
+     *
135
+     * @param \OCP\Files\FileInfo $i
136
+     * @return array formatted file info
137
+     */
138
+    public static function formatFileInfo(FileInfo $i) {
139
+        $entry = array();
140 140
 
141
-		$entry['id'] = $i['fileid'];
142
-		$entry['parentId'] = $i['parent'];
143
-		$entry['mtime'] = $i['mtime'] * 1000;
144
-		// only pick out the needed attributes
145
-		$entry['name'] = $i->getName();
146
-		$entry['permissions'] = $i['permissions'];
147
-		$entry['mimetype'] = $i['mimetype'];
148
-		$entry['size'] = $i['size'];
149
-		$entry['type'] = $i['type'];
150
-		$entry['etag'] = $i['etag'];
151
-		if (isset($i['tags'])) {
152
-			$entry['tags'] = $i['tags'];
153
-		}
154
-		if (isset($i['displayname_owner'])) {
155
-			$entry['shareOwner'] = $i['displayname_owner'];
156
-		}
157
-		if (isset($i['is_share_mount_point'])) {
158
-			$entry['isShareMountPoint'] = $i['is_share_mount_point'];
159
-		}
160
-		$mountType = null;
161
-		$mount = $i->getMountPoint();
162
-		$mountType = $mount->getMountType();
163
-		if ($mountType !== '') {
164
-			if ($i->getInternalPath() === '') {
165
-				$mountType .= '-root';
166
-			}
167
-			$entry['mountType'] = $mountType;
168
-		}
169
-		if (isset($i['extraData'])) {
170
-			$entry['extraData'] = $i['extraData'];
171
-		}
172
-		return $entry;
173
-	}
141
+        $entry['id'] = $i['fileid'];
142
+        $entry['parentId'] = $i['parent'];
143
+        $entry['mtime'] = $i['mtime'] * 1000;
144
+        // only pick out the needed attributes
145
+        $entry['name'] = $i->getName();
146
+        $entry['permissions'] = $i['permissions'];
147
+        $entry['mimetype'] = $i['mimetype'];
148
+        $entry['size'] = $i['size'];
149
+        $entry['type'] = $i['type'];
150
+        $entry['etag'] = $i['etag'];
151
+        if (isset($i['tags'])) {
152
+            $entry['tags'] = $i['tags'];
153
+        }
154
+        if (isset($i['displayname_owner'])) {
155
+            $entry['shareOwner'] = $i['displayname_owner'];
156
+        }
157
+        if (isset($i['is_share_mount_point'])) {
158
+            $entry['isShareMountPoint'] = $i['is_share_mount_point'];
159
+        }
160
+        $mountType = null;
161
+        $mount = $i->getMountPoint();
162
+        $mountType = $mount->getMountType();
163
+        if ($mountType !== '') {
164
+            if ($i->getInternalPath() === '') {
165
+                $mountType .= '-root';
166
+            }
167
+            $entry['mountType'] = $mountType;
168
+        }
169
+        if (isset($i['extraData'])) {
170
+            $entry['extraData'] = $i['extraData'];
171
+        }
172
+        return $entry;
173
+    }
174 174
 
175
-	/**
176
-	 * Format file info for JSON
177
-	 * @param \OCP\Files\FileInfo[] $fileInfos file infos
178
-	 * @return array
179
-	 */
180
-	public static function formatFileInfos($fileInfos) {
181
-		$files = array();
182
-		foreach ($fileInfos as $i) {
183
-			$files[] = self::formatFileInfo($i);
184
-		}
175
+    /**
176
+     * Format file info for JSON
177
+     * @param \OCP\Files\FileInfo[] $fileInfos file infos
178
+     * @return array
179
+     */
180
+    public static function formatFileInfos($fileInfos) {
181
+        $files = array();
182
+        foreach ($fileInfos as $i) {
183
+            $files[] = self::formatFileInfo($i);
184
+        }
185 185
 
186
-		return $files;
187
-	}
186
+        return $files;
187
+    }
188 188
 
189
-	/**
190
-	 * Retrieves the contents of the given directory and
191
-	 * returns it as a sorted array of FileInfo.
192
-	 *
193
-	 * @param string $dir path to the directory
194
-	 * @param string $sortAttribute attribute to sort on
195
-	 * @param bool $sortDescending true for descending sort, false otherwise
196
-	 * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
197
-	 * @return \OCP\Files\FileInfo[] files
198
-	 */
199
-	public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
200
-		$content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
189
+    /**
190
+     * Retrieves the contents of the given directory and
191
+     * returns it as a sorted array of FileInfo.
192
+     *
193
+     * @param string $dir path to the directory
194
+     * @param string $sortAttribute attribute to sort on
195
+     * @param bool $sortDescending true for descending sort, false otherwise
196
+     * @param string $mimetypeFilter limit returned content to this mimetype or mimepart
197
+     * @return \OCP\Files\FileInfo[] files
198
+     */
199
+    public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') {
200
+        $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter);
201 201
 
202
-		return self::sortFiles($content, $sortAttribute, $sortDescending);
203
-	}
202
+        return self::sortFiles($content, $sortAttribute, $sortDescending);
203
+    }
204 204
 
205
-	/**
206
-	 * Populate the result set with file tags
207
-	 *
208
-	 * @param array $fileList
209
-	 * @param string $fileIdentifier identifier attribute name for values in $fileList
210
-	 * @param ITagManager $tagManager
211
-	 * @return array file list populated with tags
212
-	 */
213
-	public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) {
214
-		$ids = [];
215
-		foreach ($fileList as $fileData) {
216
-			$ids[] = $fileData[$fileIdentifier];
217
-		}
218
-		$tagger = $tagManager->load('files');
219
-		$tags = $tagger->getTagsForObjects($ids);
205
+    /**
206
+     * Populate the result set with file tags
207
+     *
208
+     * @param array $fileList
209
+     * @param string $fileIdentifier identifier attribute name for values in $fileList
210
+     * @param ITagManager $tagManager
211
+     * @return array file list populated with tags
212
+     */
213
+    public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) {
214
+        $ids = [];
215
+        foreach ($fileList as $fileData) {
216
+            $ids[] = $fileData[$fileIdentifier];
217
+        }
218
+        $tagger = $tagManager->load('files');
219
+        $tags = $tagger->getTagsForObjects($ids);
220 220
 
221
-		if (!is_array($tags)) {
222
-			throw new \UnexpectedValueException('$tags must be an array');
223
-		}
221
+        if (!is_array($tags)) {
222
+            throw new \UnexpectedValueException('$tags must be an array');
223
+        }
224 224
 
225
-		// Set empty tag array
226
-		foreach ($fileList as $key => $fileData) {
227
-			$fileList[$key]['tags'] = [];
228
-		}
225
+        // Set empty tag array
226
+        foreach ($fileList as $key => $fileData) {
227
+            $fileList[$key]['tags'] = [];
228
+        }
229 229
 
230
-		if (!empty($tags)) {
231
-			foreach ($tags as $fileId => $fileTags) {
230
+        if (!empty($tags)) {
231
+            foreach ($tags as $fileId => $fileTags) {
232 232
 
233
-				foreach ($fileList as $key => $fileData) {
234
-					if ($fileId !== $fileData[$fileIdentifier]) {
235
-						continue;
236
-					}
233
+                foreach ($fileList as $key => $fileData) {
234
+                    if ($fileId !== $fileData[$fileIdentifier]) {
235
+                        continue;
236
+                    }
237 237
 
238
-					$fileList[$key]['tags'] = $fileTags;
239
-				}
240
-			}
241
-		}
238
+                    $fileList[$key]['tags'] = $fileTags;
239
+                }
240
+            }
241
+        }
242 242
 
243
-		return $fileList;
244
-	}
243
+        return $fileList;
244
+    }
245 245
 
246
-	/**
247
-	 * Sort the given file info array
248
-	 *
249
-	 * @param \OCP\Files\FileInfo[] $files files to sort
250
-	 * @param string $sortAttribute attribute to sort on
251
-	 * @param bool $sortDescending true for descending sort, false otherwise
252
-	 * @return \OCP\Files\FileInfo[] sorted files
253
-	 */
254
-	public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
255
-		$sortFunc = 'compareFileNames';
256
-		if ($sortAttribute === 'mtime') {
257
-			$sortFunc = 'compareTimestamp';
258
-		} else if ($sortAttribute === 'size') {
259
-			$sortFunc = 'compareSize';
260
-		}
261
-		usort($files, array('\OCA\Files\Helper', $sortFunc));
262
-		if ($sortDescending) {
263
-			$files = array_reverse($files);
264
-		}
265
-		return $files;
266
-	}
246
+    /**
247
+     * Sort the given file info array
248
+     *
249
+     * @param \OCP\Files\FileInfo[] $files files to sort
250
+     * @param string $sortAttribute attribute to sort on
251
+     * @param bool $sortDescending true for descending sort, false otherwise
252
+     * @return \OCP\Files\FileInfo[] sorted files
253
+     */
254
+    public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
255
+        $sortFunc = 'compareFileNames';
256
+        if ($sortAttribute === 'mtime') {
257
+            $sortFunc = 'compareTimestamp';
258
+        } else if ($sortAttribute === 'size') {
259
+            $sortFunc = 'compareSize';
260
+        }
261
+        usort($files, array('\OCA\Files\Helper', $sortFunc));
262
+        if ($sortDescending) {
263
+            $files = array_reverse($files);
264
+        }
265
+        return $files;
266
+    }
267 267
 }
Please login to merge, or discard this patch.
apps/files_sharing/lib/Controller/ShareAPIController.php 1 patch
Indentation   +878 added lines, -878 removed lines patch added patch discarded remove patch
@@ -52,891 +52,891 @@
 block discarded – undo
52 52
  */
53 53
 class ShareAPIController extends OCSController {
54 54
 
55
-	/** @var IManager */
56
-	private $shareManager;
57
-	/** @var IGroupManager */
58
-	private $groupManager;
59
-	/** @var IUserManager */
60
-	private $userManager;
61
-	/** @var IRequest */
62
-	protected $request;
63
-	/** @var IRootFolder */
64
-	private $rootFolder;
65
-	/** @var IURLGenerator */
66
-	private $urlGenerator;
67
-	/** @var string */
68
-	private $currentUser;
69
-	/** @var IL10N */
70
-	private $l;
71
-	/** @var \OCP\Files\Node */
72
-	private $lockedNode;
73
-
74
-	/**
75
-	 * Share20OCS constructor.
76
-	 *
77
-	 * @param string $appName
78
-	 * @param IRequest $request
79
-	 * @param IManager $shareManager
80
-	 * @param IGroupManager $groupManager
81
-	 * @param IUserManager $userManager
82
-	 * @param IRootFolder $rootFolder
83
-	 * @param IURLGenerator $urlGenerator
84
-	 * @param string $userId
85
-	 * @param IL10N $l10n
86
-	 */
87
-	public function __construct(
88
-		$appName,
89
-		IRequest $request,
90
-		IManager $shareManager,
91
-		IGroupManager $groupManager,
92
-		IUserManager $userManager,
93
-		IRootFolder $rootFolder,
94
-		IURLGenerator $urlGenerator,
95
-		$userId,
96
-		IL10N $l10n
97
-	) {
98
-		parent::__construct($appName, $request);
99
-
100
-		$this->shareManager = $shareManager;
101
-		$this->userManager = $userManager;
102
-		$this->groupManager = $groupManager;
103
-		$this->request = $request;
104
-		$this->rootFolder = $rootFolder;
105
-		$this->urlGenerator = $urlGenerator;
106
-		$this->currentUser = $userId;
107
-		$this->l = $l10n;
108
-	}
109
-
110
-	/**
111
-	 * Convert an IShare to an array for OCS output
112
-	 *
113
-	 * @param \OCP\Share\IShare $share
114
-	 * @param Node|null $recipientNode
115
-	 * @return array
116
-	 * @throws NotFoundException In case the node can't be resolved.
117
-	 */
118
-	protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null) {
119
-		$sharedBy = $this->userManager->get($share->getSharedBy());
120
-		$shareOwner = $this->userManager->get($share->getShareOwner());
121
-
122
-		$result = [
123
-			'id' => $share->getId(),
124
-			'share_type' => $share->getShareType(),
125
-			'uid_owner' => $share->getSharedBy(),
126
-			'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
127
-			'permissions' => $share->getPermissions(),
128
-			'stime' => $share->getShareTime()->getTimestamp(),
129
-			'parent' => null,
130
-			'expiration' => null,
131
-			'token' => null,
132
-			'uid_file_owner' => $share->getShareOwner(),
133
-			'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
134
-		];
135
-
136
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
137
-		if ($recipientNode) {
138
-			$node = $recipientNode;
139
-		} else {
140
-			$nodes = $userFolder->getById($share->getNodeId());
141
-
142
-			if (empty($nodes)) {
143
-				// fallback to guessing the path
144
-				$node = $userFolder->get($share->getTarget());
145
-				if ($node === null) {
146
-					throw new NotFoundException();
147
-				}
148
-			} else {
149
-				$node = $nodes[0];
150
-			}
151
-		}
152
-
153
-		$result['path'] = $userFolder->getRelativePath($node->getPath());
154
-		if ($node instanceOf \OCP\Files\Folder) {
155
-			$result['item_type'] = 'folder';
156
-		} else {
157
-			$result['item_type'] = 'file';
158
-		}
159
-		$result['mimetype'] = $node->getMimetype();
160
-		$result['storage_id'] = $node->getStorage()->getId();
161
-		$result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
162
-		$result['item_source'] = $node->getId();
163
-		$result['file_source'] = $node->getId();
164
-		$result['file_parent'] = $node->getParent()->getId();
165
-		$result['file_target'] = $share->getTarget();
166
-
167
-		$expiration = $share->getExpirationDate();
168
-		if ($expiration !== null) {
169
-			$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
170
-		}
171
-
172
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
173
-			$sharedWith = $this->userManager->get($share->getSharedWith());
174
-			$result['share_with'] = $share->getSharedWith();
175
-			$result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
176
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
177
-			$group = $this->groupManager->get($share->getSharedWith());
178
-			$result['share_with'] = $share->getSharedWith();
179
-			$result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
180
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
181
-
182
-			$result['share_with'] = $share->getPassword();
183
-			$result['share_with_displayname'] = $share->getPassword();
184
-
185
-			$result['token'] = $share->getToken();
186
-			$result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
187
-
188
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
189
-			$result['share_with'] = $share->getSharedWith();
190
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
191
-			$result['token'] = $share->getToken();
192
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
193
-			$result['share_with'] = $share->getSharedWith();
194
-			$result['password'] = $share->getPassword();
195
-			$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
196
-			$result['token'] = $share->getToken();
197
-		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
198
-			$result['share_with_displayname'] = $share->getSharedWith();
199
-			$result['share_with'] = explode(' ', $share->getSharedWith(), 2)[0];
200
-		}
201
-
202
-
203
-		$result['mail_send'] = $share->getMailSend() ? 1 : 0;
204
-
205
-		return $result;
206
-	}
207
-
208
-	/**
209
-	 * Check if one of the users address books knows the exact property, if
210
-	 * yes we return the full name.
211
-	 *
212
-	 * @param string $query
213
-	 * @param string $property
214
-	 * @return string
215
-	 */
216
-	private function getDisplayNameFromAddressBook($query, $property) {
217
-		// FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
218
-		$result = \OC::$server->getContactsManager()->search($query, [$property]);
219
-		foreach ($result as $r) {
220
-			foreach($r[$property] as $value) {
221
-				if ($value === $query) {
222
-					return $r['FN'];
223
-				}
224
-			}
225
-		}
226
-
227
-		return $query;
228
-	}
229
-
230
-	/**
231
-	 * Get a specific share by id
232
-	 *
233
-	 * @NoAdminRequired
234
-	 *
235
-	 * @param string $id
236
-	 * @return DataResponse
237
-	 * @throws OCSNotFoundException
238
-	 */
239
-	public function getShare($id) {
240
-		try {
241
-			$share = $this->getShareById($id);
242
-		} catch (ShareNotFound $e) {
243
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
244
-		}
245
-
246
-		if ($this->canAccessShare($share)) {
247
-			try {
248
-				$share = $this->formatShare($share);
249
-				return new DataResponse([$share]);
250
-			} catch (NotFoundException $e) {
251
-				//Fall trough
252
-			}
253
-		}
254
-
255
-		throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
256
-	}
257
-
258
-	/**
259
-	 * Delete a share
260
-	 *
261
-	 * @NoAdminRequired
262
-	 *
263
-	 * @param string $id
264
-	 * @return DataResponse
265
-	 * @throws OCSNotFoundException
266
-	 */
267
-	public function deleteShare($id) {
268
-		try {
269
-			$share = $this->getShareById($id);
270
-		} catch (ShareNotFound $e) {
271
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
272
-		}
273
-
274
-		try {
275
-			$this->lock($share->getNode());
276
-		} catch (LockedException $e) {
277
-			throw new OCSNotFoundException($this->l->t('could not delete share'));
278
-		}
279
-
280
-		if (!$this->canAccessShare($share)) {
281
-			throw new OCSNotFoundException($this->l->t('Could not delete share'));
282
-		}
283
-
284
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
285
-			$share->getShareOwner() !== $this->currentUser &&
286
-			$share->getSharedBy() !== $this->currentUser) {
287
-			$this->shareManager->deleteFromSelf($share, $this->currentUser);
288
-		} else {
289
-			$this->shareManager->deleteShare($share);
290
-		}
291
-
292
-		return new DataResponse();
293
-	}
294
-
295
-	/**
296
-	 * @NoAdminRequired
297
-	 *
298
-	 * @param string $path
299
-	 * @param int $permissions
300
-	 * @param int $shareType
301
-	 * @param string $shareWith
302
-	 * @param string $publicUpload
303
-	 * @param string $password
304
-	 * @param string $expireDate
305
-	 *
306
-	 * @return DataResponse
307
-	 * @throws OCSNotFoundException
308
-	 * @throws OCSForbiddenException
309
-	 * @throws OCSBadRequestException
310
-	 * @throws OCSException
311
-	 *
312
-	 * @suppress PhanUndeclaredClassMethod
313
-	 */
314
-	public function createShare(
315
-		$path = null,
316
-		$permissions = \OCP\Constants::PERMISSION_ALL,
317
-		$shareType = -1,
318
-		$shareWith = null,
319
-		$publicUpload = 'false',
320
-		$password = '',
321
-		$expireDate = ''
322
-	) {
323
-		$share = $this->shareManager->newShare();
324
-
325
-		// Verify path
326
-		if ($path === null) {
327
-			throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
328
-		}
329
-
330
-		$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
331
-		try {
332
-			$path = $userFolder->get($path);
333
-		} catch (NotFoundException $e) {
334
-			throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
335
-		}
336
-
337
-		$share->setNode($path);
338
-
339
-		try {
340
-			$this->lock($share->getNode());
341
-		} catch (LockedException $e) {
342
-			throw new OCSNotFoundException($this->l->t('Could not create share'));
343
-		}
344
-
345
-		if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
346
-			throw new OCSNotFoundException($this->l->t('invalid permissions'));
347
-		}
348
-
349
-		// Shares always require read permissions
350
-		$permissions |= \OCP\Constants::PERMISSION_READ;
351
-
352
-		if ($path instanceof \OCP\Files\File) {
353
-			// Single file shares should never have delete or create permissions
354
-			$permissions &= ~\OCP\Constants::PERMISSION_DELETE;
355
-			$permissions &= ~\OCP\Constants::PERMISSION_CREATE;
356
-		}
357
-
358
-		/*
55
+    /** @var IManager */
56
+    private $shareManager;
57
+    /** @var IGroupManager */
58
+    private $groupManager;
59
+    /** @var IUserManager */
60
+    private $userManager;
61
+    /** @var IRequest */
62
+    protected $request;
63
+    /** @var IRootFolder */
64
+    private $rootFolder;
65
+    /** @var IURLGenerator */
66
+    private $urlGenerator;
67
+    /** @var string */
68
+    private $currentUser;
69
+    /** @var IL10N */
70
+    private $l;
71
+    /** @var \OCP\Files\Node */
72
+    private $lockedNode;
73
+
74
+    /**
75
+     * Share20OCS constructor.
76
+     *
77
+     * @param string $appName
78
+     * @param IRequest $request
79
+     * @param IManager $shareManager
80
+     * @param IGroupManager $groupManager
81
+     * @param IUserManager $userManager
82
+     * @param IRootFolder $rootFolder
83
+     * @param IURLGenerator $urlGenerator
84
+     * @param string $userId
85
+     * @param IL10N $l10n
86
+     */
87
+    public function __construct(
88
+        $appName,
89
+        IRequest $request,
90
+        IManager $shareManager,
91
+        IGroupManager $groupManager,
92
+        IUserManager $userManager,
93
+        IRootFolder $rootFolder,
94
+        IURLGenerator $urlGenerator,
95
+        $userId,
96
+        IL10N $l10n
97
+    ) {
98
+        parent::__construct($appName, $request);
99
+
100
+        $this->shareManager = $shareManager;
101
+        $this->userManager = $userManager;
102
+        $this->groupManager = $groupManager;
103
+        $this->request = $request;
104
+        $this->rootFolder = $rootFolder;
105
+        $this->urlGenerator = $urlGenerator;
106
+        $this->currentUser = $userId;
107
+        $this->l = $l10n;
108
+    }
109
+
110
+    /**
111
+     * Convert an IShare to an array for OCS output
112
+     *
113
+     * @param \OCP\Share\IShare $share
114
+     * @param Node|null $recipientNode
115
+     * @return array
116
+     * @throws NotFoundException In case the node can't be resolved.
117
+     */
118
+    protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null) {
119
+        $sharedBy = $this->userManager->get($share->getSharedBy());
120
+        $shareOwner = $this->userManager->get($share->getShareOwner());
121
+
122
+        $result = [
123
+            'id' => $share->getId(),
124
+            'share_type' => $share->getShareType(),
125
+            'uid_owner' => $share->getSharedBy(),
126
+            'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
127
+            'permissions' => $share->getPermissions(),
128
+            'stime' => $share->getShareTime()->getTimestamp(),
129
+            'parent' => null,
130
+            'expiration' => null,
131
+            'token' => null,
132
+            'uid_file_owner' => $share->getShareOwner(),
133
+            'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
134
+        ];
135
+
136
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
137
+        if ($recipientNode) {
138
+            $node = $recipientNode;
139
+        } else {
140
+            $nodes = $userFolder->getById($share->getNodeId());
141
+
142
+            if (empty($nodes)) {
143
+                // fallback to guessing the path
144
+                $node = $userFolder->get($share->getTarget());
145
+                if ($node === null) {
146
+                    throw new NotFoundException();
147
+                }
148
+            } else {
149
+                $node = $nodes[0];
150
+            }
151
+        }
152
+
153
+        $result['path'] = $userFolder->getRelativePath($node->getPath());
154
+        if ($node instanceOf \OCP\Files\Folder) {
155
+            $result['item_type'] = 'folder';
156
+        } else {
157
+            $result['item_type'] = 'file';
158
+        }
159
+        $result['mimetype'] = $node->getMimetype();
160
+        $result['storage_id'] = $node->getStorage()->getId();
161
+        $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
162
+        $result['item_source'] = $node->getId();
163
+        $result['file_source'] = $node->getId();
164
+        $result['file_parent'] = $node->getParent()->getId();
165
+        $result['file_target'] = $share->getTarget();
166
+
167
+        $expiration = $share->getExpirationDate();
168
+        if ($expiration !== null) {
169
+            $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
170
+        }
171
+
172
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
173
+            $sharedWith = $this->userManager->get($share->getSharedWith());
174
+            $result['share_with'] = $share->getSharedWith();
175
+            $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
176
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
177
+            $group = $this->groupManager->get($share->getSharedWith());
178
+            $result['share_with'] = $share->getSharedWith();
179
+            $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
180
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
181
+
182
+            $result['share_with'] = $share->getPassword();
183
+            $result['share_with_displayname'] = $share->getPassword();
184
+
185
+            $result['token'] = $share->getToken();
186
+            $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
187
+
188
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
189
+            $result['share_with'] = $share->getSharedWith();
190
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
191
+            $result['token'] = $share->getToken();
192
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
193
+            $result['share_with'] = $share->getSharedWith();
194
+            $result['password'] = $share->getPassword();
195
+            $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
196
+            $result['token'] = $share->getToken();
197
+        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
198
+            $result['share_with_displayname'] = $share->getSharedWith();
199
+            $result['share_with'] = explode(' ', $share->getSharedWith(), 2)[0];
200
+        }
201
+
202
+
203
+        $result['mail_send'] = $share->getMailSend() ? 1 : 0;
204
+
205
+        return $result;
206
+    }
207
+
208
+    /**
209
+     * Check if one of the users address books knows the exact property, if
210
+     * yes we return the full name.
211
+     *
212
+     * @param string $query
213
+     * @param string $property
214
+     * @return string
215
+     */
216
+    private function getDisplayNameFromAddressBook($query, $property) {
217
+        // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
218
+        $result = \OC::$server->getContactsManager()->search($query, [$property]);
219
+        foreach ($result as $r) {
220
+            foreach($r[$property] as $value) {
221
+                if ($value === $query) {
222
+                    return $r['FN'];
223
+                }
224
+            }
225
+        }
226
+
227
+        return $query;
228
+    }
229
+
230
+    /**
231
+     * Get a specific share by id
232
+     *
233
+     * @NoAdminRequired
234
+     *
235
+     * @param string $id
236
+     * @return DataResponse
237
+     * @throws OCSNotFoundException
238
+     */
239
+    public function getShare($id) {
240
+        try {
241
+            $share = $this->getShareById($id);
242
+        } catch (ShareNotFound $e) {
243
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
244
+        }
245
+
246
+        if ($this->canAccessShare($share)) {
247
+            try {
248
+                $share = $this->formatShare($share);
249
+                return new DataResponse([$share]);
250
+            } catch (NotFoundException $e) {
251
+                //Fall trough
252
+            }
253
+        }
254
+
255
+        throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
256
+    }
257
+
258
+    /**
259
+     * Delete a share
260
+     *
261
+     * @NoAdminRequired
262
+     *
263
+     * @param string $id
264
+     * @return DataResponse
265
+     * @throws OCSNotFoundException
266
+     */
267
+    public function deleteShare($id) {
268
+        try {
269
+            $share = $this->getShareById($id);
270
+        } catch (ShareNotFound $e) {
271
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
272
+        }
273
+
274
+        try {
275
+            $this->lock($share->getNode());
276
+        } catch (LockedException $e) {
277
+            throw new OCSNotFoundException($this->l->t('could not delete share'));
278
+        }
279
+
280
+        if (!$this->canAccessShare($share)) {
281
+            throw new OCSNotFoundException($this->l->t('Could not delete share'));
282
+        }
283
+
284
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP &&
285
+            $share->getShareOwner() !== $this->currentUser &&
286
+            $share->getSharedBy() !== $this->currentUser) {
287
+            $this->shareManager->deleteFromSelf($share, $this->currentUser);
288
+        } else {
289
+            $this->shareManager->deleteShare($share);
290
+        }
291
+
292
+        return new DataResponse();
293
+    }
294
+
295
+    /**
296
+     * @NoAdminRequired
297
+     *
298
+     * @param string $path
299
+     * @param int $permissions
300
+     * @param int $shareType
301
+     * @param string $shareWith
302
+     * @param string $publicUpload
303
+     * @param string $password
304
+     * @param string $expireDate
305
+     *
306
+     * @return DataResponse
307
+     * @throws OCSNotFoundException
308
+     * @throws OCSForbiddenException
309
+     * @throws OCSBadRequestException
310
+     * @throws OCSException
311
+     *
312
+     * @suppress PhanUndeclaredClassMethod
313
+     */
314
+    public function createShare(
315
+        $path = null,
316
+        $permissions = \OCP\Constants::PERMISSION_ALL,
317
+        $shareType = -1,
318
+        $shareWith = null,
319
+        $publicUpload = 'false',
320
+        $password = '',
321
+        $expireDate = ''
322
+    ) {
323
+        $share = $this->shareManager->newShare();
324
+
325
+        // Verify path
326
+        if ($path === null) {
327
+            throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
328
+        }
329
+
330
+        $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
331
+        try {
332
+            $path = $userFolder->get($path);
333
+        } catch (NotFoundException $e) {
334
+            throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
335
+        }
336
+
337
+        $share->setNode($path);
338
+
339
+        try {
340
+            $this->lock($share->getNode());
341
+        } catch (LockedException $e) {
342
+            throw new OCSNotFoundException($this->l->t('Could not create share'));
343
+        }
344
+
345
+        if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) {
346
+            throw new OCSNotFoundException($this->l->t('invalid permissions'));
347
+        }
348
+
349
+        // Shares always require read permissions
350
+        $permissions |= \OCP\Constants::PERMISSION_READ;
351
+
352
+        if ($path instanceof \OCP\Files\File) {
353
+            // Single file shares should never have delete or create permissions
354
+            $permissions &= ~\OCP\Constants::PERMISSION_DELETE;
355
+            $permissions &= ~\OCP\Constants::PERMISSION_CREATE;
356
+        }
357
+
358
+        /*
359 359
 		 * Hack for https://github.com/owncloud/core/issues/22587
360 360
 		 * We check the permissions via webdav. But the permissions of the mount point
361 361
 		 * do not equal the share permissions. Here we fix that for federated mounts.
362 362
 		 */
363
-		if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
364
-			$permissions &= ~($permissions & ~$path->getPermissions());
365
-		}
366
-
367
-		if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
368
-			// Valid user is required to share
369
-			if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
370
-				throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
371
-			}
372
-			$share->setSharedWith($shareWith);
373
-			$share->setPermissions($permissions);
374
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
375
-			if (!$this->shareManager->allowGroupSharing()) {
376
-				throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
377
-			}
378
-
379
-			// Valid group is required to share
380
-			if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
381
-				throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
382
-			}
383
-			$share->setSharedWith($shareWith);
384
-			$share->setPermissions($permissions);
385
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
386
-			//Can we even share links?
387
-			if (!$this->shareManager->shareApiAllowLinks()) {
388
-				throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
389
-			}
390
-
391
-			/*
363
+        if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
364
+            $permissions &= ~($permissions & ~$path->getPermissions());
365
+        }
366
+
367
+        if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
368
+            // Valid user is required to share
369
+            if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
370
+                throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
371
+            }
372
+            $share->setSharedWith($shareWith);
373
+            $share->setPermissions($permissions);
374
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
375
+            if (!$this->shareManager->allowGroupSharing()) {
376
+                throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
377
+            }
378
+
379
+            // Valid group is required to share
380
+            if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
381
+                throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
382
+            }
383
+            $share->setSharedWith($shareWith);
384
+            $share->setPermissions($permissions);
385
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
386
+            //Can we even share links?
387
+            if (!$this->shareManager->shareApiAllowLinks()) {
388
+                throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
389
+            }
390
+
391
+            /*
392 392
 			 * For now we only allow 1 link share.
393 393
 			 * Return the existing link share if this is a duplicate
394 394
 			 */
395
-			$existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
396
-			if (!empty($existingShares)) {
397
-				return new DataResponse($this->formatShare($existingShares[0]));
398
-			}
399
-
400
-			if ($publicUpload === 'true') {
401
-				// Check if public upload is allowed
402
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
403
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
404
-				}
405
-
406
-				// Public upload can only be set for folders
407
-				if ($path instanceof \OCP\Files\File) {
408
-					throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
409
-				}
410
-
411
-				$share->setPermissions(
412
-					\OCP\Constants::PERMISSION_READ |
413
-					\OCP\Constants::PERMISSION_CREATE |
414
-					\OCP\Constants::PERMISSION_UPDATE |
415
-					\OCP\Constants::PERMISSION_DELETE
416
-				);
417
-			} else {
418
-				$share->setPermissions(\OCP\Constants::PERMISSION_READ);
419
-			}
420
-
421
-			// Set password
422
-			if ($password !== '') {
423
-				$share->setPassword($password);
424
-			}
425
-
426
-			//Expire date
427
-			if ($expireDate !== '') {
428
-				try {
429
-					$expireDate = $this->parseDate($expireDate);
430
-					$share->setExpirationDate($expireDate);
431
-				} catch (\Exception $e) {
432
-					throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
433
-				}
434
-			}
435
-
436
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
437
-			if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
438
-				throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
439
-			}
440
-
441
-			$share->setSharedWith($shareWith);
442
-			$share->setPermissions($permissions);
443
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
444
-			if ($share->getNodeType() === 'file') {
445
-				$share->setPermissions(\OCP\Constants::PERMISSION_READ);
446
-			} else {
447
-				$share->setPermissions(
448
-					\OCP\Constants::PERMISSION_READ |
449
-					\OCP\Constants::PERMISSION_CREATE |
450
-					\OCP\Constants::PERMISSION_UPDATE |
451
-					\OCP\Constants::PERMISSION_DELETE);
452
-			}
453
-			$share->setSharedWith($shareWith);
454
-		} else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
455
-			if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
456
-				throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
457
-			}
458
-
459
-			$circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
460
-
461
-			// Valid circle is required to share
462
-			if ($circle === null) {
463
-				throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
464
-			}
465
-			$share->setSharedWith($shareWith);
466
-			$share->setPermissions($permissions);
467
-		} else {
468
-			throw new OCSBadRequestException($this->l->t('Unknown share type'));
469
-		}
470
-
471
-		$share->setShareType($shareType);
472
-		$share->setSharedBy($this->currentUser);
473
-
474
-		try {
475
-			$share = $this->shareManager->createShare($share);
476
-		} catch (GenericShareException $e) {
477
-			$code = $e->getCode() === 0 ? 403 : $e->getCode();
478
-			throw new OCSException($e->getHint(), $code);
479
-		} catch (\Exception $e) {
480
-			throw new OCSForbiddenException($e->getMessage());
481
-		}
482
-
483
-		$output = $this->formatShare($share);
484
-
485
-		return new DataResponse($output);
486
-	}
487
-
488
-	/**
489
-	 * @param \OCP\Files\File|\OCP\Files\Folder $node
490
-	 * @param boolean $includeTags
491
-	 * @return DataResponse
492
-	 */
493
-	private function getSharedWithMe($node = null, $includeTags) {
494
-
495
-		$userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
496
-		$groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
497
-		$circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
498
-
499
-		$shares = array_merge($userShares, $groupShares, $circleShares);
500
-
501
-		$shares = array_filter($shares, function (IShare $share) {
502
-			return $share->getShareOwner() !== $this->currentUser;
503
-		});
504
-
505
-		$formatted = [];
506
-		foreach ($shares as $share) {
507
-			if ($this->canAccessShare($share)) {
508
-				try {
509
-					$formatted[] = $this->formatShare($share);
510
-				} catch (NotFoundException $e) {
511
-					// Ignore this share
512
-				}
513
-			}
514
-		}
515
-
516
-		if ($includeTags) {
517
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
518
-		}
519
-
520
-		return new DataResponse($formatted);
521
-	}
522
-
523
-	/**
524
-	 * @param \OCP\Files\Folder $folder
525
-	 * @return DataResponse
526
-	 * @throws OCSBadRequestException
527
-	 */
528
-	private function getSharesInDir($folder) {
529
-		if (!($folder instanceof \OCP\Files\Folder)) {
530
-			throw new OCSBadRequestException($this->l->t('Not a directory'));
531
-		}
532
-
533
-		$nodes = $folder->getDirectoryListing();
534
-		/** @var \OCP\Share\IShare[] $shares */
535
-		$shares = [];
536
-		foreach ($nodes as $node) {
537
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
538
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
539
-			$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
540
-			if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
541
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
542
-			}
543
-			if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
544
-				$shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
545
-			}
546
-		}
547
-
548
-		$formatted = [];
549
-		foreach ($shares as $share) {
550
-			try {
551
-				$formatted[] = $this->formatShare($share);
552
-			} catch (NotFoundException $e) {
553
-				//Ignore this share
554
-			}
555
-		}
556
-
557
-		return new DataResponse($formatted);
558
-	}
559
-
560
-	/**
561
-	 * The getShares function.
562
-	 *
563
-	 * @NoAdminRequired
564
-	 *
565
-	 * @param string $shared_with_me
566
-	 * @param string $reshares
567
-	 * @param string $subfiles
568
-	 * @param string $path
569
-	 *
570
-	 * - Get shares by the current user
571
-	 * - Get shares by the current user and reshares (?reshares=true)
572
-	 * - Get shares with the current user (?shared_with_me=true)
573
-	 * - Get shares for a specific path (?path=...)
574
-	 * - Get all shares in a folder (?subfiles=true&path=..)
575
-	 *
576
-	 * @return DataResponse
577
-	 * @throws OCSNotFoundException
578
-	 */
579
-	public function getShares(
580
-		$shared_with_me = 'false',
581
-		$reshares = 'false',
582
-		$subfiles = 'false',
583
-		$path = null,
584
-		$include_tags = 'false'
585
-	) {
586
-
587
-		if ($path !== null) {
588
-			$userFolder = $this->rootFolder->getUserFolder($this->currentUser);
589
-			try {
590
-				$path = $userFolder->get($path);
591
-				$this->lock($path);
592
-			} catch (\OCP\Files\NotFoundException $e) {
593
-				throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
594
-			} catch (LockedException $e) {
595
-				throw new OCSNotFoundException($this->l->t('Could not lock path'));
596
-			}
597
-		}
598
-
599
-		if ($shared_with_me === 'true') {
600
-			$result = $this->getSharedWithMe($path, $include_tags);
601
-			return $result;
602
-		}
603
-
604
-		if ($subfiles === 'true') {
605
-			$result = $this->getSharesInDir($path);
606
-			return $result;
607
-		}
608
-
609
-		if ($reshares === 'true') {
610
-			$reshares = true;
611
-		} else {
612
-			$reshares = false;
613
-		}
614
-
615
-		// Get all shares
616
-		$userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
617
-		$groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
618
-		$linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
619
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
620
-			$mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
621
-		} else {
622
-			$mailShares = [];
623
-		}
624
-		if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
625
-			$circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
626
-		} else {
627
-			$circleShares = [];
628
-		}
629
-
630
-		$shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
631
-
632
-		if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
633
-			$federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
634
-			$shares = array_merge($shares, $federatedShares);
635
-		}
636
-
637
-		$formatted = [];
638
-		foreach ($shares as $share) {
639
-			try {
640
-				$formatted[] = $this->formatShare($share, $path);
641
-			} catch (NotFoundException $e) {
642
-				//Ignore share
643
-			}
644
-		}
645
-
646
-		if ($include_tags) {
647
-			$formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
648
-		}
649
-
650
-		return new DataResponse($formatted);
651
-	}
652
-
653
-	/**
654
-	 * @NoAdminRequired
655
-	 *
656
-	 * @param int $id
657
-	 * @param int $permissions
658
-	 * @param string $password
659
-	 * @param string $publicUpload
660
-	 * @param string $expireDate
661
-	 * @return DataResponse
662
-	 * @throws OCSNotFoundException
663
-	 * @throws OCSBadRequestException
664
-	 * @throws OCSForbiddenException
665
-	 */
666
-	public function updateShare(
667
-		$id,
668
-		$permissions = null,
669
-		$password = null,
670
-		$publicUpload = null,
671
-		$expireDate = null
672
-	) {
673
-		try {
674
-			$share = $this->getShareById($id);
675
-		} catch (ShareNotFound $e) {
676
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
677
-		}
678
-
679
-		$this->lock($share->getNode());
680
-
681
-		if (!$this->canAccessShare($share, false)) {
682
-			throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
683
-		}
684
-
685
-		if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
686
-			throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
687
-		}
688
-
689
-		/*
395
+            $existingShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0);
396
+            if (!empty($existingShares)) {
397
+                return new DataResponse($this->formatShare($existingShares[0]));
398
+            }
399
+
400
+            if ($publicUpload === 'true') {
401
+                // Check if public upload is allowed
402
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
403
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
404
+                }
405
+
406
+                // Public upload can only be set for folders
407
+                if ($path instanceof \OCP\Files\File) {
408
+                    throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
409
+                }
410
+
411
+                $share->setPermissions(
412
+                    \OCP\Constants::PERMISSION_READ |
413
+                    \OCP\Constants::PERMISSION_CREATE |
414
+                    \OCP\Constants::PERMISSION_UPDATE |
415
+                    \OCP\Constants::PERMISSION_DELETE
416
+                );
417
+            } else {
418
+                $share->setPermissions(\OCP\Constants::PERMISSION_READ);
419
+            }
420
+
421
+            // Set password
422
+            if ($password !== '') {
423
+                $share->setPassword($password);
424
+            }
425
+
426
+            //Expire date
427
+            if ($expireDate !== '') {
428
+                try {
429
+                    $expireDate = $this->parseDate($expireDate);
430
+                    $share->setExpirationDate($expireDate);
431
+                } catch (\Exception $e) {
432
+                    throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
433
+                }
434
+            }
435
+
436
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
437
+            if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
438
+                throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType]));
439
+            }
440
+
441
+            $share->setSharedWith($shareWith);
442
+            $share->setPermissions($permissions);
443
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_EMAIL) {
444
+            if ($share->getNodeType() === 'file') {
445
+                $share->setPermissions(\OCP\Constants::PERMISSION_READ);
446
+            } else {
447
+                $share->setPermissions(
448
+                    \OCP\Constants::PERMISSION_READ |
449
+                    \OCP\Constants::PERMISSION_CREATE |
450
+                    \OCP\Constants::PERMISSION_UPDATE |
451
+                    \OCP\Constants::PERMISSION_DELETE);
452
+            }
453
+            $share->setSharedWith($shareWith);
454
+        } else if ($shareType === \OCP\Share::SHARE_TYPE_CIRCLE) {
455
+            if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
456
+                throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
457
+            }
458
+
459
+            $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
460
+
461
+            // Valid circle is required to share
462
+            if ($circle === null) {
463
+                throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
464
+            }
465
+            $share->setSharedWith($shareWith);
466
+            $share->setPermissions($permissions);
467
+        } else {
468
+            throw new OCSBadRequestException($this->l->t('Unknown share type'));
469
+        }
470
+
471
+        $share->setShareType($shareType);
472
+        $share->setSharedBy($this->currentUser);
473
+
474
+        try {
475
+            $share = $this->shareManager->createShare($share);
476
+        } catch (GenericShareException $e) {
477
+            $code = $e->getCode() === 0 ? 403 : $e->getCode();
478
+            throw new OCSException($e->getHint(), $code);
479
+        } catch (\Exception $e) {
480
+            throw new OCSForbiddenException($e->getMessage());
481
+        }
482
+
483
+        $output = $this->formatShare($share);
484
+
485
+        return new DataResponse($output);
486
+    }
487
+
488
+    /**
489
+     * @param \OCP\Files\File|\OCP\Files\Folder $node
490
+     * @param boolean $includeTags
491
+     * @return DataResponse
492
+     */
493
+    private function getSharedWithMe($node = null, $includeTags) {
494
+
495
+        $userShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, -1, 0);
496
+        $groupShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0);
497
+        $circleShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
498
+
499
+        $shares = array_merge($userShares, $groupShares, $circleShares);
500
+
501
+        $shares = array_filter($shares, function (IShare $share) {
502
+            return $share->getShareOwner() !== $this->currentUser;
503
+        });
504
+
505
+        $formatted = [];
506
+        foreach ($shares as $share) {
507
+            if ($this->canAccessShare($share)) {
508
+                try {
509
+                    $formatted[] = $this->formatShare($share);
510
+                } catch (NotFoundException $e) {
511
+                    // Ignore this share
512
+                }
513
+            }
514
+        }
515
+
516
+        if ($includeTags) {
517
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
518
+        }
519
+
520
+        return new DataResponse($formatted);
521
+    }
522
+
523
+    /**
524
+     * @param \OCP\Files\Folder $folder
525
+     * @return DataResponse
526
+     * @throws OCSBadRequestException
527
+     */
528
+    private function getSharesInDir($folder) {
529
+        if (!($folder instanceof \OCP\Files\Folder)) {
530
+            throw new OCSBadRequestException($this->l->t('Not a directory'));
531
+        }
532
+
533
+        $nodes = $folder->getDirectoryListing();
534
+        /** @var \OCP\Share\IShare[] $shares */
535
+        $shares = [];
536
+        foreach ($nodes as $node) {
537
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $node, false, -1, 0));
538
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $node, false, -1, 0));
539
+            $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $node, false, -1, 0));
540
+            if($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
541
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $node, false, -1, 0));
542
+            }
543
+            if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
544
+                $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $node, false, -1, 0));
545
+            }
546
+        }
547
+
548
+        $formatted = [];
549
+        foreach ($shares as $share) {
550
+            try {
551
+                $formatted[] = $this->formatShare($share);
552
+            } catch (NotFoundException $e) {
553
+                //Ignore this share
554
+            }
555
+        }
556
+
557
+        return new DataResponse($formatted);
558
+    }
559
+
560
+    /**
561
+     * The getShares function.
562
+     *
563
+     * @NoAdminRequired
564
+     *
565
+     * @param string $shared_with_me
566
+     * @param string $reshares
567
+     * @param string $subfiles
568
+     * @param string $path
569
+     *
570
+     * - Get shares by the current user
571
+     * - Get shares by the current user and reshares (?reshares=true)
572
+     * - Get shares with the current user (?shared_with_me=true)
573
+     * - Get shares for a specific path (?path=...)
574
+     * - Get all shares in a folder (?subfiles=true&path=..)
575
+     *
576
+     * @return DataResponse
577
+     * @throws OCSNotFoundException
578
+     */
579
+    public function getShares(
580
+        $shared_with_me = 'false',
581
+        $reshares = 'false',
582
+        $subfiles = 'false',
583
+        $path = null,
584
+        $include_tags = 'false'
585
+    ) {
586
+
587
+        if ($path !== null) {
588
+            $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
589
+            try {
590
+                $path = $userFolder->get($path);
591
+                $this->lock($path);
592
+            } catch (\OCP\Files\NotFoundException $e) {
593
+                throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
594
+            } catch (LockedException $e) {
595
+                throw new OCSNotFoundException($this->l->t('Could not lock path'));
596
+            }
597
+        }
598
+
599
+        if ($shared_with_me === 'true') {
600
+            $result = $this->getSharedWithMe($path, $include_tags);
601
+            return $result;
602
+        }
603
+
604
+        if ($subfiles === 'true') {
605
+            $result = $this->getSharesInDir($path);
606
+            return $result;
607
+        }
608
+
609
+        if ($reshares === 'true') {
610
+            $reshares = true;
611
+        } else {
612
+            $reshares = false;
613
+        }
614
+
615
+        // Get all shares
616
+        $userShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
617
+        $groupShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
618
+        $linkShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
619
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
620
+            $mailShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
621
+        } else {
622
+            $mailShares = [];
623
+        }
624
+        if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
625
+            $circleShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
626
+        } else {
627
+            $circleShares = [];
628
+        }
629
+
630
+        $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares);
631
+
632
+        if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
633
+            $federatedShares = $this->shareManager->getSharesBy($this->currentUser, \OCP\Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
634
+            $shares = array_merge($shares, $federatedShares);
635
+        }
636
+
637
+        $formatted = [];
638
+        foreach ($shares as $share) {
639
+            try {
640
+                $formatted[] = $this->formatShare($share, $path);
641
+            } catch (NotFoundException $e) {
642
+                //Ignore share
643
+            }
644
+        }
645
+
646
+        if ($include_tags) {
647
+            $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
648
+        }
649
+
650
+        return new DataResponse($formatted);
651
+    }
652
+
653
+    /**
654
+     * @NoAdminRequired
655
+     *
656
+     * @param int $id
657
+     * @param int $permissions
658
+     * @param string $password
659
+     * @param string $publicUpload
660
+     * @param string $expireDate
661
+     * @return DataResponse
662
+     * @throws OCSNotFoundException
663
+     * @throws OCSBadRequestException
664
+     * @throws OCSForbiddenException
665
+     */
666
+    public function updateShare(
667
+        $id,
668
+        $permissions = null,
669
+        $password = null,
670
+        $publicUpload = null,
671
+        $expireDate = null
672
+    ) {
673
+        try {
674
+            $share = $this->getShareById($id);
675
+        } catch (ShareNotFound $e) {
676
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
677
+        }
678
+
679
+        $this->lock($share->getNode());
680
+
681
+        if (!$this->canAccessShare($share, false)) {
682
+            throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
683
+        }
684
+
685
+        if ($permissions === null && $password === null && $publicUpload === null && $expireDate === null) {
686
+            throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
687
+        }
688
+
689
+        /*
690 690
 		 * expirationdate, password and publicUpload only make sense for link shares
691 691
 		 */
692
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
693
-
694
-			$newPermissions = null;
695
-			if ($publicUpload === 'true') {
696
-				$newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
697
-			} else if ($publicUpload === 'false') {
698
-				$newPermissions = \OCP\Constants::PERMISSION_READ;
699
-			}
700
-
701
-			if ($permissions !== null) {
702
-				$newPermissions = (int)$permissions;
703
-			}
704
-
705
-			if ($newPermissions !== null &&
706
-				!in_array($newPermissions, [
707
-					\OCP\Constants::PERMISSION_READ,
708
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE, // legacy
709
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE, // correct
710
-					\OCP\Constants::PERMISSION_CREATE, // hidden file list
711
-					\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, // allow to edit single files
712
-				])
713
-			) {
714
-				throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
715
-			}
716
-
717
-			if (
718
-				// legacy
719
-				$newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) ||
720
-				// correct
721
-				$newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
722
-			) {
723
-				if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
724
-					throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
725
-				}
726
-
727
-				if (!($share->getNode() instanceof \OCP\Files\Folder)) {
728
-					throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
729
-				}
730
-
731
-				// normalize to correct public upload permissions
732
-				$newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
733
-			}
734
-
735
-			if ($newPermissions !== null) {
736
-				$share->setPermissions($newPermissions);
737
-				$permissions = $newPermissions;
738
-			}
739
-
740
-			if ($expireDate === '') {
741
-				$share->setExpirationDate(null);
742
-			} else if ($expireDate !== null) {
743
-				try {
744
-					$expireDate = $this->parseDate($expireDate);
745
-				} catch (\Exception $e) {
746
-					throw new OCSBadRequestException($e->getMessage());
747
-				}
748
-				$share->setExpirationDate($expireDate);
749
-			}
750
-
751
-			if ($password === '') {
752
-				$share->setPassword(null);
753
-			} else if ($password !== null) {
754
-				$share->setPassword($password);
755
-			}
756
-
757
-		} else {
758
-			if ($permissions !== null) {
759
-				$permissions = (int)$permissions;
760
-				$share->setPermissions($permissions);
761
-			}
762
-
763
-			if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
764
-				if ($password === '') {
765
-					$share->setPassword(null);
766
-				} else if ($password !== null) {
767
-					$share->setPassword($password);
768
-				}
769
-			}
770
-
771
-			if ($expireDate === '') {
772
-				$share->setExpirationDate(null);
773
-			} else if ($expireDate !== null) {
774
-				try {
775
-					$expireDate = $this->parseDate($expireDate);
776
-				} catch (\Exception $e) {
777
-					throw new OCSBadRequestException($e->getMessage());
778
-				}
779
-				$share->setExpirationDate($expireDate);
780
-			}
781
-
782
-		}
783
-
784
-		if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
785
-			/* Check if this is an incomming share */
786
-			$incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
787
-			$incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
788
-
789
-			/** @var \OCP\Share\IShare[] $incomingShares */
790
-			if (!empty($incomingShares)) {
791
-				$maxPermissions = 0;
792
-				foreach ($incomingShares as $incomingShare) {
793
-					$maxPermissions |= $incomingShare->getPermissions();
794
-				}
795
-
796
-				if ($share->getPermissions() & ~$maxPermissions) {
797
-					throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
798
-				}
799
-			}
800
-		}
801
-
802
-
803
-		try {
804
-			$share = $this->shareManager->updateShare($share);
805
-		} catch (\Exception $e) {
806
-			throw new OCSBadRequestException($e->getMessage());
807
-		}
808
-
809
-		return new DataResponse($this->formatShare($share));
810
-	}
811
-
812
-	/**
813
-	 * @param \OCP\Share\IShare $share
814
-	 * @return bool
815
-	 */
816
-	protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) {
817
-		// A file with permissions 0 can't be accessed by us. So Don't show it
818
-		if ($share->getPermissions() === 0) {
819
-			return false;
820
-		}
821
-
822
-		// Owner of the file and the sharer of the file can always get share
823
-		if ($share->getShareOwner() === $this->currentUser ||
824
-			$share->getSharedBy() === $this->currentUser
825
-		) {
826
-			return true;
827
-		}
828
-
829
-		// If the share is shared with you (or a group you are a member of)
830
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
831
-			$share->getSharedWith() === $this->currentUser
832
-		) {
833
-			return true;
834
-		}
835
-
836
-		if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
837
-			$sharedWith = $this->groupManager->get($share->getSharedWith());
838
-			$user = $this->userManager->get($this->currentUser);
839
-			if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
840
-				return true;
841
-			}
842
-		}
843
-
844
-		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
845
-			// TODO: have a sanity check like above?
846
-			return true;
847
-		}
848
-
849
-		return false;
850
-	}
851
-
852
-	/**
853
-	 * Make sure that the passed date is valid ISO 8601
854
-	 * So YYYY-MM-DD
855
-	 * If not throw an exception
856
-	 *
857
-	 * @param string $expireDate
858
-	 *
859
-	 * @throws \Exception
860
-	 * @return \DateTime
861
-	 */
862
-	private function parseDate($expireDate) {
863
-		try {
864
-			$date = new \DateTime($expireDate);
865
-		} catch (\Exception $e) {
866
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
867
-		}
868
-
869
-		if ($date === false) {
870
-			throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
871
-		}
872
-
873
-		$date->setTime(0, 0, 0);
874
-
875
-		return $date;
876
-	}
877
-
878
-	/**
879
-	 * Since we have multiple providers but the OCS Share API v1 does
880
-	 * not support this we need to check all backends.
881
-	 *
882
-	 * @param string $id
883
-	 * @return \OCP\Share\IShare
884
-	 * @throws ShareNotFound
885
-	 */
886
-	private function getShareById($id) {
887
-		$share = null;
888
-
889
-		// First check if it is an internal share.
890
-		try {
891
-			$share = $this->shareManager->getShareById('ocinternal:' . $id);
892
-			return $share;
893
-		} catch (ShareNotFound $e) {
894
-			// Do nothing, just try the other share type
895
-		}
896
-
897
-
898
-		try {
899
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
900
-				$share = $this->shareManager->getShareById('ocCircleShare:' . $id);
901
-				return $share;
902
-			}
903
-		} catch (ShareNotFound $e) {
904
-			// Do nothing, just try the other share type
905
-		}
906
-
907
-		try {
908
-			if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
909
-				$share = $this->shareManager->getShareById('ocMailShare:' . $id);
910
-				return $share;
911
-			}
912
-		} catch (ShareNotFound $e) {
913
-			// Do nothing, just try the other share type
914
-		}
915
-
916
-		if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
917
-			throw new ShareNotFound();
918
-		}
919
-		$share = $this->shareManager->getShareById('ocFederatedSharing:' . $id);
920
-
921
-		return $share;
922
-	}
923
-
924
-	/**
925
-	 * Lock a Node
926
-	 *
927
-	 * @param \OCP\Files\Node $node
928
-	 */
929
-	private function lock(\OCP\Files\Node $node) {
930
-		$node->lock(ILockingProvider::LOCK_SHARED);
931
-		$this->lockedNode = $node;
932
-	}
933
-
934
-	/**
935
-	 * Cleanup the remaining locks
936
-	 */
937
-	public function cleanup() {
938
-		if ($this->lockedNode !== null) {
939
-			$this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
940
-		}
941
-	}
692
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
693
+
694
+            $newPermissions = null;
695
+            if ($publicUpload === 'true') {
696
+                $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
697
+            } else if ($publicUpload === 'false') {
698
+                $newPermissions = \OCP\Constants::PERMISSION_READ;
699
+            }
700
+
701
+            if ($permissions !== null) {
702
+                $newPermissions = (int)$permissions;
703
+            }
704
+
705
+            if ($newPermissions !== null &&
706
+                !in_array($newPermissions, [
707
+                    \OCP\Constants::PERMISSION_READ,
708
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE, // legacy
709
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE, // correct
710
+                    \OCP\Constants::PERMISSION_CREATE, // hidden file list
711
+                    \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, // allow to edit single files
712
+                ])
713
+            ) {
714
+                throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
715
+            }
716
+
717
+            if (
718
+                // legacy
719
+                $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE) ||
720
+                // correct
721
+                $newPermissions === (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)
722
+            ) {
723
+                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
724
+                    throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
725
+                }
726
+
727
+                if (!($share->getNode() instanceof \OCP\Files\Folder)) {
728
+                    throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
729
+                }
730
+
731
+                // normalize to correct public upload permissions
732
+                $newPermissions = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
733
+            }
734
+
735
+            if ($newPermissions !== null) {
736
+                $share->setPermissions($newPermissions);
737
+                $permissions = $newPermissions;
738
+            }
739
+
740
+            if ($expireDate === '') {
741
+                $share->setExpirationDate(null);
742
+            } else if ($expireDate !== null) {
743
+                try {
744
+                    $expireDate = $this->parseDate($expireDate);
745
+                } catch (\Exception $e) {
746
+                    throw new OCSBadRequestException($e->getMessage());
747
+                }
748
+                $share->setExpirationDate($expireDate);
749
+            }
750
+
751
+            if ($password === '') {
752
+                $share->setPassword(null);
753
+            } else if ($password !== null) {
754
+                $share->setPassword($password);
755
+            }
756
+
757
+        } else {
758
+            if ($permissions !== null) {
759
+                $permissions = (int)$permissions;
760
+                $share->setPermissions($permissions);
761
+            }
762
+
763
+            if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
764
+                if ($password === '') {
765
+                    $share->setPassword(null);
766
+                } else if ($password !== null) {
767
+                    $share->setPassword($password);
768
+                }
769
+            }
770
+
771
+            if ($expireDate === '') {
772
+                $share->setExpirationDate(null);
773
+            } else if ($expireDate !== null) {
774
+                try {
775
+                    $expireDate = $this->parseDate($expireDate);
776
+                } catch (\Exception $e) {
777
+                    throw new OCSBadRequestException($e->getMessage());
778
+                }
779
+                $share->setExpirationDate($expireDate);
780
+            }
781
+
782
+        }
783
+
784
+        if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {
785
+            /* Check if this is an incomming share */
786
+            $incomingShares = $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_USER, $share->getNode(), -1, 0);
787
+            $incomingShares = array_merge($incomingShares, $this->shareManager->getSharedWith($this->currentUser, \OCP\Share::SHARE_TYPE_GROUP, $share->getNode(), -1, 0));
788
+
789
+            /** @var \OCP\Share\IShare[] $incomingShares */
790
+            if (!empty($incomingShares)) {
791
+                $maxPermissions = 0;
792
+                foreach ($incomingShares as $incomingShare) {
793
+                    $maxPermissions |= $incomingShare->getPermissions();
794
+                }
795
+
796
+                if ($share->getPermissions() & ~$maxPermissions) {
797
+                    throw new OCSNotFoundException($this->l->t('Cannot increase permissions'));
798
+                }
799
+            }
800
+        }
801
+
802
+
803
+        try {
804
+            $share = $this->shareManager->updateShare($share);
805
+        } catch (\Exception $e) {
806
+            throw new OCSBadRequestException($e->getMessage());
807
+        }
808
+
809
+        return new DataResponse($this->formatShare($share));
810
+    }
811
+
812
+    /**
813
+     * @param \OCP\Share\IShare $share
814
+     * @return bool
815
+     */
816
+    protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) {
817
+        // A file with permissions 0 can't be accessed by us. So Don't show it
818
+        if ($share->getPermissions() === 0) {
819
+            return false;
820
+        }
821
+
822
+        // Owner of the file and the sharer of the file can always get share
823
+        if ($share->getShareOwner() === $this->currentUser ||
824
+            $share->getSharedBy() === $this->currentUser
825
+        ) {
826
+            return true;
827
+        }
828
+
829
+        // If the share is shared with you (or a group you are a member of)
830
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
831
+            $share->getSharedWith() === $this->currentUser
832
+        ) {
833
+            return true;
834
+        }
835
+
836
+        if ($checkGroups && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
837
+            $sharedWith = $this->groupManager->get($share->getSharedWith());
838
+            $user = $this->userManager->get($this->currentUser);
839
+            if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
840
+                return true;
841
+            }
842
+        }
843
+
844
+        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) {
845
+            // TODO: have a sanity check like above?
846
+            return true;
847
+        }
848
+
849
+        return false;
850
+    }
851
+
852
+    /**
853
+     * Make sure that the passed date is valid ISO 8601
854
+     * So YYYY-MM-DD
855
+     * If not throw an exception
856
+     *
857
+     * @param string $expireDate
858
+     *
859
+     * @throws \Exception
860
+     * @return \DateTime
861
+     */
862
+    private function parseDate($expireDate) {
863
+        try {
864
+            $date = new \DateTime($expireDate);
865
+        } catch (\Exception $e) {
866
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
867
+        }
868
+
869
+        if ($date === false) {
870
+            throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
871
+        }
872
+
873
+        $date->setTime(0, 0, 0);
874
+
875
+        return $date;
876
+    }
877
+
878
+    /**
879
+     * Since we have multiple providers but the OCS Share API v1 does
880
+     * not support this we need to check all backends.
881
+     *
882
+     * @param string $id
883
+     * @return \OCP\Share\IShare
884
+     * @throws ShareNotFound
885
+     */
886
+    private function getShareById($id) {
887
+        $share = null;
888
+
889
+        // First check if it is an internal share.
890
+        try {
891
+            $share = $this->shareManager->getShareById('ocinternal:' . $id);
892
+            return $share;
893
+        } catch (ShareNotFound $e) {
894
+            // Do nothing, just try the other share type
895
+        }
896
+
897
+
898
+        try {
899
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) {
900
+                $share = $this->shareManager->getShareById('ocCircleShare:' . $id);
901
+                return $share;
902
+            }
903
+        } catch (ShareNotFound $e) {
904
+            // Do nothing, just try the other share type
905
+        }
906
+
907
+        try {
908
+            if ($this->shareManager->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) {
909
+                $share = $this->shareManager->getShareById('ocMailShare:' . $id);
910
+                return $share;
911
+            }
912
+        } catch (ShareNotFound $e) {
913
+            // Do nothing, just try the other share type
914
+        }
915
+
916
+        if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
917
+            throw new ShareNotFound();
918
+        }
919
+        $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id);
920
+
921
+        return $share;
922
+    }
923
+
924
+    /**
925
+     * Lock a Node
926
+     *
927
+     * @param \OCP\Files\Node $node
928
+     */
929
+    private function lock(\OCP\Files\Node $node) {
930
+        $node->lock(ILockingProvider::LOCK_SHARED);
931
+        $this->lockedNode = $node;
932
+    }
933
+
934
+    /**
935
+     * Cleanup the remaining locks
936
+     */
937
+    public function cleanup() {
938
+        if ($this->lockedNode !== null) {
939
+            $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
940
+        }
941
+    }
942 942
 }
Please login to merge, or discard this patch.