diff --git a/src/Api.php b/src/Api.php index 748f9d7..7a1f903 100644 --- a/src/Api.php +++ b/src/Api.php @@ -9,15 +9,27 @@ class Api private $username; private $password; + /** + * @var array for additional curlSettings in \Sabre\DAV\Client + */ + private $fileManagementConfig; + + /** + * @var Api\FileManagement + */ + private $fileManagement; + public function __construct($host, $username, $password, $config = array()) { $this->host = $host; $this->username = $username; $this->password = $password; - + $this->fileManagementConfig = $config['fileManagementConfig']; $config['base_url'] = $host; $config['defaults']['auth'] = [$username, $password]; $this->client = new Client($config); + + } public function fileSharing() @@ -25,8 +37,40 @@ public function fileSharing() return new Api\FileSharing($this->client); } - public function fileManagement() + public function fileManagement(): Api\FileManagement + { + if($this->fileManagement){ + return $this->fileManagement; + } + return $this->fileManagement = new Api\FileManagement($this->host, $this->username, $this->password, $this->fileManagementConfig); + + } + + public function listContents(string $path): array + { + return $this->fileManagement()->listContents($path); + } + + public function has(string $path): bool + { + return $this->fileManagement()->has($path); + } + + /** + * @throws \League\Flysystem\FileNotFoundException + */ + public function delete(string $filePath): bool { - return new Api\FileManagement($this->host, $this->username, $this->password); + return $this->fileManagement()->delete($filePath); } + + public function getFileContent(string $filePath) + { + if($handler = $this->fileManagement()->get($filePath)){ + return $handler->read(); + } + return ''; + + } + } diff --git a/src/Api/FileManagement.php b/src/Api/FileManagement.php index dd6f5e0..2d0e2ab 100644 --- a/src/Api/FileManagement.php +++ b/src/Api/FileManagement.php @@ -2,20 +2,48 @@ namespace Owncloud\Api; +use League\Flysystem\Adapter\WebDav; +use League\Flysystem\Filesystem; +use Sabre\DAV\Client; + /** * @author Gustavo Pilla */ -class FileManagement extends \League\Flysystem\Filesystem +class FileManagement extends Filesystem { - public function __construct($host, $username, $password, $settings = array()) + /** + * @param $host string + * @param $username string + * @param $password string + * @param $settings array ['curlSettings' => [],'pathRemoteWebDav' => 'wweebb'] + */ + public function __construct( + $host, + $username, + $password, + $settings = array() + ) { $settings['baseUri'] = $host; $settings['userName'] = $username; $settings['password'] = $password; - $client = new \Sabre\DAV\Client($settings); - $adapter = new \League\Flysystem\Adapter\WebDav($client, 'remote.php/webdav/'); + $client = new Client($settings); + + /** load curl settings */ + if (isset($settings['curlSettings'])) { + foreach ($settings['curlSettings'] as $curlSettingName => $curlSettingValue) { + $client->addCurlSetting($curlSettingName, $curlSettingValue); + } + } + + /** set from setting WebDav path */ + if (isset($settings['pathRemoteWebDav'])) { + $adapter = new WebDav($client, $settings['pathRemoteWebDav']); + } else { + $adapter = new WebDav($client, 'remote.php/webdav/'); + } parent::__construct($adapter); }