@@ -30,10 +30,10 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | interface IBackendProvider { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @since 9.1.0 |
|
| 35 | - * @return Backend[] |
|
| 36 | - */ |
|
| 37 | - public function getBackends(); |
|
| 33 | + /** |
|
| 34 | + * @since 9.1.0 |
|
| 35 | + * @return Backend[] |
|
| 36 | + */ |
|
| 37 | + public function getBackends(); |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -30,10 +30,10 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | interface IAuthMechanismProvider { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @since 9.1.0 |
|
| 35 | - * @return AuthMechanism[] |
|
| 36 | - */ |
|
| 37 | - public function getAuthMechanisms(); |
|
| 33 | + /** |
|
| 34 | + * @since 9.1.0 |
|
| 35 | + * @return AuthMechanism[] |
|
| 36 | + */ |
|
| 37 | + public function getAuthMechanisms(); |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -31,14 +31,14 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class SessionStorageWrapper extends PermissionsMask { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param array $arguments ['storage' => $storage] |
|
| 36 | - */ |
|
| 37 | - public function __construct($arguments) { |
|
| 38 | - // disable sharing permission |
|
| 39 | - $arguments['mask'] = Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE; |
|
| 40 | - parent::__construct($arguments); |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * @param array $arguments ['storage' => $storage] |
|
| 36 | + */ |
|
| 37 | + public function __construct($arguments) { |
|
| 38 | + // disable sharing permission |
|
| 39 | + $arguments['mask'] = Constants::PERMISSION_ALL & ~Constants::PERMISSION_SHARE; |
|
| 40 | + parent::__construct($arguments); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
@@ -29,15 +29,15 @@ |
||
| 29 | 29 | * Authentication mechanism or backend has insufficient data |
| 30 | 30 | */ |
| 31 | 31 | class InsufficientDataForMeaningfulAnswerException extends StorageNotAvailableException { |
| 32 | - /** |
|
| 33 | - * StorageNotAvailableException constructor. |
|
| 34 | - * |
|
| 35 | - * @param string $message |
|
| 36 | - * @param int $code |
|
| 37 | - * @param \Exception $previous |
|
| 38 | - * @since 6.0.0 |
|
| 39 | - */ |
|
| 40 | - public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
| 41 | - parent::__construct($message, $code, $previous); |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * StorageNotAvailableException constructor. |
|
| 34 | + * |
|
| 35 | + * @param string $message |
|
| 36 | + * @param int $code |
|
| 37 | + * @param \Exception $previous |
|
| 38 | + * @since 6.0.0 |
|
| 39 | + */ |
|
| 40 | + public function __construct($message = '', $code = self::STATUS_INDETERMINATE, \Exception $previous = null) { |
|
| 41 | + parent::__construct($message, $code, $previous); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -42,428 +42,428 @@ |
||
| 42 | 42 | * provide access to SFTP servers. |
| 43 | 43 | */ |
| 44 | 44 | class SFTP extends \OC\Files\Storage\Common { |
| 45 | - private $host; |
|
| 46 | - private $user; |
|
| 47 | - private $root; |
|
| 48 | - private $port = 22; |
|
| 49 | - |
|
| 50 | - private $auth; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var \phpseclib\Net\SFTP |
|
| 54 | - */ |
|
| 55 | - protected $client; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $host protocol://server:port |
|
| 59 | - * @return array [$server, $port] |
|
| 60 | - */ |
|
| 61 | - private function splitHost($host) { |
|
| 62 | - $input = $host; |
|
| 63 | - if (strpos($host, '://') === false) { |
|
| 64 | - // add a protocol to fix parse_url behavior with ipv6 |
|
| 65 | - $host = 'http://' . $host; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $parsed = parse_url($host); |
|
| 69 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
| 70 | - return [$parsed['host'], $parsed['port']]; |
|
| 71 | - } else if (is_array($parsed)) { |
|
| 72 | - return [$parsed['host'], 22]; |
|
| 73 | - } else { |
|
| 74 | - return [$input, 22]; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * {@inheritdoc} |
|
| 80 | - */ |
|
| 81 | - public function __construct($params) { |
|
| 82 | - // Register sftp:// |
|
| 83 | - Stream::register(); |
|
| 84 | - |
|
| 85 | - $parsedHost = $this->splitHost($params['host']); |
|
| 86 | - |
|
| 87 | - $this->host = $parsedHost[0]; |
|
| 88 | - $this->port = $parsedHost[1]; |
|
| 89 | - |
|
| 90 | - if (!isset($params['user'])) { |
|
| 91 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 92 | - } |
|
| 93 | - $this->user = $params['user']; |
|
| 94 | - |
|
| 95 | - if (isset($params['public_key_auth'])) { |
|
| 96 | - $this->auth = $params['public_key_auth']; |
|
| 97 | - } elseif (isset($params['password'])) { |
|
| 98 | - $this->auth = $params['password']; |
|
| 99 | - } else { |
|
| 100 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->root |
|
| 104 | - = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
| 105 | - |
|
| 106 | - if ($this->root[0] != '/') { |
|
| 107 | - $this->root = '/' . $this->root; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if (substr($this->root, -1, 1) != '/') { |
|
| 111 | - $this->root .= '/'; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Returns the connection. |
|
| 117 | - * |
|
| 118 | - * @return \phpseclib\Net\SFTP connected client instance |
|
| 119 | - * @throws \Exception when the connection failed |
|
| 120 | - */ |
|
| 121 | - public function getConnection() { |
|
| 122 | - if (!is_null($this->client)) { |
|
| 123 | - return $this->client; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $hostKeys = $this->readHostKeys(); |
|
| 127 | - $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
| 128 | - |
|
| 129 | - // The SSH Host Key MUST be verified before login(). |
|
| 130 | - $currentHostKey = $this->client->getServerPublicHostKey(); |
|
| 131 | - if (array_key_exists($this->host, $hostKeys)) { |
|
| 132 | - if ($hostKeys[$this->host] != $currentHostKey) { |
|
| 133 | - throw new \Exception('Host public key does not match known key'); |
|
| 134 | - } |
|
| 135 | - } else { |
|
| 136 | - $hostKeys[$this->host] = $currentHostKey; |
|
| 137 | - $this->writeHostKeys($hostKeys); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if (!$this->client->login($this->user, $this->auth)) { |
|
| 141 | - throw new \Exception('Login failed'); |
|
| 142 | - } |
|
| 143 | - return $this->client; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * {@inheritdoc} |
|
| 148 | - */ |
|
| 149 | - public function test() { |
|
| 150 | - if ( |
|
| 151 | - !isset($this->host) |
|
| 152 | - || !isset($this->user) |
|
| 153 | - ) { |
|
| 154 | - return false; |
|
| 155 | - } |
|
| 156 | - return $this->getConnection()->nlist() !== false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * {@inheritdoc} |
|
| 161 | - */ |
|
| 162 | - public function getId(){ |
|
| 163 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
| 164 | - if ($this->port !== 22) { |
|
| 165 | - $id .= ':' . $this->port; |
|
| 166 | - } |
|
| 167 | - // note: this will double the root slash, |
|
| 168 | - // we should not change it to keep compatible with |
|
| 169 | - // old storage ids |
|
| 170 | - $id .= '/' . $this->root; |
|
| 171 | - return $id; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @return string |
|
| 176 | - */ |
|
| 177 | - public function getHost() { |
|
| 178 | - return $this->host; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * @return string |
|
| 183 | - */ |
|
| 184 | - public function getRoot() { |
|
| 185 | - return $this->root; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @return mixed |
|
| 190 | - */ |
|
| 191 | - public function getUser() { |
|
| 192 | - return $this->user; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @param string $path |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - private function absPath($path) { |
|
| 200 | - return $this->root . $this->cleanPath($path); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @return string|false |
|
| 205 | - */ |
|
| 206 | - private function hostKeysPath() { |
|
| 207 | - try { |
|
| 208 | - $storage_view = \OCP\Files::getStorage('files_external'); |
|
| 209 | - if ($storage_view) { |
|
| 210 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
| 211 | - $storage_view->getAbsolutePath('') . |
|
| 212 | - 'ssh_hostKeys'; |
|
| 213 | - } |
|
| 214 | - } catch (\Exception $e) { |
|
| 215 | - } |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @param $keys |
|
| 221 | - * @return bool |
|
| 222 | - */ |
|
| 223 | - protected function writeHostKeys($keys) { |
|
| 224 | - try { |
|
| 225 | - $keyPath = $this->hostKeysPath(); |
|
| 226 | - if ($keyPath && file_exists($keyPath)) { |
|
| 227 | - $fp = fopen($keyPath, 'w'); |
|
| 228 | - foreach ($keys as $host => $key) { |
|
| 229 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
| 230 | - } |
|
| 231 | - fclose($fp); |
|
| 232 | - return true; |
|
| 233 | - } |
|
| 234 | - } catch (\Exception $e) { |
|
| 235 | - } |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @return array |
|
| 241 | - */ |
|
| 242 | - protected function readHostKeys() { |
|
| 243 | - try { |
|
| 244 | - $keyPath = $this->hostKeysPath(); |
|
| 245 | - if (file_exists($keyPath)) { |
|
| 246 | - $hosts = array(); |
|
| 247 | - $keys = array(); |
|
| 248 | - $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
| 249 | - if ($lines) { |
|
| 250 | - foreach ($lines as $line) { |
|
| 251 | - $hostKeyArray = explode("::", $line, 2); |
|
| 252 | - if (count($hostKeyArray) == 2) { |
|
| 253 | - $hosts[] = $hostKeyArray[0]; |
|
| 254 | - $keys[] = $hostKeyArray[1]; |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - return array_combine($hosts, $keys); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - } catch (\Exception $e) { |
|
| 261 | - } |
|
| 262 | - return array(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * {@inheritdoc} |
|
| 267 | - */ |
|
| 268 | - public function mkdir($path) { |
|
| 269 | - try { |
|
| 270 | - return $this->getConnection()->mkdir($this->absPath($path)); |
|
| 271 | - } catch (\Exception $e) { |
|
| 272 | - return false; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * {@inheritdoc} |
|
| 278 | - */ |
|
| 279 | - public function rmdir($path) { |
|
| 280 | - try { |
|
| 281 | - $result = $this->getConnection()->delete($this->absPath($path), true); |
|
| 282 | - // workaround: stray stat cache entry when deleting empty folders |
|
| 283 | - // see https://github.com/phpseclib/phpseclib/issues/706 |
|
| 284 | - $this->getConnection()->clearStatCache(); |
|
| 285 | - return $result; |
|
| 286 | - } catch (\Exception $e) { |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * {@inheritdoc} |
|
| 293 | - */ |
|
| 294 | - public function opendir($path) { |
|
| 295 | - try { |
|
| 296 | - $list = $this->getConnection()->nlist($this->absPath($path)); |
|
| 297 | - if ($list === false) { |
|
| 298 | - return false; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - $id = md5('sftp:' . $path); |
|
| 302 | - $dirStream = array(); |
|
| 303 | - foreach($list as $file) { |
|
| 304 | - if ($file != '.' && $file != '..') { |
|
| 305 | - $dirStream[] = $file; |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - return IteratorDirectory::wrap($dirStream); |
|
| 309 | - } catch(\Exception $e) { |
|
| 310 | - return false; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * {@inheritdoc} |
|
| 316 | - */ |
|
| 317 | - public function filetype($path) { |
|
| 318 | - try { |
|
| 319 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 320 | - if ($stat['type'] == NET_SFTP_TYPE_REGULAR) { |
|
| 321 | - return 'file'; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { |
|
| 325 | - return 'dir'; |
|
| 326 | - } |
|
| 327 | - } catch (\Exception $e) { |
|
| 328 | - |
|
| 329 | - } |
|
| 330 | - return false; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * {@inheritdoc} |
|
| 335 | - */ |
|
| 336 | - public function file_exists($path) { |
|
| 337 | - try { |
|
| 338 | - return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
| 339 | - } catch (\Exception $e) { |
|
| 340 | - return false; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * {@inheritdoc} |
|
| 346 | - */ |
|
| 347 | - public function unlink($path) { |
|
| 348 | - try { |
|
| 349 | - return $this->getConnection()->delete($this->absPath($path), true); |
|
| 350 | - } catch (\Exception $e) { |
|
| 351 | - return false; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * {@inheritdoc} |
|
| 357 | - */ |
|
| 358 | - public function fopen($path, $mode) { |
|
| 359 | - try { |
|
| 360 | - $absPath = $this->absPath($path); |
|
| 361 | - switch($mode) { |
|
| 362 | - case 'r': |
|
| 363 | - case 'rb': |
|
| 364 | - if ( !$this->file_exists($path)) { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - case 'w': |
|
| 368 | - case 'wb': |
|
| 369 | - case 'a': |
|
| 370 | - case 'ab': |
|
| 371 | - case 'r+': |
|
| 372 | - case 'w+': |
|
| 373 | - case 'wb+': |
|
| 374 | - case 'a+': |
|
| 375 | - case 'x': |
|
| 376 | - case 'x+': |
|
| 377 | - case 'c': |
|
| 378 | - case 'c+': |
|
| 379 | - $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
| 380 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 381 | - return RetryWrapper::wrap($handle); |
|
| 382 | - } |
|
| 383 | - } catch (\Exception $e) { |
|
| 384 | - } |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * {@inheritdoc} |
|
| 390 | - */ |
|
| 391 | - public function touch($path, $mtime=null) { |
|
| 392 | - try { |
|
| 393 | - if (!is_null($mtime)) { |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - if (!$this->file_exists($path)) { |
|
| 397 | - $this->getConnection()->put($this->absPath($path), ''); |
|
| 398 | - } else { |
|
| 399 | - return false; |
|
| 400 | - } |
|
| 401 | - } catch (\Exception $e) { |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - return true; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * @param string $path |
|
| 409 | - * @param string $target |
|
| 410 | - * @throws \Exception |
|
| 411 | - */ |
|
| 412 | - public function getFile($path, $target) { |
|
| 413 | - $this->getConnection()->get($path, $target); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @param string $path |
|
| 418 | - * @param string $target |
|
| 419 | - * @throws \Exception |
|
| 420 | - */ |
|
| 421 | - public function uploadFile($path, $target) { |
|
| 422 | - $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * {@inheritdoc} |
|
| 427 | - */ |
|
| 428 | - public function rename($source, $target) { |
|
| 429 | - try { |
|
| 430 | - if ($this->file_exists($target)) { |
|
| 431 | - $this->unlink($target); |
|
| 432 | - } |
|
| 433 | - return $this->getConnection()->rename( |
|
| 434 | - $this->absPath($source), |
|
| 435 | - $this->absPath($target) |
|
| 436 | - ); |
|
| 437 | - } catch (\Exception $e) { |
|
| 438 | - return false; |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * {@inheritdoc} |
|
| 444 | - */ |
|
| 445 | - public function stat($path) { |
|
| 446 | - try { |
|
| 447 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 448 | - |
|
| 449 | - $mtime = $stat ? $stat['mtime'] : -1; |
|
| 450 | - $size = $stat ? $stat['size'] : 0; |
|
| 451 | - |
|
| 452 | - return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
| 453 | - } catch (\Exception $e) { |
|
| 454 | - return false; |
|
| 455 | - } |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * @param string $path |
|
| 460 | - * @return string |
|
| 461 | - */ |
|
| 462 | - public function constructUrl($path) { |
|
| 463 | - // Do not pass the password here. We want to use the Net_SFTP object |
|
| 464 | - // supplied via stream context or fail. We only supply username and |
|
| 465 | - // hostname because this might show up in logs (they are not used). |
|
| 466 | - $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
| 467 | - return $url; |
|
| 468 | - } |
|
| 45 | + private $host; |
|
| 46 | + private $user; |
|
| 47 | + private $root; |
|
| 48 | + private $port = 22; |
|
| 49 | + |
|
| 50 | + private $auth; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var \phpseclib\Net\SFTP |
|
| 54 | + */ |
|
| 55 | + protected $client; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $host protocol://server:port |
|
| 59 | + * @return array [$server, $port] |
|
| 60 | + */ |
|
| 61 | + private function splitHost($host) { |
|
| 62 | + $input = $host; |
|
| 63 | + if (strpos($host, '://') === false) { |
|
| 64 | + // add a protocol to fix parse_url behavior with ipv6 |
|
| 65 | + $host = 'http://' . $host; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $parsed = parse_url($host); |
|
| 69 | + if(is_array($parsed) && isset($parsed['port'])) { |
|
| 70 | + return [$parsed['host'], $parsed['port']]; |
|
| 71 | + } else if (is_array($parsed)) { |
|
| 72 | + return [$parsed['host'], 22]; |
|
| 73 | + } else { |
|
| 74 | + return [$input, 22]; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * {@inheritdoc} |
|
| 80 | + */ |
|
| 81 | + public function __construct($params) { |
|
| 82 | + // Register sftp:// |
|
| 83 | + Stream::register(); |
|
| 84 | + |
|
| 85 | + $parsedHost = $this->splitHost($params['host']); |
|
| 86 | + |
|
| 87 | + $this->host = $parsedHost[0]; |
|
| 88 | + $this->port = $parsedHost[1]; |
|
| 89 | + |
|
| 90 | + if (!isset($params['user'])) { |
|
| 91 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 92 | + } |
|
| 93 | + $this->user = $params['user']; |
|
| 94 | + |
|
| 95 | + if (isset($params['public_key_auth'])) { |
|
| 96 | + $this->auth = $params['public_key_auth']; |
|
| 97 | + } elseif (isset($params['password'])) { |
|
| 98 | + $this->auth = $params['password']; |
|
| 99 | + } else { |
|
| 100 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->root |
|
| 104 | + = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
| 105 | + |
|
| 106 | + if ($this->root[0] != '/') { |
|
| 107 | + $this->root = '/' . $this->root; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if (substr($this->root, -1, 1) != '/') { |
|
| 111 | + $this->root .= '/'; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Returns the connection. |
|
| 117 | + * |
|
| 118 | + * @return \phpseclib\Net\SFTP connected client instance |
|
| 119 | + * @throws \Exception when the connection failed |
|
| 120 | + */ |
|
| 121 | + public function getConnection() { |
|
| 122 | + if (!is_null($this->client)) { |
|
| 123 | + return $this->client; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $hostKeys = $this->readHostKeys(); |
|
| 127 | + $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
| 128 | + |
|
| 129 | + // The SSH Host Key MUST be verified before login(). |
|
| 130 | + $currentHostKey = $this->client->getServerPublicHostKey(); |
|
| 131 | + if (array_key_exists($this->host, $hostKeys)) { |
|
| 132 | + if ($hostKeys[$this->host] != $currentHostKey) { |
|
| 133 | + throw new \Exception('Host public key does not match known key'); |
|
| 134 | + } |
|
| 135 | + } else { |
|
| 136 | + $hostKeys[$this->host] = $currentHostKey; |
|
| 137 | + $this->writeHostKeys($hostKeys); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if (!$this->client->login($this->user, $this->auth)) { |
|
| 141 | + throw new \Exception('Login failed'); |
|
| 142 | + } |
|
| 143 | + return $this->client; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * {@inheritdoc} |
|
| 148 | + */ |
|
| 149 | + public function test() { |
|
| 150 | + if ( |
|
| 151 | + !isset($this->host) |
|
| 152 | + || !isset($this->user) |
|
| 153 | + ) { |
|
| 154 | + return false; |
|
| 155 | + } |
|
| 156 | + return $this->getConnection()->nlist() !== false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * {@inheritdoc} |
|
| 161 | + */ |
|
| 162 | + public function getId(){ |
|
| 163 | + $id = 'sftp::' . $this->user . '@' . $this->host; |
|
| 164 | + if ($this->port !== 22) { |
|
| 165 | + $id .= ':' . $this->port; |
|
| 166 | + } |
|
| 167 | + // note: this will double the root slash, |
|
| 168 | + // we should not change it to keep compatible with |
|
| 169 | + // old storage ids |
|
| 170 | + $id .= '/' . $this->root; |
|
| 171 | + return $id; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @return string |
|
| 176 | + */ |
|
| 177 | + public function getHost() { |
|
| 178 | + return $this->host; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * @return string |
|
| 183 | + */ |
|
| 184 | + public function getRoot() { |
|
| 185 | + return $this->root; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @return mixed |
|
| 190 | + */ |
|
| 191 | + public function getUser() { |
|
| 192 | + return $this->user; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @param string $path |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + private function absPath($path) { |
|
| 200 | + return $this->root . $this->cleanPath($path); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @return string|false |
|
| 205 | + */ |
|
| 206 | + private function hostKeysPath() { |
|
| 207 | + try { |
|
| 208 | + $storage_view = \OCP\Files::getStorage('files_external'); |
|
| 209 | + if ($storage_view) { |
|
| 210 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
| 211 | + $storage_view->getAbsolutePath('') . |
|
| 212 | + 'ssh_hostKeys'; |
|
| 213 | + } |
|
| 214 | + } catch (\Exception $e) { |
|
| 215 | + } |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @param $keys |
|
| 221 | + * @return bool |
|
| 222 | + */ |
|
| 223 | + protected function writeHostKeys($keys) { |
|
| 224 | + try { |
|
| 225 | + $keyPath = $this->hostKeysPath(); |
|
| 226 | + if ($keyPath && file_exists($keyPath)) { |
|
| 227 | + $fp = fopen($keyPath, 'w'); |
|
| 228 | + foreach ($keys as $host => $key) { |
|
| 229 | + fwrite($fp, $host . '::' . $key . "\n"); |
|
| 230 | + } |
|
| 231 | + fclose($fp); |
|
| 232 | + return true; |
|
| 233 | + } |
|
| 234 | + } catch (\Exception $e) { |
|
| 235 | + } |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @return array |
|
| 241 | + */ |
|
| 242 | + protected function readHostKeys() { |
|
| 243 | + try { |
|
| 244 | + $keyPath = $this->hostKeysPath(); |
|
| 245 | + if (file_exists($keyPath)) { |
|
| 246 | + $hosts = array(); |
|
| 247 | + $keys = array(); |
|
| 248 | + $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
| 249 | + if ($lines) { |
|
| 250 | + foreach ($lines as $line) { |
|
| 251 | + $hostKeyArray = explode("::", $line, 2); |
|
| 252 | + if (count($hostKeyArray) == 2) { |
|
| 253 | + $hosts[] = $hostKeyArray[0]; |
|
| 254 | + $keys[] = $hostKeyArray[1]; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + return array_combine($hosts, $keys); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + } catch (\Exception $e) { |
|
| 261 | + } |
|
| 262 | + return array(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * {@inheritdoc} |
|
| 267 | + */ |
|
| 268 | + public function mkdir($path) { |
|
| 269 | + try { |
|
| 270 | + return $this->getConnection()->mkdir($this->absPath($path)); |
|
| 271 | + } catch (\Exception $e) { |
|
| 272 | + return false; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * {@inheritdoc} |
|
| 278 | + */ |
|
| 279 | + public function rmdir($path) { |
|
| 280 | + try { |
|
| 281 | + $result = $this->getConnection()->delete($this->absPath($path), true); |
|
| 282 | + // workaround: stray stat cache entry when deleting empty folders |
|
| 283 | + // see https://github.com/phpseclib/phpseclib/issues/706 |
|
| 284 | + $this->getConnection()->clearStatCache(); |
|
| 285 | + return $result; |
|
| 286 | + } catch (\Exception $e) { |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * {@inheritdoc} |
|
| 293 | + */ |
|
| 294 | + public function opendir($path) { |
|
| 295 | + try { |
|
| 296 | + $list = $this->getConnection()->nlist($this->absPath($path)); |
|
| 297 | + if ($list === false) { |
|
| 298 | + return false; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + $id = md5('sftp:' . $path); |
|
| 302 | + $dirStream = array(); |
|
| 303 | + foreach($list as $file) { |
|
| 304 | + if ($file != '.' && $file != '..') { |
|
| 305 | + $dirStream[] = $file; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + return IteratorDirectory::wrap($dirStream); |
|
| 309 | + } catch(\Exception $e) { |
|
| 310 | + return false; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * {@inheritdoc} |
|
| 316 | + */ |
|
| 317 | + public function filetype($path) { |
|
| 318 | + try { |
|
| 319 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 320 | + if ($stat['type'] == NET_SFTP_TYPE_REGULAR) { |
|
| 321 | + return 'file'; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) { |
|
| 325 | + return 'dir'; |
|
| 326 | + } |
|
| 327 | + } catch (\Exception $e) { |
|
| 328 | + |
|
| 329 | + } |
|
| 330 | + return false; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * {@inheritdoc} |
|
| 335 | + */ |
|
| 336 | + public function file_exists($path) { |
|
| 337 | + try { |
|
| 338 | + return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
| 339 | + } catch (\Exception $e) { |
|
| 340 | + return false; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * {@inheritdoc} |
|
| 346 | + */ |
|
| 347 | + public function unlink($path) { |
|
| 348 | + try { |
|
| 349 | + return $this->getConnection()->delete($this->absPath($path), true); |
|
| 350 | + } catch (\Exception $e) { |
|
| 351 | + return false; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * {@inheritdoc} |
|
| 357 | + */ |
|
| 358 | + public function fopen($path, $mode) { |
|
| 359 | + try { |
|
| 360 | + $absPath = $this->absPath($path); |
|
| 361 | + switch($mode) { |
|
| 362 | + case 'r': |
|
| 363 | + case 'rb': |
|
| 364 | + if ( !$this->file_exists($path)) { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + case 'w': |
|
| 368 | + case 'wb': |
|
| 369 | + case 'a': |
|
| 370 | + case 'ab': |
|
| 371 | + case 'r+': |
|
| 372 | + case 'w+': |
|
| 373 | + case 'wb+': |
|
| 374 | + case 'a+': |
|
| 375 | + case 'x': |
|
| 376 | + case 'x+': |
|
| 377 | + case 'c': |
|
| 378 | + case 'c+': |
|
| 379 | + $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
| 380 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 381 | + return RetryWrapper::wrap($handle); |
|
| 382 | + } |
|
| 383 | + } catch (\Exception $e) { |
|
| 384 | + } |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * {@inheritdoc} |
|
| 390 | + */ |
|
| 391 | + public function touch($path, $mtime=null) { |
|
| 392 | + try { |
|
| 393 | + if (!is_null($mtime)) { |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + if (!$this->file_exists($path)) { |
|
| 397 | + $this->getConnection()->put($this->absPath($path), ''); |
|
| 398 | + } else { |
|
| 399 | + return false; |
|
| 400 | + } |
|
| 401 | + } catch (\Exception $e) { |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + return true; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * @param string $path |
|
| 409 | + * @param string $target |
|
| 410 | + * @throws \Exception |
|
| 411 | + */ |
|
| 412 | + public function getFile($path, $target) { |
|
| 413 | + $this->getConnection()->get($path, $target); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @param string $path |
|
| 418 | + * @param string $target |
|
| 419 | + * @throws \Exception |
|
| 420 | + */ |
|
| 421 | + public function uploadFile($path, $target) { |
|
| 422 | + $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * {@inheritdoc} |
|
| 427 | + */ |
|
| 428 | + public function rename($source, $target) { |
|
| 429 | + try { |
|
| 430 | + if ($this->file_exists($target)) { |
|
| 431 | + $this->unlink($target); |
|
| 432 | + } |
|
| 433 | + return $this->getConnection()->rename( |
|
| 434 | + $this->absPath($source), |
|
| 435 | + $this->absPath($target) |
|
| 436 | + ); |
|
| 437 | + } catch (\Exception $e) { |
|
| 438 | + return false; |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * {@inheritdoc} |
|
| 444 | + */ |
|
| 445 | + public function stat($path) { |
|
| 446 | + try { |
|
| 447 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
| 448 | + |
|
| 449 | + $mtime = $stat ? $stat['mtime'] : -1; |
|
| 450 | + $size = $stat ? $stat['size'] : 0; |
|
| 451 | + |
|
| 452 | + return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
| 453 | + } catch (\Exception $e) { |
|
| 454 | + return false; |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * @param string $path |
|
| 460 | + * @return string |
|
| 461 | + */ |
|
| 462 | + public function constructUrl($path) { |
|
| 463 | + // Do not pass the password here. We want to use the Net_SFTP object |
|
| 464 | + // supplied via stream context or fail. We only supply username and |
|
| 465 | + // hostname because this might show up in logs (they are not used). |
|
| 466 | + $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
| 467 | + return $url; |
|
| 468 | + } |
|
| 469 | 469 | } |
@@ -37,122 +37,122 @@ |
||
| 37 | 37 | use Icewind\Streams\RetryWrapper; |
| 38 | 38 | |
| 39 | 39 | class FTP extends StreamWrapper{ |
| 40 | - private $password; |
|
| 41 | - private $user; |
|
| 42 | - private $host; |
|
| 43 | - private $secure; |
|
| 44 | - private $root; |
|
| 40 | + private $password; |
|
| 41 | + private $user; |
|
| 42 | + private $host; |
|
| 43 | + private $secure; |
|
| 44 | + private $root; |
|
| 45 | 45 | |
| 46 | - private static $tempFiles=array(); |
|
| 46 | + private static $tempFiles=array(); |
|
| 47 | 47 | |
| 48 | - public function __construct($params) { |
|
| 49 | - if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | - $this->host=$params['host']; |
|
| 51 | - $this->user=$params['user']; |
|
| 52 | - $this->password=$params['password']; |
|
| 53 | - if (isset($params['secure'])) { |
|
| 54 | - $this->secure = $params['secure']; |
|
| 55 | - } else { |
|
| 56 | - $this->secure = false; |
|
| 57 | - } |
|
| 58 | - $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | - if ( ! $this->root || $this->root[0]!='/') { |
|
| 60 | - $this->root='/'.$this->root; |
|
| 61 | - } |
|
| 62 | - if (substr($this->root, -1) !== '/') { |
|
| 63 | - $this->root .= '/'; |
|
| 64 | - } |
|
| 65 | - } else { |
|
| 66 | - throw new \Exception('Creating FTP storage failed'); |
|
| 67 | - } |
|
| 48 | + public function __construct($params) { |
|
| 49 | + if (isset($params['host']) && isset($params['user']) && isset($params['password'])) { |
|
| 50 | + $this->host=$params['host']; |
|
| 51 | + $this->user=$params['user']; |
|
| 52 | + $this->password=$params['password']; |
|
| 53 | + if (isset($params['secure'])) { |
|
| 54 | + $this->secure = $params['secure']; |
|
| 55 | + } else { |
|
| 56 | + $this->secure = false; |
|
| 57 | + } |
|
| 58 | + $this->root=isset($params['root'])?$params['root']:'/'; |
|
| 59 | + if ( ! $this->root || $this->root[0]!='/') { |
|
| 60 | + $this->root='/'.$this->root; |
|
| 61 | + } |
|
| 62 | + if (substr($this->root, -1) !== '/') { |
|
| 63 | + $this->root .= '/'; |
|
| 64 | + } |
|
| 65 | + } else { |
|
| 66 | + throw new \Exception('Creating FTP storage failed'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function getId(){ |
|
| 72 | - return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | - } |
|
| 71 | + public function getId(){ |
|
| 72 | + return 'ftp::' . $this->user . '@' . $this->host . '/' . $this->root; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * construct the ftp url |
|
| 77 | - * @param string $path |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public function constructUrl($path) { |
|
| 81 | - $url='ftp'; |
|
| 82 | - if ($this->secure) { |
|
| 83 | - $url.='s'; |
|
| 84 | - } |
|
| 85 | - $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | - return $url; |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * construct the ftp url |
|
| 77 | + * @param string $path |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public function constructUrl($path) { |
|
| 81 | + $url='ftp'; |
|
| 82 | + if ($this->secure) { |
|
| 83 | + $url.='s'; |
|
| 84 | + } |
|
| 85 | + $url.='://'.urlencode($this->user).':'.urlencode($this->password).'@'.$this->host.$this->root.$path; |
|
| 86 | + return $url; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Unlinks file or directory |
|
| 91 | - * @param string $path |
|
| 92 | - */ |
|
| 93 | - public function unlink($path) { |
|
| 94 | - if ($this->is_dir($path)) { |
|
| 95 | - return $this->rmdir($path); |
|
| 96 | - } |
|
| 97 | - else { |
|
| 98 | - $url = $this->constructUrl($path); |
|
| 99 | - $result = unlink($url); |
|
| 100 | - clearstatcache(true, $url); |
|
| 101 | - return $result; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - public function fopen($path,$mode) { |
|
| 105 | - switch($mode) { |
|
| 106 | - case 'r': |
|
| 107 | - case 'rb': |
|
| 108 | - case 'w': |
|
| 109 | - case 'wb': |
|
| 110 | - case 'a': |
|
| 111 | - case 'ab': |
|
| 112 | - //these are supported by the wrapper |
|
| 113 | - $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | - return RetryWrapper::wrap($handle); |
|
| 116 | - case 'r+': |
|
| 117 | - case 'w+': |
|
| 118 | - case 'wb+': |
|
| 119 | - case 'a+': |
|
| 120 | - case 'x': |
|
| 121 | - case 'x+': |
|
| 122 | - case 'c': |
|
| 123 | - case 'c+': |
|
| 124 | - //emulate these |
|
| 125 | - if (strrpos($path, '.')!==false) { |
|
| 126 | - $ext=substr($path, strrpos($path, '.')); |
|
| 127 | - } else { |
|
| 128 | - $ext=''; |
|
| 129 | - } |
|
| 130 | - $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | - if ($this->file_exists($path)) { |
|
| 132 | - $this->getFile($path, $tmpFile); |
|
| 133 | - } |
|
| 134 | - $handle = fopen($tmpFile, $mode); |
|
| 135 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | - $this->writeBack($tmpFile, $path); |
|
| 137 | - }); |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 89 | + /** |
|
| 90 | + * Unlinks file or directory |
|
| 91 | + * @param string $path |
|
| 92 | + */ |
|
| 93 | + public function unlink($path) { |
|
| 94 | + if ($this->is_dir($path)) { |
|
| 95 | + return $this->rmdir($path); |
|
| 96 | + } |
|
| 97 | + else { |
|
| 98 | + $url = $this->constructUrl($path); |
|
| 99 | + $result = unlink($url); |
|
| 100 | + clearstatcache(true, $url); |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + public function fopen($path,$mode) { |
|
| 105 | + switch($mode) { |
|
| 106 | + case 'r': |
|
| 107 | + case 'rb': |
|
| 108 | + case 'w': |
|
| 109 | + case 'wb': |
|
| 110 | + case 'a': |
|
| 111 | + case 'ab': |
|
| 112 | + //these are supported by the wrapper |
|
| 113 | + $context = stream_context_create(array('ftp' => array('overwrite' => true))); |
|
| 114 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
| 115 | + return RetryWrapper::wrap($handle); |
|
| 116 | + case 'r+': |
|
| 117 | + case 'w+': |
|
| 118 | + case 'wb+': |
|
| 119 | + case 'a+': |
|
| 120 | + case 'x': |
|
| 121 | + case 'x+': |
|
| 122 | + case 'c': |
|
| 123 | + case 'c+': |
|
| 124 | + //emulate these |
|
| 125 | + if (strrpos($path, '.')!==false) { |
|
| 126 | + $ext=substr($path, strrpos($path, '.')); |
|
| 127 | + } else { |
|
| 128 | + $ext=''; |
|
| 129 | + } |
|
| 130 | + $tmpFile=\OCP\Files::tmpFile($ext); |
|
| 131 | + if ($this->file_exists($path)) { |
|
| 132 | + $this->getFile($path, $tmpFile); |
|
| 133 | + } |
|
| 134 | + $handle = fopen($tmpFile, $mode); |
|
| 135 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 136 | + $this->writeBack($tmpFile, $path); |
|
| 137 | + }); |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - public function writeBack($tmpFile, $path) { |
|
| 143 | - $this->uploadFile($tmpFile, $path); |
|
| 144 | - unlink($tmpFile); |
|
| 145 | - } |
|
| 142 | + public function writeBack($tmpFile, $path) { |
|
| 143 | + $this->uploadFile($tmpFile, $path); |
|
| 144 | + unlink($tmpFile); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * check if php-ftp is installed |
|
| 149 | - */ |
|
| 150 | - public static function checkDependencies() { |
|
| 151 | - if (function_exists('ftp_login')) { |
|
| 152 | - return(true); |
|
| 153 | - } else { |
|
| 154 | - return array('ftp'); |
|
| 155 | - } |
|
| 156 | - } |
|
| 147 | + /** |
|
| 148 | + * check if php-ftp is installed |
|
| 149 | + */ |
|
| 150 | + public static function checkDependencies() { |
|
| 151 | + if (function_exists('ftp_login')) { |
|
| 152 | + return(true); |
|
| 153 | + } else { |
|
| 154 | + return array('ftp'); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | } |
@@ -30,101 +30,101 @@ |
||
| 30 | 30 | |
| 31 | 31 | abstract class StreamWrapper extends \OC\Files\Storage\Common { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @param string $path |
|
| 35 | - * @return string|null |
|
| 36 | - */ |
|
| 37 | - abstract public function constructUrl($path); |
|
| 38 | - |
|
| 39 | - public function mkdir($path) { |
|
| 40 | - return mkdir($this->constructUrl($path)); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function rmdir($path) { |
|
| 44 | - if ($this->is_dir($path) && $this->isDeletable($path)) { |
|
| 45 | - $dh = $this->opendir($path); |
|
| 46 | - if (!is_resource($dh)) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 49 | - while (($file = readdir($dh)) !== false) { |
|
| 50 | - if ($this->is_dir($path . '/' . $file)) { |
|
| 51 | - $this->rmdir($path . '/' . $file); |
|
| 52 | - } else { |
|
| 53 | - $this->unlink($path . '/' . $file); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - $url = $this->constructUrl($path); |
|
| 57 | - $success = rmdir($url); |
|
| 58 | - clearstatcache(false, $url); |
|
| 59 | - return $success; |
|
| 60 | - } else { |
|
| 61 | - return false; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function opendir($path) { |
|
| 66 | - return opendir($this->constructUrl($path)); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - public function filetype($path) { |
|
| 70 | - return @filetype($this->constructUrl($path)); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function file_exists($path) { |
|
| 74 | - return file_exists($this->constructUrl($path)); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function unlink($path) { |
|
| 78 | - $url = $this->constructUrl($path); |
|
| 79 | - $success = unlink($url); |
|
| 80 | - // normally unlink() is supposed to do this implicitly, |
|
| 81 | - // but doing it anyway just to be sure |
|
| 82 | - clearstatcache(false, $url); |
|
| 83 | - return $success; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function fopen($path, $mode) { |
|
| 87 | - return fopen($this->constructUrl($path), $mode); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function touch($path, $mtime = null) { |
|
| 91 | - if ($this->file_exists($path)) { |
|
| 92 | - if (is_null($mtime)) { |
|
| 93 | - $fh = $this->fopen($path, 'a'); |
|
| 94 | - fwrite($fh, ''); |
|
| 95 | - fclose($fh); |
|
| 96 | - |
|
| 97 | - return true; |
|
| 98 | - } else { |
|
| 99 | - return false; //not supported |
|
| 100 | - } |
|
| 101 | - } else { |
|
| 102 | - $this->file_put_contents($path, ''); |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $path |
|
| 109 | - * @param string $target |
|
| 110 | - */ |
|
| 111 | - public function getFile($path, $target) { |
|
| 112 | - return copy($this->constructUrl($path), $target); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @param string $target |
|
| 117 | - */ |
|
| 118 | - public function uploadFile($path, $target) { |
|
| 119 | - return copy($path, $this->constructUrl($target)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public function rename($path1, $path2) { |
|
| 123 | - return rename($this->constructUrl($path1), $this->constructUrl($path2)); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - public function stat($path) { |
|
| 127 | - return stat($this->constructUrl($path)); |
|
| 128 | - } |
|
| 33 | + /** |
|
| 34 | + * @param string $path |
|
| 35 | + * @return string|null |
|
| 36 | + */ |
|
| 37 | + abstract public function constructUrl($path); |
|
| 38 | + |
|
| 39 | + public function mkdir($path) { |
|
| 40 | + return mkdir($this->constructUrl($path)); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function rmdir($path) { |
|
| 44 | + if ($this->is_dir($path) && $this->isDeletable($path)) { |
|
| 45 | + $dh = $this->opendir($path); |
|
| 46 | + if (!is_resource($dh)) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | + while (($file = readdir($dh)) !== false) { |
|
| 50 | + if ($this->is_dir($path . '/' . $file)) { |
|
| 51 | + $this->rmdir($path . '/' . $file); |
|
| 52 | + } else { |
|
| 53 | + $this->unlink($path . '/' . $file); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + $url = $this->constructUrl($path); |
|
| 57 | + $success = rmdir($url); |
|
| 58 | + clearstatcache(false, $url); |
|
| 59 | + return $success; |
|
| 60 | + } else { |
|
| 61 | + return false; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function opendir($path) { |
|
| 66 | + return opendir($this->constructUrl($path)); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + public function filetype($path) { |
|
| 70 | + return @filetype($this->constructUrl($path)); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function file_exists($path) { |
|
| 74 | + return file_exists($this->constructUrl($path)); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function unlink($path) { |
|
| 78 | + $url = $this->constructUrl($path); |
|
| 79 | + $success = unlink($url); |
|
| 80 | + // normally unlink() is supposed to do this implicitly, |
|
| 81 | + // but doing it anyway just to be sure |
|
| 82 | + clearstatcache(false, $url); |
|
| 83 | + return $success; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function fopen($path, $mode) { |
|
| 87 | + return fopen($this->constructUrl($path), $mode); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function touch($path, $mtime = null) { |
|
| 91 | + if ($this->file_exists($path)) { |
|
| 92 | + if (is_null($mtime)) { |
|
| 93 | + $fh = $this->fopen($path, 'a'); |
|
| 94 | + fwrite($fh, ''); |
|
| 95 | + fclose($fh); |
|
| 96 | + |
|
| 97 | + return true; |
|
| 98 | + } else { |
|
| 99 | + return false; //not supported |
|
| 100 | + } |
|
| 101 | + } else { |
|
| 102 | + $this->file_put_contents($path, ''); |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $path |
|
| 109 | + * @param string $target |
|
| 110 | + */ |
|
| 111 | + public function getFile($path, $target) { |
|
| 112 | + return copy($this->constructUrl($path), $target); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @param string $target |
|
| 117 | + */ |
|
| 118 | + public function uploadFile($path, $target) { |
|
| 119 | + return copy($path, $this->constructUrl($target)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public function rename($path1, $path2) { |
|
| 123 | + return rename($this->constructUrl($path1), $this->constructUrl($path2)); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + public function stat($path) { |
|
| 127 | + return stat($this->constructUrl($path)); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | namespace OCA\Files_External\Lib\Storage; |
| 38 | 38 | |
| 39 | 39 | set_include_path(get_include_path() . PATH_SEPARATOR . |
| 40 | - \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
| 40 | + \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php'); |
|
| 41 | 41 | require_once 'aws-autoloader.php'; |
| 42 | 42 | |
| 43 | 43 | use Aws\S3\S3Client; |
@@ -47,498 +47,498 @@ discard block |
||
| 47 | 47 | use OC\Files\ObjectStore\S3ConnectionTrait; |
| 48 | 48 | |
| 49 | 49 | class AmazonS3 extends \OC\Files\Storage\Common { |
| 50 | - use S3ConnectionTrait; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - private static $tmpFiles = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @var int in seconds |
|
| 59 | - */ |
|
| 60 | - private $rescanDelay = 10; |
|
| 61 | - |
|
| 62 | - public function __construct($parameters) { |
|
| 63 | - parent::__construct($parameters); |
|
| 64 | - $this->parseParams($parameters); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param string $path |
|
| 69 | - * @return string correctly encoded path |
|
| 70 | - */ |
|
| 71 | - private function normalizePath($path) { |
|
| 72 | - $path = trim($path, '/'); |
|
| 73 | - |
|
| 74 | - if (!$path) { |
|
| 75 | - $path = '.'; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return $path; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - private function isRoot($path) { |
|
| 82 | - return $path === '.'; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - private function cleanKey($path) { |
|
| 86 | - if ($this->isRoot($path)) { |
|
| 87 | - return '/'; |
|
| 88 | - } |
|
| 89 | - return $path; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. |
|
| 94 | - * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home |
|
| 95 | - * |
|
| 96 | - * @param array $params |
|
| 97 | - */ |
|
| 98 | - public function updateLegacyId (array $params) { |
|
| 99 | - $oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
| 100 | - |
|
| 101 | - // find by old id or bucket |
|
| 102 | - $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
| 103 | - 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)' |
|
| 104 | - ); |
|
| 105 | - $stmt->execute(array($oldId, $this->id)); |
|
| 106 | - while ($row = $stmt->fetch()) { |
|
| 107 | - $storages[$row['id']] = $row['numeric_id']; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if (isset($storages[$this->id]) && isset($storages[$oldId])) { |
|
| 111 | - // if both ids exist, delete the old storage and corresponding filecache entries |
|
| 112 | - \OC\Files\Cache\Storage::remove($oldId); |
|
| 113 | - } else if (isset($storages[$oldId])) { |
|
| 114 | - // if only the old id exists do an update |
|
| 115 | - $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
| 116 | - 'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?' |
|
| 117 | - ); |
|
| 118 | - $stmt->execute(array($this->id, $oldId)); |
|
| 119 | - } |
|
| 120 | - // only the bucket based id may exist, do nothing |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Remove a file or folder |
|
| 125 | - * |
|
| 126 | - * @param string $path |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - protected function remove($path) { |
|
| 130 | - // remember fileType to reduce http calls |
|
| 131 | - $fileType = $this->filetype($path); |
|
| 132 | - if ($fileType === 'dir') { |
|
| 133 | - return $this->rmdir($path); |
|
| 134 | - } else if ($fileType === 'file') { |
|
| 135 | - return $this->unlink($path); |
|
| 136 | - } else { |
|
| 137 | - return false; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function mkdir($path) { |
|
| 142 | - $path = $this->normalizePath($path); |
|
| 143 | - |
|
| 144 | - if ($this->is_dir($path)) { |
|
| 145 | - return false; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - try { |
|
| 149 | - $this->getConnection()->putObject(array( |
|
| 150 | - 'Bucket' => $this->bucket, |
|
| 151 | - 'Key' => $path . '/', |
|
| 152 | - 'Body' => '', |
|
| 153 | - 'ContentType' => 'httpd/unix-directory' |
|
| 154 | - )); |
|
| 155 | - $this->testTimeout(); |
|
| 156 | - } catch (S3Exception $e) { |
|
| 157 | - \OCP\Util::logException('files_external', $e); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return true; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function file_exists($path) { |
|
| 165 | - return $this->filetype($path) !== false; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - public function rmdir($path) { |
|
| 170 | - $path = $this->normalizePath($path); |
|
| 171 | - |
|
| 172 | - if ($this->isRoot($path)) { |
|
| 173 | - return $this->clearBucket(); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if (!$this->file_exists($path)) { |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $this->batchDelete($path); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - protected function clearBucket() { |
|
| 184 | - try { |
|
| 185 | - $this->getConnection()->clearBucket($this->bucket); |
|
| 186 | - return true; |
|
| 187 | - // clearBucket() is not working with Ceph, so if it fails we try the slower approach |
|
| 188 | - } catch (\Exception $e) { |
|
| 189 | - return $this->batchDelete(); |
|
| 190 | - } |
|
| 191 | - return false; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - private function batchDelete ($path = null) { |
|
| 195 | - $params = array( |
|
| 196 | - 'Bucket' => $this->bucket |
|
| 197 | - ); |
|
| 198 | - if ($path !== null) { |
|
| 199 | - $params['Prefix'] = $path . '/'; |
|
| 200 | - } |
|
| 201 | - try { |
|
| 202 | - // Since there are no real directories on S3, we need |
|
| 203 | - // to delete all objects prefixed with the path. |
|
| 204 | - do { |
|
| 205 | - // instead of the iterator, manually loop over the list ... |
|
| 206 | - $objects = $this->getConnection()->listObjects($params); |
|
| 207 | - // ... so we can delete the files in batches |
|
| 208 | - $this->getConnection()->deleteObjects(array( |
|
| 209 | - 'Bucket' => $this->bucket, |
|
| 210 | - 'Objects' => $objects['Contents'] |
|
| 211 | - )); |
|
| 212 | - $this->testTimeout(); |
|
| 213 | - // we reached the end when the list is no longer truncated |
|
| 214 | - } while ($objects['IsTruncated']); |
|
| 215 | - } catch (S3Exception $e) { |
|
| 216 | - \OCP\Util::logException('files_external', $e); |
|
| 217 | - return false; |
|
| 218 | - } |
|
| 219 | - return true; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - public function opendir($path) { |
|
| 223 | - $path = $this->normalizePath($path); |
|
| 224 | - |
|
| 225 | - if ($this->isRoot($path)) { |
|
| 226 | - $path = ''; |
|
| 227 | - } else { |
|
| 228 | - $path .= '/'; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - try { |
|
| 232 | - $files = array(); |
|
| 233 | - $result = $this->getConnection()->getIterator('ListObjects', array( |
|
| 234 | - 'Bucket' => $this->bucket, |
|
| 235 | - 'Delimiter' => '/', |
|
| 236 | - 'Prefix' => $path |
|
| 237 | - ), array('return_prefixes' => true)); |
|
| 238 | - |
|
| 239 | - foreach ($result as $object) { |
|
| 240 | - if (isset($object['Key']) && $object['Key'] === $path) { |
|
| 241 | - // it's the directory itself, skip |
|
| 242 | - continue; |
|
| 243 | - } |
|
| 244 | - $file = basename( |
|
| 245 | - isset($object['Key']) ? $object['Key'] : $object['Prefix'] |
|
| 246 | - ); |
|
| 247 | - $files[] = $file; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - return IteratorDirectory::wrap($files); |
|
| 251 | - } catch (S3Exception $e) { |
|
| 252 | - \OCP\Util::logException('files_external', $e); |
|
| 253 | - return false; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - public function stat($path) { |
|
| 258 | - $path = $this->normalizePath($path); |
|
| 259 | - |
|
| 260 | - try { |
|
| 261 | - $stat = array(); |
|
| 262 | - if ($this->is_dir($path)) { |
|
| 263 | - //folders don't really exist |
|
| 264 | - $stat['size'] = -1; //unknown |
|
| 265 | - $stat['mtime'] = time() - $this->rescanDelay * 1000; |
|
| 266 | - } else { |
|
| 267 | - $result = $this->getConnection()->headObject(array( |
|
| 268 | - 'Bucket' => $this->bucket, |
|
| 269 | - 'Key' => $path |
|
| 270 | - )); |
|
| 271 | - |
|
| 272 | - $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; |
|
| 273 | - if ($result['Metadata']['lastmodified']) { |
|
| 274 | - $stat['mtime'] = strtotime($result['Metadata']['lastmodified']); |
|
| 275 | - } else { |
|
| 276 | - $stat['mtime'] = strtotime($result['LastModified']); |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - $stat['atime'] = time(); |
|
| 280 | - |
|
| 281 | - return $stat; |
|
| 282 | - } catch(S3Exception $e) { |
|
| 283 | - \OCP\Util::logException('files_external', $e); |
|
| 284 | - return false; |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - public function filetype($path) { |
|
| 289 | - $path = $this->normalizePath($path); |
|
| 290 | - |
|
| 291 | - if ($this->isRoot($path)) { |
|
| 292 | - return 'dir'; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - try { |
|
| 296 | - if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { |
|
| 297 | - return 'file'; |
|
| 298 | - } |
|
| 299 | - if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { |
|
| 300 | - return 'dir'; |
|
| 301 | - } |
|
| 302 | - } catch (S3Exception $e) { |
|
| 303 | - \OCP\Util::logException('files_external', $e); |
|
| 304 | - return false; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - return false; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - public function unlink($path) { |
|
| 311 | - $path = $this->normalizePath($path); |
|
| 312 | - |
|
| 313 | - if ($this->is_dir($path)) { |
|
| 314 | - return $this->rmdir($path); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - try { |
|
| 318 | - $this->getConnection()->deleteObject(array( |
|
| 319 | - 'Bucket' => $this->bucket, |
|
| 320 | - 'Key' => $path |
|
| 321 | - )); |
|
| 322 | - $this->testTimeout(); |
|
| 323 | - } catch (S3Exception $e) { |
|
| 324 | - \OCP\Util::logException('files_external', $e); |
|
| 325 | - return false; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return true; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - public function fopen($path, $mode) { |
|
| 332 | - $path = $this->normalizePath($path); |
|
| 333 | - |
|
| 334 | - switch ($mode) { |
|
| 335 | - case 'r': |
|
| 336 | - case 'rb': |
|
| 337 | - $tmpFile = \OCP\Files::tmpFile(); |
|
| 338 | - self::$tmpFiles[$tmpFile] = $path; |
|
| 339 | - |
|
| 340 | - try { |
|
| 341 | - $this->getConnection()->getObject(array( |
|
| 342 | - 'Bucket' => $this->bucket, |
|
| 343 | - 'Key' => $path, |
|
| 344 | - 'SaveAs' => $tmpFile |
|
| 345 | - )); |
|
| 346 | - } catch (S3Exception $e) { |
|
| 347 | - \OCP\Util::logException('files_external', $e); |
|
| 348 | - return false; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - return fopen($tmpFile, 'r'); |
|
| 352 | - case 'w': |
|
| 353 | - case 'wb': |
|
| 354 | - case 'a': |
|
| 355 | - case 'ab': |
|
| 356 | - case 'r+': |
|
| 357 | - case 'w+': |
|
| 358 | - case 'wb+': |
|
| 359 | - case 'a+': |
|
| 360 | - case 'x': |
|
| 361 | - case 'x+': |
|
| 362 | - case 'c': |
|
| 363 | - case 'c+': |
|
| 364 | - if (strrpos($path, '.') !== false) { |
|
| 365 | - $ext = substr($path, strrpos($path, '.')); |
|
| 366 | - } else { |
|
| 367 | - $ext = ''; |
|
| 368 | - } |
|
| 369 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 370 | - if ($this->file_exists($path)) { |
|
| 371 | - $source = $this->fopen($path, 'r'); |
|
| 372 | - file_put_contents($tmpFile, $source); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - $handle = fopen($tmpFile, $mode); |
|
| 376 | - return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 377 | - $this->writeBack($tmpFile, $path); |
|
| 378 | - }); |
|
| 379 | - } |
|
| 380 | - return false; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - public function touch($path, $mtime = null) { |
|
| 384 | - $path = $this->normalizePath($path); |
|
| 385 | - |
|
| 386 | - $metadata = array(); |
|
| 387 | - if (is_null($mtime)) { |
|
| 388 | - $mtime = time(); |
|
| 389 | - } |
|
| 390 | - $metadata = [ |
|
| 391 | - 'lastmodified' => gmdate(\Aws\Common\Enum\DateFormat::RFC1123, $mtime) |
|
| 392 | - ]; |
|
| 393 | - |
|
| 394 | - $fileType = $this->filetype($path); |
|
| 395 | - try { |
|
| 396 | - if ($fileType !== false) { |
|
| 397 | - if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
| 398 | - $path .= '/'; |
|
| 399 | - } |
|
| 400 | - $this->getConnection()->copyObject([ |
|
| 401 | - 'Bucket' => $this->bucket, |
|
| 402 | - 'Key' => $this->cleanKey($path), |
|
| 403 | - 'Metadata' => $metadata, |
|
| 404 | - 'CopySource' => $this->bucket . '/' . $path, |
|
| 405 | - 'MetadataDirective' => 'REPLACE', |
|
| 406 | - ]); |
|
| 407 | - $this->testTimeout(); |
|
| 408 | - } else { |
|
| 409 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 410 | - $this->getConnection()->putObject([ |
|
| 411 | - 'Bucket' => $this->bucket, |
|
| 412 | - 'Key' => $this->cleanKey($path), |
|
| 413 | - 'Metadata' => $metadata, |
|
| 414 | - 'Body' => '', |
|
| 415 | - 'ContentType' => $mimeType, |
|
| 416 | - 'MetadataDirective' => 'REPLACE', |
|
| 417 | - ]); |
|
| 418 | - $this->testTimeout(); |
|
| 419 | - } |
|
| 420 | - } catch (S3Exception $e) { |
|
| 421 | - \OCP\Util::logException('files_external', $e); |
|
| 422 | - return false; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - return true; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - public function copy($path1, $path2) { |
|
| 429 | - $path1 = $this->normalizePath($path1); |
|
| 430 | - $path2 = $this->normalizePath($path2); |
|
| 431 | - |
|
| 432 | - if ($this->is_file($path1)) { |
|
| 433 | - try { |
|
| 434 | - $this->getConnection()->copyObject(array( |
|
| 435 | - 'Bucket' => $this->bucket, |
|
| 436 | - 'Key' => $this->cleanKey($path2), |
|
| 437 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
| 438 | - )); |
|
| 439 | - $this->testTimeout(); |
|
| 440 | - } catch (S3Exception $e) { |
|
| 441 | - \OCP\Util::logException('files_external', $e); |
|
| 442 | - return false; |
|
| 443 | - } |
|
| 444 | - } else { |
|
| 445 | - $this->remove($path2); |
|
| 446 | - |
|
| 447 | - try { |
|
| 448 | - $this->getConnection()->copyObject(array( |
|
| 449 | - 'Bucket' => $this->bucket, |
|
| 450 | - 'Key' => $path2 . '/', |
|
| 451 | - 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
| 452 | - )); |
|
| 453 | - $this->testTimeout(); |
|
| 454 | - } catch (S3Exception $e) { |
|
| 455 | - \OCP\Util::logException('files_external', $e); |
|
| 456 | - return false; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - $dh = $this->opendir($path1); |
|
| 460 | - if (is_resource($dh)) { |
|
| 461 | - while (($file = readdir($dh)) !== false) { |
|
| 462 | - if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 463 | - continue; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - $source = $path1 . '/' . $file; |
|
| 467 | - $target = $path2 . '/' . $file; |
|
| 468 | - $this->copy($source, $target); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - return true; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - public function rename($path1, $path2) { |
|
| 477 | - $path1 = $this->normalizePath($path1); |
|
| 478 | - $path2 = $this->normalizePath($path2); |
|
| 479 | - |
|
| 480 | - if ($this->is_file($path1)) { |
|
| 481 | - |
|
| 482 | - if ($this->copy($path1, $path2) === false) { |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - if ($this->unlink($path1) === false) { |
|
| 487 | - $this->unlink($path2); |
|
| 488 | - return false; |
|
| 489 | - } |
|
| 490 | - } else { |
|
| 491 | - |
|
| 492 | - if ($this->copy($path1, $path2) === false) { |
|
| 493 | - return false; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - if ($this->rmdir($path1) === false) { |
|
| 497 | - $this->rmdir($path2); |
|
| 498 | - return false; |
|
| 499 | - } |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - return true; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - public function test() { |
|
| 506 | - $test = $this->getConnection()->getBucketAcl(array( |
|
| 507 | - 'Bucket' => $this->bucket, |
|
| 508 | - )); |
|
| 509 | - if (isset($test) && !is_null($test->getPath('Owner/ID'))) { |
|
| 510 | - return true; |
|
| 511 | - } |
|
| 512 | - return false; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - public function getId() { |
|
| 516 | - return $this->id; |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - public function writeBack($tmpFile, $path) { |
|
| 520 | - try { |
|
| 521 | - $this->getConnection()->putObject(array( |
|
| 522 | - 'Bucket' => $this->bucket, |
|
| 523 | - 'Key' => $this->cleanKey($path), |
|
| 524 | - 'SourceFile' => $tmpFile, |
|
| 525 | - 'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile), |
|
| 526 | - 'ContentLength' => filesize($tmpFile) |
|
| 527 | - )); |
|
| 528 | - $this->testTimeout(); |
|
| 529 | - |
|
| 530 | - unlink($tmpFile); |
|
| 531 | - } catch (S3Exception $e) { |
|
| 532 | - \OCP\Util::logException('files_external', $e); |
|
| 533 | - return false; |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * check if curl is installed |
|
| 539 | - */ |
|
| 540 | - public static function checkDependencies() { |
|
| 541 | - return true; |
|
| 542 | - } |
|
| 50 | + use S3ConnectionTrait; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + private static $tmpFiles = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @var int in seconds |
|
| 59 | + */ |
|
| 60 | + private $rescanDelay = 10; |
|
| 61 | + |
|
| 62 | + public function __construct($parameters) { |
|
| 63 | + parent::__construct($parameters); |
|
| 64 | + $this->parseParams($parameters); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param string $path |
|
| 69 | + * @return string correctly encoded path |
|
| 70 | + */ |
|
| 71 | + private function normalizePath($path) { |
|
| 72 | + $path = trim($path, '/'); |
|
| 73 | + |
|
| 74 | + if (!$path) { |
|
| 75 | + $path = '.'; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return $path; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + private function isRoot($path) { |
|
| 82 | + return $path === '.'; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + private function cleanKey($path) { |
|
| 86 | + if ($this->isRoot($path)) { |
|
| 87 | + return '/'; |
|
| 88 | + } |
|
| 89 | + return $path; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name. |
|
| 94 | + * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home |
|
| 95 | + * |
|
| 96 | + * @param array $params |
|
| 97 | + */ |
|
| 98 | + public function updateLegacyId (array $params) { |
|
| 99 | + $oldId = 'amazon::' . $params['key'] . md5($params['secret']); |
|
| 100 | + |
|
| 101 | + // find by old id or bucket |
|
| 102 | + $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
| 103 | + 'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)' |
|
| 104 | + ); |
|
| 105 | + $stmt->execute(array($oldId, $this->id)); |
|
| 106 | + while ($row = $stmt->fetch()) { |
|
| 107 | + $storages[$row['id']] = $row['numeric_id']; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if (isset($storages[$this->id]) && isset($storages[$oldId])) { |
|
| 111 | + // if both ids exist, delete the old storage and corresponding filecache entries |
|
| 112 | + \OC\Files\Cache\Storage::remove($oldId); |
|
| 113 | + } else if (isset($storages[$oldId])) { |
|
| 114 | + // if only the old id exists do an update |
|
| 115 | + $stmt = \OC::$server->getDatabaseConnection()->prepare( |
|
| 116 | + 'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?' |
|
| 117 | + ); |
|
| 118 | + $stmt->execute(array($this->id, $oldId)); |
|
| 119 | + } |
|
| 120 | + // only the bucket based id may exist, do nothing |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Remove a file or folder |
|
| 125 | + * |
|
| 126 | + * @param string $path |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + protected function remove($path) { |
|
| 130 | + // remember fileType to reduce http calls |
|
| 131 | + $fileType = $this->filetype($path); |
|
| 132 | + if ($fileType === 'dir') { |
|
| 133 | + return $this->rmdir($path); |
|
| 134 | + } else if ($fileType === 'file') { |
|
| 135 | + return $this->unlink($path); |
|
| 136 | + } else { |
|
| 137 | + return false; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function mkdir($path) { |
|
| 142 | + $path = $this->normalizePath($path); |
|
| 143 | + |
|
| 144 | + if ($this->is_dir($path)) { |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + try { |
|
| 149 | + $this->getConnection()->putObject(array( |
|
| 150 | + 'Bucket' => $this->bucket, |
|
| 151 | + 'Key' => $path . '/', |
|
| 152 | + 'Body' => '', |
|
| 153 | + 'ContentType' => 'httpd/unix-directory' |
|
| 154 | + )); |
|
| 155 | + $this->testTimeout(); |
|
| 156 | + } catch (S3Exception $e) { |
|
| 157 | + \OCP\Util::logException('files_external', $e); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return true; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function file_exists($path) { |
|
| 165 | + return $this->filetype($path) !== false; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + public function rmdir($path) { |
|
| 170 | + $path = $this->normalizePath($path); |
|
| 171 | + |
|
| 172 | + if ($this->isRoot($path)) { |
|
| 173 | + return $this->clearBucket(); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if (!$this->file_exists($path)) { |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $this->batchDelete($path); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + protected function clearBucket() { |
|
| 184 | + try { |
|
| 185 | + $this->getConnection()->clearBucket($this->bucket); |
|
| 186 | + return true; |
|
| 187 | + // clearBucket() is not working with Ceph, so if it fails we try the slower approach |
|
| 188 | + } catch (\Exception $e) { |
|
| 189 | + return $this->batchDelete(); |
|
| 190 | + } |
|
| 191 | + return false; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + private function batchDelete ($path = null) { |
|
| 195 | + $params = array( |
|
| 196 | + 'Bucket' => $this->bucket |
|
| 197 | + ); |
|
| 198 | + if ($path !== null) { |
|
| 199 | + $params['Prefix'] = $path . '/'; |
|
| 200 | + } |
|
| 201 | + try { |
|
| 202 | + // Since there are no real directories on S3, we need |
|
| 203 | + // to delete all objects prefixed with the path. |
|
| 204 | + do { |
|
| 205 | + // instead of the iterator, manually loop over the list ... |
|
| 206 | + $objects = $this->getConnection()->listObjects($params); |
|
| 207 | + // ... so we can delete the files in batches |
|
| 208 | + $this->getConnection()->deleteObjects(array( |
|
| 209 | + 'Bucket' => $this->bucket, |
|
| 210 | + 'Objects' => $objects['Contents'] |
|
| 211 | + )); |
|
| 212 | + $this->testTimeout(); |
|
| 213 | + // we reached the end when the list is no longer truncated |
|
| 214 | + } while ($objects['IsTruncated']); |
|
| 215 | + } catch (S3Exception $e) { |
|
| 216 | + \OCP\Util::logException('files_external', $e); |
|
| 217 | + return false; |
|
| 218 | + } |
|
| 219 | + return true; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + public function opendir($path) { |
|
| 223 | + $path = $this->normalizePath($path); |
|
| 224 | + |
|
| 225 | + if ($this->isRoot($path)) { |
|
| 226 | + $path = ''; |
|
| 227 | + } else { |
|
| 228 | + $path .= '/'; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + try { |
|
| 232 | + $files = array(); |
|
| 233 | + $result = $this->getConnection()->getIterator('ListObjects', array( |
|
| 234 | + 'Bucket' => $this->bucket, |
|
| 235 | + 'Delimiter' => '/', |
|
| 236 | + 'Prefix' => $path |
|
| 237 | + ), array('return_prefixes' => true)); |
|
| 238 | + |
|
| 239 | + foreach ($result as $object) { |
|
| 240 | + if (isset($object['Key']) && $object['Key'] === $path) { |
|
| 241 | + // it's the directory itself, skip |
|
| 242 | + continue; |
|
| 243 | + } |
|
| 244 | + $file = basename( |
|
| 245 | + isset($object['Key']) ? $object['Key'] : $object['Prefix'] |
|
| 246 | + ); |
|
| 247 | + $files[] = $file; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + return IteratorDirectory::wrap($files); |
|
| 251 | + } catch (S3Exception $e) { |
|
| 252 | + \OCP\Util::logException('files_external', $e); |
|
| 253 | + return false; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + public function stat($path) { |
|
| 258 | + $path = $this->normalizePath($path); |
|
| 259 | + |
|
| 260 | + try { |
|
| 261 | + $stat = array(); |
|
| 262 | + if ($this->is_dir($path)) { |
|
| 263 | + //folders don't really exist |
|
| 264 | + $stat['size'] = -1; //unknown |
|
| 265 | + $stat['mtime'] = time() - $this->rescanDelay * 1000; |
|
| 266 | + } else { |
|
| 267 | + $result = $this->getConnection()->headObject(array( |
|
| 268 | + 'Bucket' => $this->bucket, |
|
| 269 | + 'Key' => $path |
|
| 270 | + )); |
|
| 271 | + |
|
| 272 | + $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0; |
|
| 273 | + if ($result['Metadata']['lastmodified']) { |
|
| 274 | + $stat['mtime'] = strtotime($result['Metadata']['lastmodified']); |
|
| 275 | + } else { |
|
| 276 | + $stat['mtime'] = strtotime($result['LastModified']); |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + $stat['atime'] = time(); |
|
| 280 | + |
|
| 281 | + return $stat; |
|
| 282 | + } catch(S3Exception $e) { |
|
| 283 | + \OCP\Util::logException('files_external', $e); |
|
| 284 | + return false; |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + public function filetype($path) { |
|
| 289 | + $path = $this->normalizePath($path); |
|
| 290 | + |
|
| 291 | + if ($this->isRoot($path)) { |
|
| 292 | + return 'dir'; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + try { |
|
| 296 | + if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { |
|
| 297 | + return 'file'; |
|
| 298 | + } |
|
| 299 | + if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { |
|
| 300 | + return 'dir'; |
|
| 301 | + } |
|
| 302 | + } catch (S3Exception $e) { |
|
| 303 | + \OCP\Util::logException('files_external', $e); |
|
| 304 | + return false; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + return false; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + public function unlink($path) { |
|
| 311 | + $path = $this->normalizePath($path); |
|
| 312 | + |
|
| 313 | + if ($this->is_dir($path)) { |
|
| 314 | + return $this->rmdir($path); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + try { |
|
| 318 | + $this->getConnection()->deleteObject(array( |
|
| 319 | + 'Bucket' => $this->bucket, |
|
| 320 | + 'Key' => $path |
|
| 321 | + )); |
|
| 322 | + $this->testTimeout(); |
|
| 323 | + } catch (S3Exception $e) { |
|
| 324 | + \OCP\Util::logException('files_external', $e); |
|
| 325 | + return false; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return true; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + public function fopen($path, $mode) { |
|
| 332 | + $path = $this->normalizePath($path); |
|
| 333 | + |
|
| 334 | + switch ($mode) { |
|
| 335 | + case 'r': |
|
| 336 | + case 'rb': |
|
| 337 | + $tmpFile = \OCP\Files::tmpFile(); |
|
| 338 | + self::$tmpFiles[$tmpFile] = $path; |
|
| 339 | + |
|
| 340 | + try { |
|
| 341 | + $this->getConnection()->getObject(array( |
|
| 342 | + 'Bucket' => $this->bucket, |
|
| 343 | + 'Key' => $path, |
|
| 344 | + 'SaveAs' => $tmpFile |
|
| 345 | + )); |
|
| 346 | + } catch (S3Exception $e) { |
|
| 347 | + \OCP\Util::logException('files_external', $e); |
|
| 348 | + return false; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + return fopen($tmpFile, 'r'); |
|
| 352 | + case 'w': |
|
| 353 | + case 'wb': |
|
| 354 | + case 'a': |
|
| 355 | + case 'ab': |
|
| 356 | + case 'r+': |
|
| 357 | + case 'w+': |
|
| 358 | + case 'wb+': |
|
| 359 | + case 'a+': |
|
| 360 | + case 'x': |
|
| 361 | + case 'x+': |
|
| 362 | + case 'c': |
|
| 363 | + case 'c+': |
|
| 364 | + if (strrpos($path, '.') !== false) { |
|
| 365 | + $ext = substr($path, strrpos($path, '.')); |
|
| 366 | + } else { |
|
| 367 | + $ext = ''; |
|
| 368 | + } |
|
| 369 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 370 | + if ($this->file_exists($path)) { |
|
| 371 | + $source = $this->fopen($path, 'r'); |
|
| 372 | + file_put_contents($tmpFile, $source); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + $handle = fopen($tmpFile, $mode); |
|
| 376 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 377 | + $this->writeBack($tmpFile, $path); |
|
| 378 | + }); |
|
| 379 | + } |
|
| 380 | + return false; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + public function touch($path, $mtime = null) { |
|
| 384 | + $path = $this->normalizePath($path); |
|
| 385 | + |
|
| 386 | + $metadata = array(); |
|
| 387 | + if (is_null($mtime)) { |
|
| 388 | + $mtime = time(); |
|
| 389 | + } |
|
| 390 | + $metadata = [ |
|
| 391 | + 'lastmodified' => gmdate(\Aws\Common\Enum\DateFormat::RFC1123, $mtime) |
|
| 392 | + ]; |
|
| 393 | + |
|
| 394 | + $fileType = $this->filetype($path); |
|
| 395 | + try { |
|
| 396 | + if ($fileType !== false) { |
|
| 397 | + if ($fileType === 'dir' && ! $this->isRoot($path)) { |
|
| 398 | + $path .= '/'; |
|
| 399 | + } |
|
| 400 | + $this->getConnection()->copyObject([ |
|
| 401 | + 'Bucket' => $this->bucket, |
|
| 402 | + 'Key' => $this->cleanKey($path), |
|
| 403 | + 'Metadata' => $metadata, |
|
| 404 | + 'CopySource' => $this->bucket . '/' . $path, |
|
| 405 | + 'MetadataDirective' => 'REPLACE', |
|
| 406 | + ]); |
|
| 407 | + $this->testTimeout(); |
|
| 408 | + } else { |
|
| 409 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 410 | + $this->getConnection()->putObject([ |
|
| 411 | + 'Bucket' => $this->bucket, |
|
| 412 | + 'Key' => $this->cleanKey($path), |
|
| 413 | + 'Metadata' => $metadata, |
|
| 414 | + 'Body' => '', |
|
| 415 | + 'ContentType' => $mimeType, |
|
| 416 | + 'MetadataDirective' => 'REPLACE', |
|
| 417 | + ]); |
|
| 418 | + $this->testTimeout(); |
|
| 419 | + } |
|
| 420 | + } catch (S3Exception $e) { |
|
| 421 | + \OCP\Util::logException('files_external', $e); |
|
| 422 | + return false; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + return true; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + public function copy($path1, $path2) { |
|
| 429 | + $path1 = $this->normalizePath($path1); |
|
| 430 | + $path2 = $this->normalizePath($path2); |
|
| 431 | + |
|
| 432 | + if ($this->is_file($path1)) { |
|
| 433 | + try { |
|
| 434 | + $this->getConnection()->copyObject(array( |
|
| 435 | + 'Bucket' => $this->bucket, |
|
| 436 | + 'Key' => $this->cleanKey($path2), |
|
| 437 | + 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) |
|
| 438 | + )); |
|
| 439 | + $this->testTimeout(); |
|
| 440 | + } catch (S3Exception $e) { |
|
| 441 | + \OCP\Util::logException('files_external', $e); |
|
| 442 | + return false; |
|
| 443 | + } |
|
| 444 | + } else { |
|
| 445 | + $this->remove($path2); |
|
| 446 | + |
|
| 447 | + try { |
|
| 448 | + $this->getConnection()->copyObject(array( |
|
| 449 | + 'Bucket' => $this->bucket, |
|
| 450 | + 'Key' => $path2 . '/', |
|
| 451 | + 'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') |
|
| 452 | + )); |
|
| 453 | + $this->testTimeout(); |
|
| 454 | + } catch (S3Exception $e) { |
|
| 455 | + \OCP\Util::logException('files_external', $e); |
|
| 456 | + return false; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + $dh = $this->opendir($path1); |
|
| 460 | + if (is_resource($dh)) { |
|
| 461 | + while (($file = readdir($dh)) !== false) { |
|
| 462 | + if (\OC\Files\Filesystem::isIgnoredDir($file)) { |
|
| 463 | + continue; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + $source = $path1 . '/' . $file; |
|
| 467 | + $target = $path2 . '/' . $file; |
|
| 468 | + $this->copy($source, $target); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + return true; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + public function rename($path1, $path2) { |
|
| 477 | + $path1 = $this->normalizePath($path1); |
|
| 478 | + $path2 = $this->normalizePath($path2); |
|
| 479 | + |
|
| 480 | + if ($this->is_file($path1)) { |
|
| 481 | + |
|
| 482 | + if ($this->copy($path1, $path2) === false) { |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + if ($this->unlink($path1) === false) { |
|
| 487 | + $this->unlink($path2); |
|
| 488 | + return false; |
|
| 489 | + } |
|
| 490 | + } else { |
|
| 491 | + |
|
| 492 | + if ($this->copy($path1, $path2) === false) { |
|
| 493 | + return false; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + if ($this->rmdir($path1) === false) { |
|
| 497 | + $this->rmdir($path2); |
|
| 498 | + return false; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + return true; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + public function test() { |
|
| 506 | + $test = $this->getConnection()->getBucketAcl(array( |
|
| 507 | + 'Bucket' => $this->bucket, |
|
| 508 | + )); |
|
| 509 | + if (isset($test) && !is_null($test->getPath('Owner/ID'))) { |
|
| 510 | + return true; |
|
| 511 | + } |
|
| 512 | + return false; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + public function getId() { |
|
| 516 | + return $this->id; |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + public function writeBack($tmpFile, $path) { |
|
| 520 | + try { |
|
| 521 | + $this->getConnection()->putObject(array( |
|
| 522 | + 'Bucket' => $this->bucket, |
|
| 523 | + 'Key' => $this->cleanKey($path), |
|
| 524 | + 'SourceFile' => $tmpFile, |
|
| 525 | + 'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile), |
|
| 526 | + 'ContentLength' => filesize($tmpFile) |
|
| 527 | + )); |
|
| 528 | + $this->testTimeout(); |
|
| 529 | + |
|
| 530 | + unlink($tmpFile); |
|
| 531 | + } catch (S3Exception $e) { |
|
| 532 | + \OCP\Util::logException('files_external', $e); |
|
| 533 | + return false; |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * check if curl is installed |
|
| 539 | + */ |
|
| 540 | + public static function checkDependencies() { |
|
| 541 | + return true; |
|
| 542 | + } |
|
| 543 | 543 | |
| 544 | 544 | } |
@@ -40,317 +40,317 @@ |
||
| 40 | 40 | |
| 41 | 41 | class Dropbox extends \OC\Files\Storage\Common { |
| 42 | 42 | |
| 43 | - private $dropbox; |
|
| 44 | - private $root; |
|
| 45 | - private $id; |
|
| 46 | - private $metaData = array(); |
|
| 47 | - private $oauth; |
|
| 43 | + private $dropbox; |
|
| 44 | + private $root; |
|
| 45 | + private $id; |
|
| 46 | + private $metaData = array(); |
|
| 47 | + private $oauth; |
|
| 48 | 48 | |
| 49 | - public function __construct($params) { |
|
| 50 | - if (isset($params['configured']) && $params['configured'] == 'true' |
|
| 51 | - && isset($params['app_key']) |
|
| 52 | - && isset($params['app_secret']) |
|
| 53 | - && isset($params['token']) |
|
| 54 | - && isset($params['token_secret']) |
|
| 55 | - ) { |
|
| 56 | - $this->root = isset($params['root']) ? $params['root'] : ''; |
|
| 57 | - $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
| 58 | - $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
| 59 | - $this->oauth->setToken($params['token'], $params['token_secret']); |
|
| 60 | - // note: Dropbox_API connection is lazy |
|
| 61 | - $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
| 62 | - } else { |
|
| 63 | - throw new \Exception('Creating Dropbox storage failed'); |
|
| 64 | - } |
|
| 65 | - } |
|
| 49 | + public function __construct($params) { |
|
| 50 | + if (isset($params['configured']) && $params['configured'] == 'true' |
|
| 51 | + && isset($params['app_key']) |
|
| 52 | + && isset($params['app_secret']) |
|
| 53 | + && isset($params['token']) |
|
| 54 | + && isset($params['token_secret']) |
|
| 55 | + ) { |
|
| 56 | + $this->root = isset($params['root']) ? $params['root'] : ''; |
|
| 57 | + $this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root; |
|
| 58 | + $this->oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); |
|
| 59 | + $this->oauth->setToken($params['token'], $params['token_secret']); |
|
| 60 | + // note: Dropbox_API connection is lazy |
|
| 61 | + $this->dropbox = new \Dropbox_API($this->oauth, 'auto'); |
|
| 62 | + } else { |
|
| 63 | + throw new \Exception('Creating Dropbox storage failed'); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param string $path |
|
| 69 | - */ |
|
| 70 | - private function deleteMetaData($path) { |
|
| 71 | - $path = ltrim($this->root.$path, '/'); |
|
| 72 | - if (isset($this->metaData[$path])) { |
|
| 73 | - unset($this->metaData[$path]); |
|
| 74 | - return true; |
|
| 75 | - } |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 67 | + /** |
|
| 68 | + * @param string $path |
|
| 69 | + */ |
|
| 70 | + private function deleteMetaData($path) { |
|
| 71 | + $path = ltrim($this->root.$path, '/'); |
|
| 72 | + if (isset($this->metaData[$path])) { |
|
| 73 | + unset($this->metaData[$path]); |
|
| 74 | + return true; |
|
| 75 | + } |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - private function setMetaData($path, $metaData) { |
|
| 80 | - $this->metaData[ltrim($path, '/')] = $metaData; |
|
| 81 | - } |
|
| 79 | + private function setMetaData($path, $metaData) { |
|
| 80 | + $this->metaData[ltrim($path, '/')] = $metaData; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Returns the path's metadata |
|
| 85 | - * @param string $path path for which to return the metadata |
|
| 86 | - * @param bool $list if true, also return the directory's contents |
|
| 87 | - * @return mixed directory contents if $list is true, file metadata if $list is |
|
| 88 | - * false, null if the file doesn't exist or "false" if the operation failed |
|
| 89 | - */ |
|
| 90 | - private function getDropBoxMetaData($path, $list = false) { |
|
| 91 | - $path = ltrim($this->root.$path, '/'); |
|
| 92 | - if ( ! $list && isset($this->metaData[$path])) { |
|
| 93 | - return $this->metaData[$path]; |
|
| 94 | - } else { |
|
| 95 | - if ($list) { |
|
| 96 | - try { |
|
| 97 | - $response = $this->dropbox->getMetaData($path); |
|
| 98 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 99 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 100 | - } catch (\Exception $exception) { |
|
| 101 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - $contents = array(); |
|
| 105 | - if ($response && isset($response['contents'])) { |
|
| 106 | - // Cache folder's contents |
|
| 107 | - foreach ($response['contents'] as $file) { |
|
| 108 | - if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
| 109 | - $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
| 110 | - $contents[] = $file; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - unset($response['contents']); |
|
| 114 | - } |
|
| 115 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 116 | - $this->setMetaData($path, $response); |
|
| 117 | - } |
|
| 118 | - // Return contents of folder only |
|
| 119 | - return $contents; |
|
| 120 | - } else { |
|
| 121 | - try { |
|
| 122 | - $requestPath = $path; |
|
| 123 | - if ($path === '.') { |
|
| 124 | - $requestPath = ''; |
|
| 125 | - } |
|
| 83 | + /** |
|
| 84 | + * Returns the path's metadata |
|
| 85 | + * @param string $path path for which to return the metadata |
|
| 86 | + * @param bool $list if true, also return the directory's contents |
|
| 87 | + * @return mixed directory contents if $list is true, file metadata if $list is |
|
| 88 | + * false, null if the file doesn't exist or "false" if the operation failed |
|
| 89 | + */ |
|
| 90 | + private function getDropBoxMetaData($path, $list = false) { |
|
| 91 | + $path = ltrim($this->root.$path, '/'); |
|
| 92 | + if ( ! $list && isset($this->metaData[$path])) { |
|
| 93 | + return $this->metaData[$path]; |
|
| 94 | + } else { |
|
| 95 | + if ($list) { |
|
| 96 | + try { |
|
| 97 | + $response = $this->dropbox->getMetaData($path); |
|
| 98 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 99 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 100 | + } catch (\Exception $exception) { |
|
| 101 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + $contents = array(); |
|
| 105 | + if ($response && isset($response['contents'])) { |
|
| 106 | + // Cache folder's contents |
|
| 107 | + foreach ($response['contents'] as $file) { |
|
| 108 | + if (!isset($file['is_deleted']) || !$file['is_deleted']) { |
|
| 109 | + $this->setMetaData($path.'/'.basename($file['path']), $file); |
|
| 110 | + $contents[] = $file; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + unset($response['contents']); |
|
| 114 | + } |
|
| 115 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 116 | + $this->setMetaData($path, $response); |
|
| 117 | + } |
|
| 118 | + // Return contents of folder only |
|
| 119 | + return $contents; |
|
| 120 | + } else { |
|
| 121 | + try { |
|
| 122 | + $requestPath = $path; |
|
| 123 | + if ($path === '.') { |
|
| 124 | + $requestPath = ''; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
| 128 | - if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 129 | - $this->setMetaData($path, $response); |
|
| 130 | - return $response; |
|
| 131 | - } |
|
| 132 | - return null; |
|
| 133 | - } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 134 | - throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 135 | - } catch (\Exception $exception) { |
|
| 136 | - if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
| 137 | - // don't log, might be a file_exist check |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - } |
|
| 127 | + $response = $this->dropbox->getMetaData($requestPath, 'false'); |
|
| 128 | + if (!isset($response['is_deleted']) || !$response['is_deleted']) { |
|
| 129 | + $this->setMetaData($path, $response); |
|
| 130 | + return $response; |
|
| 131 | + } |
|
| 132 | + return null; |
|
| 133 | + } catch (\Dropbox_Exception_Forbidden $e) { |
|
| 134 | + throw new StorageNotAvailableException('Dropbox API rate limit exceeded', StorageNotAvailableException::STATUS_ERROR, $e); |
|
| 135 | + } catch (\Exception $exception) { |
|
| 136 | + if ($exception instanceof \Dropbox_Exception_NotFound) { |
|
| 137 | + // don't log, might be a file_exist check |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - public function getId(){ |
|
| 148 | - return $this->id; |
|
| 149 | - } |
|
| 147 | + public function getId(){ |
|
| 148 | + return $this->id; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - public function mkdir($path) { |
|
| 152 | - $path = $this->root.$path; |
|
| 153 | - try { |
|
| 154 | - $this->dropbox->createFolder($path); |
|
| 155 | - return true; |
|
| 156 | - } catch (\Exception $exception) { |
|
| 157 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - } |
|
| 151 | + public function mkdir($path) { |
|
| 152 | + $path = $this->root.$path; |
|
| 153 | + try { |
|
| 154 | + $this->dropbox->createFolder($path); |
|
| 155 | + return true; |
|
| 156 | + } catch (\Exception $exception) { |
|
| 157 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - public function rmdir($path) { |
|
| 163 | - return $this->unlink($path); |
|
| 164 | - } |
|
| 162 | + public function rmdir($path) { |
|
| 163 | + return $this->unlink($path); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - public function opendir($path) { |
|
| 167 | - $contents = $this->getDropBoxMetaData($path, true); |
|
| 168 | - if ($contents !== false) { |
|
| 169 | - $files = array(); |
|
| 170 | - foreach ($contents as $file) { |
|
| 171 | - $files[] = basename($file['path']); |
|
| 172 | - } |
|
| 173 | - return IteratorDirectory::wrap($files); |
|
| 174 | - } |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 166 | + public function opendir($path) { |
|
| 167 | + $contents = $this->getDropBoxMetaData($path, true); |
|
| 168 | + if ($contents !== false) { |
|
| 169 | + $files = array(); |
|
| 170 | + foreach ($contents as $file) { |
|
| 171 | + $files[] = basename($file['path']); |
|
| 172 | + } |
|
| 173 | + return IteratorDirectory::wrap($files); |
|
| 174 | + } |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - public function stat($path) { |
|
| 179 | - $metaData = $this->getDropBoxMetaData($path); |
|
| 180 | - if ($metaData) { |
|
| 181 | - $stat['size'] = $metaData['bytes']; |
|
| 182 | - $stat['atime'] = time(); |
|
| 183 | - $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
| 184 | - return $stat; |
|
| 185 | - } |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 178 | + public function stat($path) { |
|
| 179 | + $metaData = $this->getDropBoxMetaData($path); |
|
| 180 | + if ($metaData) { |
|
| 181 | + $stat['size'] = $metaData['bytes']; |
|
| 182 | + $stat['atime'] = time(); |
|
| 183 | + $stat['mtime'] = (isset($metaData['modified'])) ? strtotime($metaData['modified']) : time(); |
|
| 184 | + return $stat; |
|
| 185 | + } |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - public function filetype($path) { |
|
| 190 | - if ($path == '' || $path == '/') { |
|
| 191 | - return 'dir'; |
|
| 192 | - } else { |
|
| 193 | - $metaData = $this->getDropBoxMetaData($path); |
|
| 194 | - if ($metaData) { |
|
| 195 | - if ($metaData['is_dir'] == 'true') { |
|
| 196 | - return 'dir'; |
|
| 197 | - } else { |
|
| 198 | - return 'file'; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 189 | + public function filetype($path) { |
|
| 190 | + if ($path == '' || $path == '/') { |
|
| 191 | + return 'dir'; |
|
| 192 | + } else { |
|
| 193 | + $metaData = $this->getDropBoxMetaData($path); |
|
| 194 | + if ($metaData) { |
|
| 195 | + if ($metaData['is_dir'] == 'true') { |
|
| 196 | + return 'dir'; |
|
| 197 | + } else { |
|
| 198 | + return 'file'; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - public function file_exists($path) { |
|
| 206 | - if ($path == '' || $path == '/') { |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 209 | - if ($this->getDropBoxMetaData($path)) { |
|
| 210 | - return true; |
|
| 211 | - } |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 205 | + public function file_exists($path) { |
|
| 206 | + if ($path == '' || $path == '/') { |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | + if ($this->getDropBoxMetaData($path)) { |
|
| 210 | + return true; |
|
| 211 | + } |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - public function unlink($path) { |
|
| 216 | - try { |
|
| 217 | - $this->dropbox->delete($this->root.$path); |
|
| 218 | - $this->deleteMetaData($path); |
|
| 219 | - return true; |
|
| 220 | - } catch (\Exception $exception) { |
|
| 221 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 222 | - return false; |
|
| 223 | - } |
|
| 224 | - } |
|
| 215 | + public function unlink($path) { |
|
| 216 | + try { |
|
| 217 | + $this->dropbox->delete($this->root.$path); |
|
| 218 | + $this->deleteMetaData($path); |
|
| 219 | + return true; |
|
| 220 | + } catch (\Exception $exception) { |
|
| 221 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 222 | + return false; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - public function rename($path1, $path2) { |
|
| 227 | - try { |
|
| 228 | - // overwrite if target file exists and is not a directory |
|
| 229 | - $destMetaData = $this->getDropBoxMetaData($path2); |
|
| 230 | - if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
| 231 | - $this->unlink($path2); |
|
| 232 | - } |
|
| 233 | - $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
| 234 | - $this->deleteMetaData($path1); |
|
| 235 | - return true; |
|
| 236 | - } catch (\Exception $exception) { |
|
| 237 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 238 | - return false; |
|
| 239 | - } |
|
| 240 | - } |
|
| 226 | + public function rename($path1, $path2) { |
|
| 227 | + try { |
|
| 228 | + // overwrite if target file exists and is not a directory |
|
| 229 | + $destMetaData = $this->getDropBoxMetaData($path2); |
|
| 230 | + if (isset($destMetaData) && $destMetaData !== false && !$destMetaData['is_dir']) { |
|
| 231 | + $this->unlink($path2); |
|
| 232 | + } |
|
| 233 | + $this->dropbox->move($this->root.$path1, $this->root.$path2); |
|
| 234 | + $this->deleteMetaData($path1); |
|
| 235 | + return true; |
|
| 236 | + } catch (\Exception $exception) { |
|
| 237 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 238 | + return false; |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - public function copy($path1, $path2) { |
|
| 243 | - $path1 = $this->root.$path1; |
|
| 244 | - $path2 = $this->root.$path2; |
|
| 245 | - try { |
|
| 246 | - $this->dropbox->copy($path1, $path2); |
|
| 247 | - return true; |
|
| 248 | - } catch (\Exception $exception) { |
|
| 249 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - } |
|
| 242 | + public function copy($path1, $path2) { |
|
| 243 | + $path1 = $this->root.$path1; |
|
| 244 | + $path2 = $this->root.$path2; |
|
| 245 | + try { |
|
| 246 | + $this->dropbox->copy($path1, $path2); |
|
| 247 | + return true; |
|
| 248 | + } catch (\Exception $exception) { |
|
| 249 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - public function fopen($path, $mode) { |
|
| 255 | - $path = $this->root.$path; |
|
| 256 | - switch ($mode) { |
|
| 257 | - case 'r': |
|
| 258 | - case 'rb': |
|
| 259 | - try { |
|
| 260 | - // slashes need to stay |
|
| 261 | - $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
| 262 | - $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
| 263 | - $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
| 254 | + public function fopen($path, $mode) { |
|
| 255 | + $path = $this->root.$path; |
|
| 256 | + switch ($mode) { |
|
| 257 | + case 'r': |
|
| 258 | + case 'rb': |
|
| 259 | + try { |
|
| 260 | + // slashes need to stay |
|
| 261 | + $encodedPath = str_replace('%2F', '/', rawurlencode(trim($path, '/'))); |
|
| 262 | + $downloadUrl = 'https://api-content.dropbox.com/1/files/auto/' . $encodedPath; |
|
| 263 | + $headers = $this->oauth->getOAuthHeader($downloadUrl, [], 'GET'); |
|
| 264 | 264 | |
| 265 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 266 | - try { |
|
| 267 | - $response = $client->get($downloadUrl, [ |
|
| 268 | - 'headers' => $headers, |
|
| 269 | - 'stream' => true, |
|
| 270 | - ]); |
|
| 271 | - } catch (RequestException $e) { |
|
| 272 | - if (!is_null($e->getResponse())) { |
|
| 273 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
| 274 | - return false; |
|
| 275 | - } else { |
|
| 276 | - throw $e; |
|
| 277 | - } |
|
| 278 | - } else { |
|
| 279 | - throw $e; |
|
| 280 | - } |
|
| 281 | - } |
|
| 265 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 266 | + try { |
|
| 267 | + $response = $client->get($downloadUrl, [ |
|
| 268 | + 'headers' => $headers, |
|
| 269 | + 'stream' => true, |
|
| 270 | + ]); |
|
| 271 | + } catch (RequestException $e) { |
|
| 272 | + if (!is_null($e->getResponse())) { |
|
| 273 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
| 274 | + return false; |
|
| 275 | + } else { |
|
| 276 | + throw $e; |
|
| 277 | + } |
|
| 278 | + } else { |
|
| 279 | + throw $e; |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - $handle = $response->getBody(); |
|
| 284 | - return RetryWrapper::wrap($handle); |
|
| 285 | - } catch (\Exception $exception) { |
|
| 286 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 287 | - return false; |
|
| 288 | - } |
|
| 289 | - case 'w': |
|
| 290 | - case 'wb': |
|
| 291 | - case 'a': |
|
| 292 | - case 'ab': |
|
| 293 | - case 'r+': |
|
| 294 | - case 'w+': |
|
| 295 | - case 'wb+': |
|
| 296 | - case 'a+': |
|
| 297 | - case 'x': |
|
| 298 | - case 'x+': |
|
| 299 | - case 'c': |
|
| 300 | - case 'c+': |
|
| 301 | - if (strrpos($path, '.') !== false) { |
|
| 302 | - $ext = substr($path, strrpos($path, '.')); |
|
| 303 | - } else { |
|
| 304 | - $ext = ''; |
|
| 305 | - } |
|
| 306 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 307 | - if ($this->file_exists($path)) { |
|
| 308 | - $source = $this->fopen($path, 'r'); |
|
| 309 | - file_put_contents($tmpFile, $source); |
|
| 310 | - } |
|
| 311 | - $handle = fopen($tmpFile, $mode); |
|
| 312 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 313 | - $this->writeBack($tmpFile, $path); |
|
| 314 | - }); |
|
| 315 | - } |
|
| 316 | - return false; |
|
| 317 | - } |
|
| 283 | + $handle = $response->getBody(); |
|
| 284 | + return RetryWrapper::wrap($handle); |
|
| 285 | + } catch (\Exception $exception) { |
|
| 286 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + case 'w': |
|
| 290 | + case 'wb': |
|
| 291 | + case 'a': |
|
| 292 | + case 'ab': |
|
| 293 | + case 'r+': |
|
| 294 | + case 'w+': |
|
| 295 | + case 'wb+': |
|
| 296 | + case 'a+': |
|
| 297 | + case 'x': |
|
| 298 | + case 'x+': |
|
| 299 | + case 'c': |
|
| 300 | + case 'c+': |
|
| 301 | + if (strrpos($path, '.') !== false) { |
|
| 302 | + $ext = substr($path, strrpos($path, '.')); |
|
| 303 | + } else { |
|
| 304 | + $ext = ''; |
|
| 305 | + } |
|
| 306 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 307 | + if ($this->file_exists($path)) { |
|
| 308 | + $source = $this->fopen($path, 'r'); |
|
| 309 | + file_put_contents($tmpFile, $source); |
|
| 310 | + } |
|
| 311 | + $handle = fopen($tmpFile, $mode); |
|
| 312 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 313 | + $this->writeBack($tmpFile, $path); |
|
| 314 | + }); |
|
| 315 | + } |
|
| 316 | + return false; |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - public function writeBack($tmpFile, $path) { |
|
| 320 | - $handle = fopen($tmpFile, 'r'); |
|
| 321 | - try { |
|
| 322 | - $this->dropbox->putFile($path, $handle); |
|
| 323 | - unlink($tmpFile); |
|
| 324 | - $this->deleteMetaData($path); |
|
| 325 | - } catch (\Exception $exception) { |
|
| 326 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 327 | - } |
|
| 328 | - } |
|
| 319 | + public function writeBack($tmpFile, $path) { |
|
| 320 | + $handle = fopen($tmpFile, 'r'); |
|
| 321 | + try { |
|
| 322 | + $this->dropbox->putFile($path, $handle); |
|
| 323 | + unlink($tmpFile); |
|
| 324 | + $this->deleteMetaData($path); |
|
| 325 | + } catch (\Exception $exception) { |
|
| 326 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - public function free_space($path) { |
|
| 331 | - try { |
|
| 332 | - $info = $this->dropbox->getAccountInfo(); |
|
| 333 | - return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
| 334 | - } catch (\Exception $exception) { |
|
| 335 | - \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 336 | - return false; |
|
| 337 | - } |
|
| 338 | - } |
|
| 330 | + public function free_space($path) { |
|
| 331 | + try { |
|
| 332 | + $info = $this->dropbox->getAccountInfo(); |
|
| 333 | + return $info['quota_info']['quota'] - $info['quota_info']['normal']; |
|
| 334 | + } catch (\Exception $exception) { |
|
| 335 | + \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR); |
|
| 336 | + return false; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | 339 | |
| 340 | - public function touch($path, $mtime = null) { |
|
| 341 | - if ($this->file_exists($path)) { |
|
| 342 | - return false; |
|
| 343 | - } else { |
|
| 344 | - $this->file_put_contents($path, ''); |
|
| 345 | - } |
|
| 346 | - return true; |
|
| 347 | - } |
|
| 340 | + public function touch($path, $mtime = null) { |
|
| 341 | + if ($this->file_exists($path)) { |
|
| 342 | + return false; |
|
| 343 | + } else { |
|
| 344 | + $this->file_put_contents($path, ''); |
|
| 345 | + } |
|
| 346 | + return true; |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * check if curl is installed |
|
| 351 | - */ |
|
| 352 | - public static function checkDependencies() { |
|
| 353 | - return true; |
|
| 354 | - } |
|
| 349 | + /** |
|
| 350 | + * check if curl is installed |
|
| 351 | + */ |
|
| 352 | + public static function checkDependencies() { |
|
| 353 | + return true; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | 356 | } |