also use httpclient for uploadFile

This commit is contained in:
Robin Appelman 2015-09-01 18:01:27 +02:00
parent df8cb2cc63
commit 1c10fb5c9f
1 changed files with 18 additions and 35 deletions

View File

@ -78,6 +78,8 @@ class DAV extends Common {
private $statCache; private $statCache;
/** @var array */ /** @var array */
private static $tempFiles = []; private static $tempFiles = [];
/** @var \OCP\Http\Client\IClientService */
private $httpClientService;
/** /**
* @param array $params * @param array $params
@ -85,6 +87,7 @@ class DAV extends Common {
*/ */
public function __construct($params) { public function __construct($params) {
$this->statCache = new ArrayCache(); $this->statCache = new ArrayCache();
$this->httpClientService = \OC::$server->getHTTPClientService();
if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
$host = $params['host']; $host = $params['host'];
//remove leading http[s], will be generated in createBaseUri() //remove leading http[s], will be generated in createBaseUri()
@ -233,7 +236,7 @@ class DAV extends Common {
* If not, request it from the server then store to cache. * If not, request it from the server then store to cache.
* *
* @param string $path path to propfind * @param string $path path to propfind
* *
* @return array propfind response * @return array propfind response
* *
* @throws NotFound * @throws NotFound
@ -338,8 +341,7 @@ class DAV extends Common {
if (!$this->file_exists($path)) { if (!$this->file_exists($path)) {
return false; return false;
} }
$httpClient = \OC::$server->getHTTPClientService(); $response = $this->httpClientService
$response = $httpClient
->newClient() ->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [ ->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password], 'auth' => [$this->user, $this->password],
@ -464,38 +466,19 @@ class DAV extends Common {
*/ */
protected function uploadFile($path, $target) { protected function uploadFile($path, $target) {
$this->init(); $this->init();
// invalidate // invalidate
$target = $this->cleanPath($target); $target = $this->cleanPath($target);
$this->statCache->remove($target); $this->statCache->remove($target);
$source = fopen($path, 'r'); $source = fopen($path, 'r');
$curl = curl_init(); $this->httpClientService
curl_setopt($curl, CURLOPT_USERPWD, $this->user . ':' . $this->password); ->newClient()
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri() . $this->encodePath($target)); ->put($this->createBaseUri() . $this->encodePath($target), [
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); 'body' => $source,
curl_setopt($curl, CURLOPT_INFILE, $source); // file pointer 'auth' => [$this->user, $this->password]
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($path)); ]);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
if ($this->secure === true) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
if ($this->certPath) {
curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
}
}
curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode !== 200) {
Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, Util::ERROR);
if ($statusCode === 423) {
throw new \OCP\Lock\LockedException($path);
}
}
curl_close($curl);
fclose($source);
$this->removeCachedFile($target); $this->removeCachedFile($target);
} }
@ -754,7 +737,7 @@ class DAV extends Common {
if ($e->getHttpStatus() === 404 || $e->getHttpStatus() === 405) { if ($e->getHttpStatus() === 404 || $e->getHttpStatus() === 405) {
if ($path === '') { if ($path === '') {
// if root is gone it means the storage is not available // if root is gone it means the storage is not available
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage()); throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} }
return false; return false;
} }
@ -788,19 +771,19 @@ class DAV extends Common {
} }
if ($e->getHttpStatus() === 401) { if ($e->getHttpStatus() === 401) {
// either password was changed or was invalid all along // either password was changed or was invalid all along
throw new StorageInvalidException(get_class($e).': '.$e->getMessage()); throw new StorageInvalidException(get_class($e) . ': ' . $e->getMessage());
} else if ($e->getHttpStatus() === 405) { } else if ($e->getHttpStatus() === 405) {
// ignore exception for MethodNotAllowed, false will be returned // ignore exception for MethodNotAllowed, false will be returned
return; return;
} }
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage()); throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} else if ($e instanceof ClientException) { } else if ($e instanceof ClientException) {
// connection timeout or refused, server could be temporarily down // connection timeout or refused, server could be temporarily down
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage()); throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} else if ($e instanceof \InvalidArgumentException) { } else if ($e instanceof \InvalidArgumentException) {
// parse error because the server returned HTML instead of XML, // parse error because the server returned HTML instead of XML,
// possibly temporarily down // possibly temporarily down
throw new StorageNotAvailableException(get_class($e).': '.$e->getMessage()); throw new StorageNotAvailableException(get_class($e) . ': ' . $e->getMessage());
} else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) { } else if (($e instanceof StorageNotAvailableException) || ($e instanceof StorageInvalidException)) {
// rethrow // rethrow
throw $e; throw $e;