Blame view

sources/apps/files_external/lib/amazons3.php 13.1 KB
03e52840d   Kload   Init
1
2
3
4
5
6
  <?php
  
  /**
   * ownCloud
   *
   * @author Michael Gapczynski
31b7f2792   Kload   Upgrade to ownclo...
7
   * @author Christian Berendt
03e52840d   Kload   Init
8
   * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
31b7f2792   Kload   Upgrade to ownclo...
9
   * @copyright 2013 Christian Berendt berendt@b1-systems.de
03e52840d   Kload   Init
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  namespace OC\Files\Storage;
31b7f2792   Kload   Upgrade to ownclo...
26
  set_include_path(get_include_path() . PATH_SEPARATOR .
6d9380f96   Cédric Dupont   Update sources OC...
27
  	\OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php');
31b7f2792   Kload   Upgrade to ownclo...
28
29
30
31
  require 'aws-autoloader.php';
  
  use Aws\S3\S3Client;
  use Aws\S3\Exception\S3Exception;
03e52840d   Kload   Init
32
33
  
  class AmazonS3 extends \OC\Files\Storage\Common {
31b7f2792   Kload   Upgrade to ownclo...
34
35
36
37
38
39
40
  	/**
  	 * @var \Aws\S3\S3Client
  	 */
  	private $connection;
  	/**
  	 * @var string
  	 */
03e52840d   Kload   Init
41
  	private $bucket;
31b7f2792   Kload   Upgrade to ownclo...
42
43
44
45
46
47
48
49
50
51
52
53
  	/**
  	 * @var array
  	 */
  	private static $tmpFiles = array();
  	/**
  	 * @var bool
  	 */
  	private $test = false;
  	/**
  	 * @var int
  	 */
  	private $timeout = 15;
03e52840d   Kload   Init
54

6d9380f96   Cédric Dupont   Update sources OC...
55
56
57
  	/**
  	 * @param string $path
  	 */
31b7f2792   Kload   Upgrade to ownclo...
58
59
  	private function normalizePath($path) {
  		$path = trim($path, '/');
03e52840d   Kload   Init
60

31b7f2792   Kload   Upgrade to ownclo...
61
62
63
  		if (!$path) {
  			$path = '.';
  		}
03e52840d   Kload   Init
64

31b7f2792   Kload   Upgrade to ownclo...
65
66
67
68
69
70
  		return $path;
  	}
  
  	private function testTimeout() {
  		if ($this->test) {
  			sleep($this->timeout);
03e52840d   Kload   Init
71
72
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
73
74
75
76
77
78
  	private function cleanKey($path) {
  		if ($path === '.') {
  			return '/';
  		}
  		return $path;
  	}
03e52840d   Kload   Init
79

31b7f2792   Kload   Upgrade to ownclo...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	public function __construct($params) {
  		if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) {
  			throw new \Exception("Access Key, Secret and Bucket have to be configured.");
  		}
  
  		$this->id = 'amazon::' . $params['key'] . md5($params['secret']);
  
  		$this->bucket = $params['bucket'];
  		$scheme = ($params['use_ssl'] === 'false') ? 'http' : 'https';
  		$this->test = isset($params['test']);
  		$this->timeout = ( ! isset($params['timeout'])) ? 15 : $params['timeout'];
  		$params['region'] = ( ! isset($params['region']) || $params['region'] === '' ) ? 'eu-west-1' : $params['region'];
  		$params['hostname'] = ( !isset($params['hostname']) || $params['hostname'] === '' ) ? 's3.amazonaws.com' : $params['hostname'];
  		if (!isset($params['port']) || $params['port'] === '') {
  			$params['port'] = ($params['use_ssl'] === 'false') ? 80 : 443;
  		}
  		$base_url = $scheme.'://'.$params['hostname'].':'.$params['port'].'/';
  
  		$this->connection = S3Client::factory(array(
  			'key' => $params['key'],
  			'secret' => $params['secret'],
  			'base_url' => $base_url,
  			'region' => $params['region']
  		));
  
  		if (!$this->connection->isValidBucketName($this->bucket)) {
  			throw new \Exception("The configured bucket name is invalid.");
  		}
  
  		if (!$this->connection->doesBucketExist($this->bucket)) {
  			try {
  				$result = $this->connection->createBucket(array(
  					'Bucket' => $this->bucket
  				));
  				$this->connection->waitUntilBucketExists(array(
  					'Bucket' => $this->bucket,
  					'waiter.interval' => 1,
  					'waiter.max_attempts' => 15
  				));
  			$this->testTimeout();
  			} catch (S3Exception $e) {
  				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  				throw new \Exception("Creation of bucket failed.");
03e52840d   Kload   Init
123
124
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
125
126
127
  		if (!$this->file_exists('.')) {
  			$result = $this->connection->putObject(array(
  				'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
128
  				'Key'    => $this->cleanKey('.'),
31b7f2792   Kload   Upgrade to ownclo...
129
130
131
132
133
134
  				'Body'   => '',
  				'ContentType' => 'httpd/unix-directory',
  				'ContentLength' => 0
  			));
  			$this->testTimeout();
  		}
03e52840d   Kload   Init
135
136
137
  	}
  
  	public function mkdir($path) {
31b7f2792   Kload   Upgrade to ownclo...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  		$path = $this->normalizePath($path);
  
  		if ($this->is_dir($path)) {
  			return false;
  		}
  
  		try {
  			$result = $this->connection->putObject(array(
  				'Bucket' => $this->bucket,
  				'Key'    => $path . '/',
  				'Body'   => '',
  				'ContentType' => 'httpd/unix-directory',
  				'ContentLength' => 0
  			));
  			$this->testTimeout();
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
  		}
  
  		return true;
  	}
  
  	public function file_exists($path) {
  		$path = $this->normalizePath($path);
  
  		if (!$path) {
  			$path = '.';
  		} else if ($path != '.' && $this->is_dir($path)) {
03e52840d   Kload   Init
167
168
  			$path .= '/';
  		}
31b7f2792   Kload   Upgrade to ownclo...
169
170
171
172
  
  		try {
  			$result = $this->connection->doesObjectExist(
  				$this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
173
  				$this->cleanKey($path)
31b7f2792   Kload   Upgrade to ownclo...
174
175
176
177
178
179
180
  			);
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
  		}
  
  		return $result;
03e52840d   Kload   Init
181
  	}
31b7f2792   Kload   Upgrade to ownclo...
182

03e52840d   Kload   Init
183
  	public function rmdir($path) {
31b7f2792   Kload   Upgrade to ownclo...
184
185
186
187
  		$path = $this->normalizePath($path);
  
  		if (!$this->file_exists($path)) {
  			return false;
03e52840d   Kload   Init
188
  		}
31b7f2792   Kload   Upgrade to ownclo...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  
  		$dh = $this->opendir($path);
  
  		if(is_resource($dh)) {
  			while (($file = readdir($dh)) !== false) {
  				if ($file === '.' || $file === '..') {
  					continue;
  				}
  
  				if ($this->is_dir($path . '/' . $file)) {
  					$this->rmdir($path . '/' . $file);
  				} else {
  					$this->unlink($path . '/' . $file);
  				}
  			}
  		}
  
  		try {
  			$result = $this->connection->deleteObject(array(
  				'Bucket' => $this->bucket,
  				'Key' => $path . '/'
  			));
  			$this->testTimeout();
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
  		}
  
  		return true;
03e52840d   Kload   Init
218
219
220
  	}
  
  	public function opendir($path) {
31b7f2792   Kload   Upgrade to ownclo...
221
222
223
224
225
226
  		$path = $this->normalizePath($path);
  
  		if ($path === '.') {
  			$path = '';
  		} else if ($path) {
  			$path .= '/';
03e52840d   Kload   Init
227
  		}
31b7f2792   Kload   Upgrade to ownclo...
228
229
  
  		try {
03e52840d   Kload   Init
230
  			$files = array();
31b7f2792   Kload   Upgrade to ownclo...
231
232
233
234
235
236
237
238
239
240
241
242
243
  			$result = $this->connection->getIterator('ListObjects', array(
  				'Bucket' => $this->bucket,
  				'Delimiter' => '/',
  				'Prefix' => $path
  			), array('return_prefixes' => true));
  
  			foreach ($result as $object) {
  				$file = basename(
  					isset($object['Key']) ? $object['Key'] : $object['Prefix']
  				);
  
  				if ($file != basename($path)) {
  					$files[] = $file;
03e52840d   Kload   Init
244
245
  				}
  			}
31b7f2792   Kload   Upgrade to ownclo...
246

03e52840d   Kload   Init
247
  			\OC\Files\Stream\Dir::register('amazons3' . $path, $files);
31b7f2792   Kload   Upgrade to ownclo...
248

03e52840d   Kload   Init
249
  			return opendir('fakedir://amazons3' . $path);
31b7f2792   Kload   Upgrade to ownclo...
250
251
252
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
253
  		}
03e52840d   Kload   Init
254
255
256
  	}
  
  	public function stat($path) {
31b7f2792   Kload   Upgrade to ownclo...
257
258
259
260
261
262
263
264
265
  		$path = $this->normalizePath($path);
  
  		try {
  			if ($this->is_dir($path) && $path != '.') {
  				$path .= '/';
  			}
  
  			$result = $this->connection->headObject(array(
  				'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
266
  				'Key' => $this->cleanKey($path)
31b7f2792   Kload   Upgrade to ownclo...
267
268
269
270
271
272
273
274
275
  			));
  
  			$stat = array();
  			$stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0;
  			if ($result['Metadata']['lastmodified']) {
  				$stat['mtime'] = strtotime($result['Metadata']['lastmodified']);
  			} else {
  				$stat['mtime'] = strtotime($result['LastModified']);
  			}
03e52840d   Kload   Init
276
  			$stat['atime'] = time();
31b7f2792   Kload   Upgrade to ownclo...
277

03e52840d   Kload   Init
278
  			return $stat;
31b7f2792   Kload   Upgrade to ownclo...
279
280
281
  		} catch(S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
282
  		}
03e52840d   Kload   Init
283
284
285
  	}
  
  	public function filetype($path) {
31b7f2792   Kload   Upgrade to ownclo...
286
287
288
289
290
291
292
293
294
  		$path = $this->normalizePath($path);
  
  		try {
  			if ($path != '.' && $this->connection->doesObjectExist($this->bucket, $path)) {
  				return 'file';
  			}
  
  			if ($path != '.') {
  				$path .= '/';
03e52840d   Kload   Init
295
  			}
6d9380f96   Cédric Dupont   Update sources OC...
296
  			if ($this->connection->doesObjectExist($this->bucket, $this->cleanKey($path))) {
31b7f2792   Kload   Upgrade to ownclo...
297
298
299
300
301
  				return 'dir';
  			}
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
302
  		}
03e52840d   Kload   Init
303

31b7f2792   Kload   Upgrade to ownclo...
304
  		return false;
03e52840d   Kload   Init
305
  	}
31b7f2792   Kload   Upgrade to ownclo...
306
307
  	public function unlink($path) {
  		$path = $this->normalizePath($path);
03e52840d   Kload   Init
308

31b7f2792   Kload   Upgrade to ownclo...
309
310
311
  		try {
  			$result = $this->connection->deleteObject(array(
  				'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
312
  				'Key' => $this->cleanKey($path)
31b7f2792   Kload   Upgrade to ownclo...
313
314
315
316
317
  			));
  			$this->testTimeout();
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
318
  		}
03e52840d   Kload   Init
319

31b7f2792   Kload   Upgrade to ownclo...
320
  		return true;
03e52840d   Kload   Init
321
322
323
  	}
  
  	public function fopen($path, $mode) {
31b7f2792   Kload   Upgrade to ownclo...
324
  		$path = $this->normalizePath($path);
03e52840d   Kload   Init
325
326
327
328
  		switch ($mode) {
  			case 'r':
  			case 'rb':
  				$tmpFile = \OC_Helper::tmpFile();
31b7f2792   Kload   Upgrade to ownclo...
329
330
331
332
333
  				self::$tmpFiles[$tmpFile] = $path;
  
  				try {
  					$result = $this->connection->getObject(array(
  						'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
334
  						'Key' => $this->cleanKey($path),
31b7f2792   Kload   Upgrade to ownclo...
335
336
337
338
339
  						'SaveAs' => $tmpFile
  					));
  				} catch (S3Exception $e) {
  					\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  					return false;
03e52840d   Kload   Init
340
  				}
31b7f2792   Kload   Upgrade to ownclo...
341
342
  
  				return fopen($tmpFile, 'r');
03e52840d   Kload   Init
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  			case 'w':
  			case 'wb':
  			case 'a':
  			case 'ab':
  			case 'r+':
  			case 'w+':
  			case 'wb+':
  			case 'a+':
  			case 'x':
  			case 'x+':
  			case 'c':
  			case 'c+':
  				if (strrpos($path, '.') !== false) {
  					$ext = substr($path, strrpos($path, '.'));
  				} else {
  					$ext = '';
  				}
  				$tmpFile = \OC_Helper::tmpFile($ext);
  				\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  				if ($this->file_exists($path)) {
  					$source = $this->fopen($path, 'r');
  					file_put_contents($tmpFile, $source);
  				}
31b7f2792   Kload   Upgrade to ownclo...
366
  				self::$tmpFiles[$tmpFile] = $path;
03e52840d   Kload   Init
367
368
369
370
  				return fopen('close://' . $tmpFile, $mode);
  		}
  		return false;
  	}
03e52840d   Kload   Init
371
  	public function getMimeType($path) {
31b7f2792   Kload   Upgrade to ownclo...
372
373
374
  		$path = $this->normalizePath($path);
  
  		if ($this->is_dir($path)) {
03e52840d   Kload   Init
375
  			return 'httpd/unix-directory';
31b7f2792   Kload   Upgrade to ownclo...
376
377
378
379
  		} else if ($this->file_exists($path)) {
  			try {
  				$result = $this->connection->headObject(array(
  					'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
380
  					'Key' => $this->cleanKey($path)
31b7f2792   Kload   Upgrade to ownclo...
381
382
383
384
  				));
  			} catch (S3Exception $e) {
  				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  				return false;
03e52840d   Kload   Init
385
  			}
31b7f2792   Kload   Upgrade to ownclo...
386
387
  
  			return $result['ContentType'];
03e52840d   Kload   Init
388
389
390
391
392
  		}
  		return false;
  	}
  
  	public function touch($path, $mtime = null) {
31b7f2792   Kload   Upgrade to ownclo...
393
394
395
396
397
  		$path = $this->normalizePath($path);
  
  		$metadata = array();
  		if (!is_null($mtime)) {
  			$metadata = array('lastmodified' => $mtime);
03e52840d   Kload   Init
398
  		}
31b7f2792   Kload   Upgrade to ownclo...
399
400
401
402
403
404
405
406
  
  		try {
  			if ($this->file_exists($path)) {
  				if ($this->is_dir($path) && $path != '.') {
  					$path .= '/';
  				}
  				$result = $this->connection->copyObject(array(
  					'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
407
  					'Key' => $this->cleanKey($path),
31b7f2792   Kload   Upgrade to ownclo...
408
409
410
411
412
413
414
  					'Metadata' => $metadata,
  					'CopySource' => $this->bucket . '/' . $path
  				));
  				$this->testTimeout();
  			} else {
  				$result = $this->connection->putObject(array(
  					'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
415
  					'Key' => $this->cleanKey($path),
31b7f2792   Kload   Upgrade to ownclo...
416
417
418
419
420
421
422
  					'Metadata' => $metadata
  				));
  				$this->testTimeout();
  			}
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
423
  		}
31b7f2792   Kload   Upgrade to ownclo...
424
425
426
427
428
429
430
431
432
433
434
435
  
  		return true;
  	}
  
  	public function copy($path1, $path2) {
  		$path1 = $this->normalizePath($path1);
  		$path2 = $this->normalizePath($path2);
  
  		if ($this->is_file($path1)) {
  			try {
  				$result = $this->connection->copyObject(array(
  					'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
436
  					'Key' => $this->cleanKey($path2),
31b7f2792   Kload   Upgrade to ownclo...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  					'CopySource' => $this->bucket . '/' . $path1
  				));
  				$this->testTimeout();
  			} catch (S3Exception $e) {
  				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  				return false;
  			}
  		} else {
  			if ($this->file_exists($path2)) {
  				return false;
  			}
  
  			try {
  				$result = $this->connection->copyObject(array(
  					'Bucket' => $this->bucket,
  					'Key' => $path2 . '/',
  					'CopySource' => $this->bucket . '/' . $path1 . '/'
  				));
  				$this->testTimeout();
  			} catch (S3Exception $e) {
  				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  				return false;
  			}
  
  			$dh = $this->opendir($path1);
  			if(is_resource($dh)) {
  				while (($file = readdir($dh)) !== false) {
  					if ($file === '.' || $file === '..') {
  						continue;
  					}
  
  					$source = $path1 . '/' . $file;
  					$target = $path2 . '/' . $file;
  					$this->copy($source, $target);
  				}
  			}
  		}
  
  		return true;
  	}
  
  	public function rename($path1, $path2) {
  		$path1 = $this->normalizePath($path1);
  		$path2 = $this->normalizePath($path2);
  
  		if ($this->is_file($path1)) {
  			if ($this->copy($path1, $path2) === false) {
  				return false;
  			}
  
  			if ($this->unlink($path1) === false) {
  				$this->unlink($path2);
  				return false;
  			}
  		} else {
  			if ($this->file_exists($path2)) {
  				return false;
  			}
  
  			if ($this->copy($path1, $path2) === false) {
  				return false;
  			}
  
  			if ($this->rmdir($path1) === false) {
  				$this->rmdir($path2);
  				return false;
  			}
  		}
  
  		return true;
03e52840d   Kload   Init
507
508
509
  	}
  
  	public function test() {
31b7f2792   Kload   Upgrade to ownclo...
510
511
512
513
  		$test = $this->connection->getBucketAcl(array(
  			'Bucket' => $this->bucket,
  		));
  		if (isset($test) && !is_null($test->getPath('Owner/ID'))) {
03e52840d   Kload   Init
514
515
516
517
  			return true;
  		}
  		return false;
  	}
31b7f2792   Kload   Upgrade to ownclo...
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  	public function getId() {
  		return $this->id;
  	}
  
  	public function getConnection() {
  		return $this->connection;
  	}
  
  	public function writeBack($tmpFile) {
  		if (!isset(self::$tmpFiles[$tmpFile])) {
  			return false;
  		}
  
  		try {
  			$result= $this->connection->putObject(array(
  				'Bucket' => $this->bucket,
6d9380f96   Cédric Dupont   Update sources OC...
534
  				'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]),
31b7f2792   Kload   Upgrade to ownclo...
535
536
537
538
539
540
541
542
543
544
545
546
  				'SourceFile' => $tmpFile,
  				'ContentType' => \OC_Helper::getMimeType($tmpFile),
  				'ContentLength' => filesize($tmpFile)
  			));
  			$this->testTimeout();
  
  			unlink($tmpFile);
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
547
548
549
550
551
552
553
554
555
556
557
  
  	/**
  	 * check if curl is installed
  	 */
  	public static function checkDependencies() {
  		if (function_exists('curl_init')) {
  			return true;
  		} else {
  			return array('curl');
  		}
  	}
03e52840d   Kload   Init
558
  }