688fdd49ee
add webdav client change pdf viewer
74 lines
1.6 KiB
PHP
74 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Api\Webdav;
|
|
|
|
use Sabre\DAV\Client as BaseClient;
|
|
|
|
/**
|
|
* class Client.
|
|
*
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
class Client
|
|
{
|
|
protected BaseClient $client;
|
|
protected string $baseUrl;
|
|
|
|
public function __construct(string $server, string $baseUrl, ?string $username, ?string $password)
|
|
{
|
|
$settings = [
|
|
'baseUri' => $server,
|
|
'userName' => $username,
|
|
'password' => $password,
|
|
'authType' => 1,
|
|
];
|
|
|
|
$this->baseUrl = rtrim($baseUrl, '/');
|
|
$this->client = new BaseClient($settings);
|
|
}
|
|
|
|
public function sendFile(string $localFile, string $remoteFile): array
|
|
{
|
|
return $this->client->request(
|
|
'PUT',
|
|
$this->baseUrl.'/'.$remoteFile,
|
|
fopen($localFile, 'r')
|
|
);
|
|
}
|
|
|
|
public function mv(string $source, string $destination): array
|
|
{
|
|
return $this->client->request(
|
|
'MOVE',
|
|
$this->baseUrl.'/'.$source,
|
|
null,
|
|
[
|
|
'Destination' => $this->baseUrl.'/'.$destination,
|
|
]
|
|
);
|
|
}
|
|
|
|
public function mkdir(string $directory): array
|
|
{
|
|
return $this->client->request(
|
|
'MKCOL',
|
|
$this->baseUrl.'/'.$directory
|
|
);
|
|
}
|
|
|
|
public function rm(string $file): array
|
|
{
|
|
return $this->client->request(
|
|
'DELETE',
|
|
$this->baseUrl.'/'.$file
|
|
);
|
|
}
|
|
|
|
public function exists(string $file): bool
|
|
{
|
|
$response = $this->client->request('GET', $this->baseUrl.'/'.$file);
|
|
|
|
return 404 !== $response['statusCode'];
|
|
}
|
|
}
|