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