Blame view

sources/apps/files_external/lib/amazons3.php 12.6 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
27
28
29
30
31
  set_include_path(get_include_path() . PATH_SEPARATOR .
          \OC_App::getAppPath('files_external') . '/3rdparty/aws-sdk-php');
  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

31b7f2792   Kload   Upgrade to ownclo...
55
56
  	private function normalizePath($path) {
  		$path = trim($path, '/');
03e52840d   Kload   Init
57

31b7f2792   Kload   Upgrade to ownclo...
58
59
60
  		if (!$path) {
  			$path = '.';
  		}
03e52840d   Kload   Init
61

31b7f2792   Kload   Upgrade to ownclo...
62
63
64
65
66
67
  		return $path;
  	}
  
  	private function testTimeout() {
  		if ($this->test) {
  			sleep($this->timeout);
03e52840d   Kload   Init
68
69
  		}
  	}
31b7f2792   Kload   Upgrade to ownclo...
70
71
72
73
74
75
76
77
78
79
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
  	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
113
114
  			}
  		}
03e52840d   Kload   Init
115

31b7f2792   Kload   Upgrade to ownclo...
116
117
118
119
120
121
122
123
124
125
  		if (!$this->file_exists('.')) {
  			$result = $this->connection->putObject(array(
  				'Bucket' => $this->bucket,
  				'Key'    => '.',
  				'Body'   => '',
  				'ContentType' => 'httpd/unix-directory',
  				'ContentLength' => 0
  			));
  			$this->testTimeout();
  		}
03e52840d   Kload   Init
126
127
128
  	}
  
  	public function mkdir($path) {
31b7f2792   Kload   Upgrade to ownclo...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  		$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
158
159
  			$path .= '/';
  		}
31b7f2792   Kload   Upgrade to ownclo...
160
161
162
163
164
165
166
167
168
169
170
171
  
  		try {
  			$result = $this->connection->doesObjectExist(
  				$this->bucket,
  				$path
  			);
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
  		}
  
  		return $result;
03e52840d   Kload   Init
172
  	}
31b7f2792   Kload   Upgrade to ownclo...
173

03e52840d   Kload   Init
174
  	public function rmdir($path) {
31b7f2792   Kload   Upgrade to ownclo...
175
176
177
178
  		$path = $this->normalizePath($path);
  
  		if (!$this->file_exists($path)) {
  			return false;
03e52840d   Kload   Init
179
  		}
31b7f2792   Kload   Upgrade to ownclo...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  
  		$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
209
210
211
  	}
  
  	public function opendir($path) {
31b7f2792   Kload   Upgrade to ownclo...
212
213
214
215
216
217
  		$path = $this->normalizePath($path);
  
  		if ($path === '.') {
  			$path = '';
  		} else if ($path) {
  			$path .= '/';
03e52840d   Kload   Init
218
  		}
31b7f2792   Kload   Upgrade to ownclo...
219
220
  
  		try {
03e52840d   Kload   Init
221
  			$files = array();
31b7f2792   Kload   Upgrade to ownclo...
222
223
224
225
226
227
228
229
230
231
232
233
234
  			$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
235
236
  				}
  			}
31b7f2792   Kload   Upgrade to ownclo...
237

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

03e52840d   Kload   Init
240
  			return opendir('fakedir://amazons3' . $path);
31b7f2792   Kload   Upgrade to ownclo...
241
242
243
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
244
  		}
03e52840d   Kload   Init
245
246
247
  	}
  
  	public function stat($path) {
31b7f2792   Kload   Upgrade to ownclo...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  		$path = $this->normalizePath($path);
  
  		try {
  			if ($this->is_dir($path) && $path != '.') {
  				$path .= '/';
  			}
  
  			$result = $this->connection->headObject(array(
  				'Bucket' => $this->bucket,
  				'Key' => $path
  			));
  
  			$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
267
  			$stat['atime'] = time();
31b7f2792   Kload   Upgrade to ownclo...
268

03e52840d   Kload   Init
269
  			return $stat;
31b7f2792   Kload   Upgrade to ownclo...
270
271
272
  		} catch(S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
273
  		}
03e52840d   Kload   Init
274
275
276
  	}
  
  	public function filetype($path) {
31b7f2792   Kload   Upgrade to ownclo...
277
278
279
280
281
282
283
284
285
  		$path = $this->normalizePath($path);
  
  		try {
  			if ($path != '.' && $this->connection->doesObjectExist($this->bucket, $path)) {
  				return 'file';
  			}
  
  			if ($path != '.') {
  				$path .= '/';
03e52840d   Kload   Init
286
  			}
31b7f2792   Kload   Upgrade to ownclo...
287
288
289
290
291
292
293
  
  			if ($this->connection->doesObjectExist($this->bucket, $path)) {
  				return 'dir';
  			}
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
294
  		}
03e52840d   Kload   Init
295

31b7f2792   Kload   Upgrade to ownclo...
296
  		return false;
03e52840d   Kload   Init
297
  	}
31b7f2792   Kload   Upgrade to ownclo...
298
299
  	public function unlink($path) {
  		$path = $this->normalizePath($path);
03e52840d   Kload   Init
300

31b7f2792   Kload   Upgrade to ownclo...
301
302
303
304
305
306
307
308
309
  		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;
03e52840d   Kload   Init
310
  		}
03e52840d   Kload   Init
311

31b7f2792   Kload   Upgrade to ownclo...
312
  		return true;
03e52840d   Kload   Init
313
314
315
  	}
  
  	public function fopen($path, $mode) {
31b7f2792   Kload   Upgrade to ownclo...
316
  		$path = $this->normalizePath($path);
03e52840d   Kload   Init
317
318
319
320
  		switch ($mode) {
  			case 'r':
  			case 'rb':
  				$tmpFile = \OC_Helper::tmpFile();
31b7f2792   Kload   Upgrade to ownclo...
321
322
323
324
325
326
327
328
329
330
331
  				self::$tmpFiles[$tmpFile] = $path;
  
  				try {
  					$result = $this->connection->getObject(array(
  						'Bucket' => $this->bucket,
  						'Key' => $path,
  						'SaveAs' => $tmpFile
  					));
  				} catch (S3Exception $e) {
  					\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  					return false;
03e52840d   Kload   Init
332
  				}
31b7f2792   Kload   Upgrade to ownclo...
333
334
  
  				return fopen($tmpFile, 'r');
03e52840d   Kload   Init
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  			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...
358
  				self::$tmpFiles[$tmpFile] = $path;
03e52840d   Kload   Init
359
360
361
362
  				return fopen('close://' . $tmpFile, $mode);
  		}
  		return false;
  	}
03e52840d   Kload   Init
363
  	public function getMimeType($path) {
31b7f2792   Kload   Upgrade to ownclo...
364
365
366
  		$path = $this->normalizePath($path);
  
  		if ($this->is_dir($path)) {
03e52840d   Kload   Init
367
  			return 'httpd/unix-directory';
31b7f2792   Kload   Upgrade to ownclo...
368
369
370
371
372
373
374
375
376
  		} else if ($this->file_exists($path)) {
  			try {
  				$result = $this->connection->headObject(array(
  					'Bucket' => $this->bucket,
  					'Key' => $path
  				));
  			} catch (S3Exception $e) {
  				\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  				return false;
03e52840d   Kload   Init
377
  			}
31b7f2792   Kload   Upgrade to ownclo...
378
379
  
  			return $result['ContentType'];
03e52840d   Kload   Init
380
381
382
383
384
  		}
  		return false;
  	}
  
  	public function touch($path, $mtime = null) {
31b7f2792   Kload   Upgrade to ownclo...
385
386
387
388
389
  		$path = $this->normalizePath($path);
  
  		$metadata = array();
  		if (!is_null($mtime)) {
  			$metadata = array('lastmodified' => $mtime);
03e52840d   Kload   Init
390
  		}
31b7f2792   Kload   Upgrade to ownclo...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  
  		try {
  			if ($this->file_exists($path)) {
  				if ($this->is_dir($path) && $path != '.') {
  					$path .= '/';
  				}
  				$result = $this->connection->copyObject(array(
  					'Bucket' => $this->bucket,
  					'Key' => $path,
  					'Metadata' => $metadata,
  					'CopySource' => $this->bucket . '/' . $path
  				));
  				$this->testTimeout();
  			} else {
  				$result = $this->connection->putObject(array(
  					'Bucket' => $this->bucket,
  					'Key' => $path,
  					'Metadata' => $metadata
  				));
  				$this->testTimeout();
  			}
  		} catch (S3Exception $e) {
  			\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
  			return false;
03e52840d   Kload   Init
415
  		}
31b7f2792   Kload   Upgrade to ownclo...
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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
  
  		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,
  					'Key' => $path2,
  					'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
499
500
501
  	}
  
  	public function test() {
31b7f2792   Kload   Upgrade to ownclo...
502
503
504
505
  		$test = $this->connection->getBucketAcl(array(
  			'Bucket' => $this->bucket,
  		));
  		if (isset($test) && !is_null($test->getPath('Owner/ID'))) {
03e52840d   Kload   Init
506
507
508
509
  			return true;
  		}
  		return false;
  	}
31b7f2792   Kload   Upgrade to ownclo...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  	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,
  				'Key' => self::$tmpFiles[$tmpFile],
  				'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;
  		}
  	}
03e52840d   Kload   Init
539
  }