Skip to content

Commit 09b11a2

Browse files
committed
Be more strict on 'fileExists' and 'directoryExists'.
Remove basename from the attributes. Remove 'directory' mimetype.
1 parent 8dfba7e commit 09b11a2

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

‎src/Nextcloud.php‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ final public function deleteDirectory(string $dirname): void
9292
}
9393

9494
/**
95-
* Get all the meta data of a file or directory.
95+
* Get all the meta data of a file
9696
*
9797
* @param string $path
9898
*
@@ -170,7 +170,12 @@ final public function visibility(string $path): FileAttributes
170170
*/
171171
final public function fileExists(string $path): bool
172172
{
173-
return $this->folder->nodeExists($path);
173+
try {
174+
$node = $this->folder->get($path);
175+
return !$this->isDirectory($node);
176+
} catch (\OCP\Files\NotFoundException $exception) {
177+
return false;
178+
}
174179
}
175180

176181
/**
@@ -182,7 +187,12 @@ final public function fileExists(string $path): bool
182187
*/
183188
final public function directoryExists(string $path): bool
184189
{
185-
return $this->folder->nodeExists($path);
190+
try {
191+
$node = $this->folder->get($path);
192+
return $this->isDirectory($node);
193+
} catch (\OCP\Files\NotFoundException $exception) {
194+
return false;
195+
}
186196
}
187197

188198
/**
@@ -384,10 +394,9 @@ private function isDirectory(\OCP\Files\Node $node)
384394
private function normalizeNodeInfo(\OCP\Files\Node $node, array $metaData = []) : array
385395
{
386396
return array_merge([
387-
'mimetype' => $this->isDirectory($node) ? "directory" : $node->getMimetype(),
397+
'mimetype' => $node->getMimetype(),
388398
'path' => substr($node->getPath(), strlen($this->folder->getPath())+1),
389399
'size' => $node->getSize(),
390-
'basename' => basename($node->getPath()),
391400
'timestamp' => $node->getMTime(),
392401
'type' => $node->getType(),
393402
// @FIXME: Use $node->getPermissions() to set private or public

‎src/NextcloudCalendar.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private function getCalendarId($path) {
127127
}
128128

129129
/**
130-
* Get all the meta data of a file or directory.
130+
* Get all the meta data of a file.
131131
*
132132
* @param string $path
133133
*
@@ -217,7 +217,7 @@ final public function fileExists(string $path): bool
217217

218218
$calendarId = $this->getCalendarId($path);
219219
if ($calendarId !== null) {
220-
return true;
220+
return false;
221221
} else {
222222
list($calendar, $filename) = $this->splitPath($path);
223223
$calendarId = $this->getCalendarId($calendar);
@@ -229,7 +229,11 @@ final public function fileExists(string $path): bool
229229

230230
final public function directoryExists(string $path): bool
231231
{
232-
return $this->fileExists($path);
232+
$calendarId = $this->getCalendarId($path);
233+
if ($calendarId !== null) {
234+
return true;
235+
}
236+
return false;
233237
}
234238

235239
/**
@@ -337,7 +341,6 @@ final public function readStream(string $path): void
337341

338342
private function normalizeAcl($acl) {
339343
return [
340-
'basename' => '.acl',
341344
'contents' => $acl,
342345
'mimetype' => 'text/turtle',
343346
'path' => '.acl',
@@ -354,7 +357,6 @@ private function normalizeCalendarItem($calendarItem, $basePath) {
354357
}
355358

356359
return [
357-
'basename' => $calendarItem['uri'],
358360
'contents' => $calendarItem['calendardata'],
359361
'mimetype' => 'text/calendar',
360362
'path' => $basePath . '/' . $calendarItem['uri'],
@@ -368,8 +370,6 @@ private function normalizeCalendarItem($calendarItem, $basePath) {
368370
private function normalizeCalendar($calendar)
369371
{
370372
return [
371-
'basename' => basename($calendar['uri']),
372-
'mimetype' => 'directory',
373373
'path' => $calendar['uri'],
374374
'size' => 0,
375375
'timestamp' => 0,

‎src/NextcloudContacts.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ final public function fileExists(string $path): bool
216216

217217
$addressBookId = $this->getAddressBookId($path);
218218
if ($addressBookId !== null) {
219-
return true;
219+
return false;
220220
} else {
221221
list($addressBook, $filename) = $this->splitPath($path);
222222
$addressBookId = $this->getAddressBookId($addressBook);
@@ -235,7 +235,11 @@ final public function fileExists(string $path): bool
235235
*/
236236
final public function directoryExists(string $path): bool
237237
{
238-
return $this->fileExists($path);
238+
$addressBookId = $this->getAddressBookId($path);
239+
if ($addressBookId !== null) {
240+
return true;
241+
}
242+
return false;
239243
}
240244

241245
/**
@@ -348,7 +352,6 @@ final public function readStream(string $path): void
348352

349353
private function normalizeAcl($acl) {
350354
return [
351-
'basename' => '.acl',
352355
'contents' => $acl,
353356
'mimetype' => 'text/turtle',
354357
'path' => '.acl',
@@ -365,7 +368,6 @@ private function normalizeCard($card, $basePath) {
365368
}
366369

367370
return [
368-
'basename' => $card['uri'],
369371
'contents' => $card['carddata'],
370372
'mimetype' => 'text/vcard',
371373
'path' => $basePath . '/' . $card['uri'],
@@ -379,8 +381,6 @@ private function normalizeCard($card, $basePath) {
379381
private function normalizeAddressBook($addressBook)
380382
{
381383
return [
382-
'basename' => basename($addressBook['uri']),
383-
'mimetype' => 'directory',
384384
'path' => $addressBook['uri'],
385385
'size' => 0,
386386
'timestamp' => 0,

‎src/NextcloudProfile.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ final public function fileExists(string $path): bool
169169
*/
170170
final public function directoryExists(string $path): bool
171171
{
172-
return $this->fileExists($path);
172+
if ($path === '/') {
173+
return true;
174+
}
175+
return false;
173176
}
174177

175178
/**
@@ -253,7 +256,6 @@ final public function readStream(string $path): void
253256

254257
private function normalizeAcl($acl) {
255258
return [
256-
'basename' => '.acl',
257259
'contents' => $acl,
258260
'mimetype' => 'text/turtle',
259261
'path' => '.acl',
@@ -267,7 +269,6 @@ private function normalizeAcl($acl) {
267269
private function normalizeProfile() {
268270
$profile = $this->profile;
269271
return [
270-
'basename' => 'card',
271272
'contents' => $profile,
272273
'mimetype' => 'text/turtle',
273274
'path' => "card",

0 commit comments

Comments
 (0)