Skip to content

Commit

Permalink
Merge pull request #29 from miklcct/directory-fix
Browse files Browse the repository at this point in the history
check for directory using DAV:iscollection
  • Loading branch information
frankdejonge committed Jan 17, 2018
2 parents d13e3c2 + a28aa98 commit db85f8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
30 changes: 16 additions & 14 deletions src/WebDAVAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Sabre\DAV\Client;
use Sabre\DAV\Exception;
use Sabre\DAV\Exception\NotFound;
use Sabre\HTTP\ClientHttpException;
use Sabre\HTTP\HttpException;

class WebDAVAdapter extends AbstractAdapter
Expand All @@ -23,6 +22,14 @@ class WebDAVAdapter extends AbstractAdapter
}
use NotSupportingVisibilityTrait;

private static $metadataFields = [
'{DAV:}displayname',
'{DAV:}getcontentlength',
'{DAV:}getcontenttype',
'{DAV:}getlastmodified',
'{DAV:}iscollection',
];

/**
* @var array
*/
Expand Down Expand Up @@ -81,12 +88,7 @@ public function getMetadata($path)
$location = $this->applyPathPrefix($this->encodePath($path));

try {
$result = $this->client->propFind($location, [
'{DAV:}displayname',
'{DAV:}getcontentlength',
'{DAV:}getcontenttype',
'{DAV:}getlastmodified',
]);
$result = $this->client->propFind($location, self::$metadataFields);

return $this->normalizeObject($result, $path);
} catch (Exception $e) {
Expand Down Expand Up @@ -276,12 +278,7 @@ public function deleteDir($dirname)
public function listContents($directory = '', $recursive = false)
{
$location = $this->applyPathPrefix($this->encodePath($directory));
$response = $this->client->propFind($location . '/', [
'{DAV:}displayname',
'{DAV:}getcontentlength',
'{DAV:}getcontenttype',
'{DAV:}getlastmodified',
], 1);
$response = $this->client->propFind($location . '/', self::$metadataFields, 1);

array_shift($response);
$result = [];
Expand Down Expand Up @@ -382,7 +379,7 @@ protected function nativeCopy($path, $newPath)
*/
protected function normalizeObject(array $object, $path)
{
if (! isset($object['{DAV:}getcontentlength']) or $object['{DAV:}getcontentlength'] == "") {
if ($this->isDirectory($object)) {
return ['type' => 'dir', 'path' => trim($path, '/')];
}

Expand All @@ -397,4 +394,9 @@ protected function normalizeObject(array $object, $path)

return $result;
}

private function isDirectory(array $object)
{
return isset($object['{DAV:}iscollection']) && $object['{DAV:}iscollection'] === '1';
}
}
11 changes: 8 additions & 3 deletions tests/WebDAVTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,20 @@ public function testListContents()
$first = [
[],
'filename' => [
'{DAV:}getcontentlength' => 20,
'{DAV:}getcontentlength' => "20",
'{DAV:}iscollection' => "0",
],
'dirname' => [
'{DAV:}getcontentlength' => "0",
'{DAV:}iscollection' => "1",
],
'dirname' => [],
];

$second = [
[],
'deeper_filename.ext' => [
'{DAV:}getcontentlength' => 20,
'{DAV:}getcontentlength' => "20",
'{DAV:}iscollection' => "0",
],
];
$mock->shouldReceive('propFind')->twice()->andReturn($first, $second);
Expand Down

0 comments on commit db85f8c

Please sign in to comment.