Completed
Pull Request — master (#8158)
by Robin
24:42
created
apps/files_external/lib/Lib/Storage/AmazonS3.php 1 patch
Indentation   +551 added lines, -551 removed lines patch added patch discarded remove patch
@@ -46,556 +46,556 @@
 block discarded – undo
46 46
 use OCP\Constants;
47 47
 
48 48
 class AmazonS3 extends \OC\Files\Storage\Common {
49
-	use S3ConnectionTrait;
50
-	use S3ObjectTrait;
51
-
52
-	public function needsPartFile() {
53
-		return false;
54
-	}
55
-
56
-	/**
57
-	 * @var int in seconds
58
-	 */
59
-	private $rescanDelay = 10;
60
-
61
-	/** @var CappedMemoryCache|Result[] */
62
-	private $objectCache;
63
-
64
-	public function __construct($parameters) {
65
-		parent::__construct($parameters);
66
-		$this->parseParams($parameters);
67
-		$this->objectCache = new CappedMemoryCache();
68
-	}
69
-
70
-	/**
71
-	 * @param string $path
72
-	 * @return string correctly encoded path
73
-	 */
74
-	private function normalizePath($path) {
75
-		$path = trim($path, '/');
76
-
77
-		if (!$path) {
78
-			$path = '.';
79
-		}
80
-
81
-		return $path;
82
-	}
83
-
84
-	private function isRoot($path) {
85
-		return $path === '.';
86
-	}
87
-
88
-	private function cleanKey($path) {
89
-		if ($this->isRoot($path)) {
90
-			return '/';
91
-		}
92
-		return $path;
93
-	}
94
-
95
-	private function clearCache() {
96
-		$this->objectCache = new CappedMemoryCache();
97
-	}
98
-
99
-	private function invalidateCache($key) {
100
-		unset($this->objectCache[$key]);
101
-		$keys = array_keys($this->objectCache->getData());
102
-		$keyLength = strlen($key);
103
-		foreach ($keys as $existingKey) {
104
-			if (substr($existingKey, 0, $keyLength) === $keys) {
105
-				unset($this->objectCache[$existingKey]);
106
-			}
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * @param $key
112
-	 * @return Result|boolean
113
-	 */
114
-	private function headObject($key) {
115
-		if (!isset($this->objectCache[$key])) {
116
-			try {
117
-				$this->objectCache[$key] = $this->getConnection()->headObject(array(
118
-					'Bucket' => $this->bucket,
119
-					'Key' => $key
120
-				));
121
-			} catch (S3Exception $e) {
122
-				if ($e->getStatusCode() >= 500) {
123
-					throw $e;
124
-				}
125
-				$this->objectCache[$key] = false;
126
-			}
127
-		}
128
-
129
-		return $this->objectCache[$key];
130
-	}
131
-
132
-	/**
133
-	 * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name.
134
-	 * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home
135
-	 *
136
-	 * @param array $params
137
-	 */
138
-	public function updateLegacyId(array $params) {
139
-		$oldId = 'amazon::' . $params['key'] . md5($params['secret']);
140
-
141
-		// find by old id or bucket
142
-		$stmt = \OC::$server->getDatabaseConnection()->prepare(
143
-			'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)'
144
-		);
145
-		$stmt->execute(array($oldId, $this->id));
146
-		while ($row = $stmt->fetch()) {
147
-			$storages[$row['id']] = $row['numeric_id'];
148
-		}
149
-
150
-		if (isset($storages[$this->id]) && isset($storages[$oldId])) {
151
-			// if both ids exist, delete the old storage and corresponding filecache entries
152
-			\OC\Files\Cache\Storage::remove($oldId);
153
-		} else if (isset($storages[$oldId])) {
154
-			// if only the old id exists do an update
155
-			$stmt = \OC::$server->getDatabaseConnection()->prepare(
156
-				'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?'
157
-			);
158
-			$stmt->execute(array($this->id, $oldId));
159
-		}
160
-		// only the bucket based id may exist, do nothing
161
-	}
162
-
163
-	/**
164
-	 * Remove a file or folder
165
-	 *
166
-	 * @param string $path
167
-	 * @return bool
168
-	 */
169
-	protected function remove($path) {
170
-		// remember fileType to reduce http calls
171
-		$fileType = $this->filetype($path);
172
-		if ($fileType === 'dir') {
173
-			return $this->rmdir($path);
174
-		} else if ($fileType === 'file') {
175
-			return $this->unlink($path);
176
-		} else {
177
-			return false;
178
-		}
179
-	}
180
-
181
-	public function mkdir($path) {
182
-		$path = $this->normalizePath($path);
183
-
184
-		if ($this->is_dir($path)) {
185
-			return false;
186
-		}
187
-
188
-		try {
189
-			$this->getConnection()->putObject(array(
190
-				'Bucket' => $this->bucket,
191
-				'Key' => $path . '/',
192
-				'Body' => '',
193
-				'ContentType' => 'httpd/unix-directory'
194
-			));
195
-			$this->testTimeout();
196
-		} catch (S3Exception $e) {
197
-			\OCP\Util::logException('files_external', $e);
198
-			return false;
199
-		}
200
-
201
-		$this->invalidateCache($path);
202
-
203
-		return true;
204
-	}
205
-
206
-	public function file_exists($path) {
207
-		return $this->filetype($path) !== false;
208
-	}
209
-
210
-
211
-	public function rmdir($path) {
212
-		$path = $this->normalizePath($path);
213
-
214
-		if ($this->isRoot($path)) {
215
-			return $this->clearBucket();
216
-		}
217
-
218
-		if (!$this->file_exists($path)) {
219
-			return false;
220
-		}
221
-
222
-		$this->invalidateCache($path);
223
-		return $this->batchDelete($path);
224
-	}
225
-
226
-	protected function clearBucket() {
227
-		$this->clearCache();
228
-		try {
229
-			$this->getConnection()->clearBucket($this->bucket);
230
-			return true;
231
-			// clearBucket() is not working with Ceph, so if it fails we try the slower approach
232
-		} catch (\Exception $e) {
233
-			return $this->batchDelete();
234
-		}
235
-	}
236
-
237
-	private function batchDelete($path = null) {
238
-		$params = array(
239
-			'Bucket' => $this->bucket
240
-		);
241
-		if ($path !== null) {
242
-			$params['Prefix'] = $path . '/';
243
-		}
244
-		try {
245
-			// Since there are no real directories on S3, we need
246
-			// to delete all objects prefixed with the path.
247
-			do {
248
-				// instead of the iterator, manually loop over the list ...
249
-				$objects = $this->getConnection()->listObjects($params);
250
-				// ... so we can delete the files in batches
251
-				$this->getConnection()->deleteObjects(array(
252
-					'Bucket' => $this->bucket,
253
-					'Objects' => $objects['Contents']
254
-				));
255
-				$this->testTimeout();
256
-				// we reached the end when the list is no longer truncated
257
-			} while ($objects['IsTruncated']);
258
-		} catch (S3Exception $e) {
259
-			\OCP\Util::logException('files_external', $e);
260
-			return false;
261
-		}
262
-		return true;
263
-	}
264
-
265
-	public function opendir($path) {
266
-		$path = $this->normalizePath($path);
267
-
268
-		if ($this->isRoot($path)) {
269
-			$path = '';
270
-		} else {
271
-			$path .= '/';
272
-		}
273
-
274
-		try {
275
-			$files = array();
276
-			$results = $this->getConnection()->getPaginator('ListObjects', [
277
-				'Bucket' => $this->bucket,
278
-				'Delimiter' => '/',
279
-				'Prefix' => $path,
280
-			]);
281
-
282
-			foreach ($results as $result) {
283
-				// sub folders
284
-				if (is_array($result['CommonPrefixes'])) {
285
-					foreach ($result['CommonPrefixes'] as $prefix) {
286
-						$files[] = substr(trim($prefix['Prefix'], '/'), strlen($path));
287
-					}
288
-				}
289
-				foreach ($result['Contents'] as $object) {
290
-					if (isset($object['Key']) && $object['Key'] === $path) {
291
-						// it's the directory itself, skip
292
-						continue;
293
-					}
294
-					$file = basename(
295
-						isset($object['Key']) ? $object['Key'] : $object['Prefix']
296
-					);
297
-					$files[] = $file;
298
-				}
299
-			}
300
-
301
-			return IteratorDirectory::wrap($files);
302
-		} catch (S3Exception $e) {
303
-			\OCP\Util::logException('files_external', $e);
304
-			return false;
305
-		}
306
-	}
307
-
308
-	public function stat($path) {
309
-		$path = $this->normalizePath($path);
310
-
311
-		try {
312
-			$stat = array();
313
-			if ($this->is_dir($path)) {
314
-				//folders don't really exist
315
-				$stat['size'] = -1; //unknown
316
-				$stat['mtime'] = time() - $this->rescanDelay * 1000;
317
-			} else {
318
-				$result = $this->headObject($path);
319
-
320
-				$stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0;
321
-				if (isset($result['Metadata']['lastmodified'])) {
322
-					$stat['mtime'] = strtotime($result['Metadata']['lastmodified']);
323
-				} else {
324
-					$stat['mtime'] = strtotime($result['LastModified']);
325
-				}
326
-			}
327
-			$stat['atime'] = time();
328
-
329
-			return $stat;
330
-		} catch (S3Exception $e) {
331
-			\OCP\Util::logException('files_external', $e);
332
-			return false;
333
-		}
334
-	}
335
-
336
-	public function is_dir($path) {
337
-		$path = $this->normalizePath($path);
338
-		try {
339
-			return $this->isRoot($path) || $this->headObject($path . '/');
340
-		} catch (S3Exception $e) {
341
-			\OCP\Util::logException('files_external', $e);
342
-			return false;
343
-		}
344
-	}
345
-
346
-	public function filetype($path) {
347
-		$path = $this->normalizePath($path);
348
-
349
-		if ($this->isRoot($path)) {
350
-			return 'dir';
351
-		}
352
-
353
-		try {
354
-			if ($this->headObject($path)) {
355
-				return 'file';
356
-			}
357
-			if ($this->headObject($path . '/')) {
358
-				return 'dir';
359
-			}
360
-		} catch (S3Exception $e) {
361
-			\OCP\Util::logException('files_external', $e);
362
-			return false;
363
-		}
364
-
365
-		return false;
366
-	}
367
-
368
-	public function getPermissions($path) {
369
-		$type = $this->filetype($path);
370
-		if (!$type) {
371
-			return 0;
372
-		}
373
-		return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
374
-	}
375
-
376
-	public function unlink($path) {
377
-		$path = $this->normalizePath($path);
378
-
379
-		if ($this->is_dir($path)) {
380
-			return $this->rmdir($path);
381
-		}
382
-
383
-		try {
384
-			$this->deleteObject($path);
385
-			$this->invalidateCache($path);
386
-		} catch (S3Exception $e) {
387
-			\OCP\Util::logException('files_external', $e);
388
-			return false;
389
-		}
390
-
391
-		return true;
392
-	}
393
-
394
-	public function fopen($path, $mode) {
395
-		$path = $this->normalizePath($path);
396
-
397
-		switch ($mode) {
398
-			case 'r':
399
-			case 'rb':
400
-				try {
401
-					return $this->readObject($path);
402
-				} catch (S3Exception $e) {
403
-					\OCP\Util::logException('files_external', $e);
404
-					return false;
405
-				}
406
-			case 'w':
407
-			case 'wb':
408
-				$tmpFile = \OCP\Files::tmpFile();
409
-
410
-				$handle = fopen($tmpFile, 'w');
411
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
412
-					$this->writeBack($tmpFile, $path);
413
-				});
414
-			case 'a':
415
-			case 'ab':
416
-			case 'r+':
417
-			case 'w+':
418
-			case 'wb+':
419
-			case 'a+':
420
-			case 'x':
421
-			case 'x+':
422
-			case 'c':
423
-			case 'c+':
424
-				if (strrpos($path, '.') !== false) {
425
-					$ext = substr($path, strrpos($path, '.'));
426
-				} else {
427
-					$ext = '';
428
-				}
429
-				$tmpFile = \OCP\Files::tmpFile($ext);
430
-				if ($this->file_exists($path)) {
431
-					$source = $this->readObject($path);
432
-					file_put_contents($tmpFile, $source);
433
-				}
434
-
435
-				$handle = fopen($tmpFile, $mode);
436
-				return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
437
-					$this->writeBack($tmpFile, $path);
438
-				});
439
-		}
440
-		return false;
441
-	}
442
-
443
-	public function touch($path, $mtime = null) {
444
-		$path = $this->normalizePath($path);
445
-
446
-		$metadata = array();
447
-		if (is_null($mtime)) {
448
-			$mtime = time();
449
-		}
450
-		$metadata = [
451
-			'lastmodified' => gmdate(\DateTime::RFC1123, $mtime)
452
-		];
453
-
454
-		$fileType = $this->filetype($path);
455
-		try {
456
-			if ($fileType !== false) {
457
-				if ($fileType === 'dir' && !$this->isRoot($path)) {
458
-					$path .= '/';
459
-				}
460
-				$this->getConnection()->copyObject([
461
-					'Bucket' => $this->bucket,
462
-					'Key' => $this->cleanKey($path),
463
-					'Metadata' => $metadata,
464
-					'CopySource' => $this->bucket . '/' . $path,
465
-					'MetadataDirective' => 'REPLACE',
466
-				]);
467
-				$this->testTimeout();
468
-			} else {
469
-				$mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
470
-				$this->getConnection()->putObject([
471
-					'Bucket' => $this->bucket,
472
-					'Key' => $this->cleanKey($path),
473
-					'Metadata' => $metadata,
474
-					'Body' => '',
475
-					'ContentType' => $mimeType,
476
-					'MetadataDirective' => 'REPLACE',
477
-				]);
478
-				$this->testTimeout();
479
-			}
480
-		} catch (S3Exception $e) {
481
-			\OCP\Util::logException('files_external', $e);
482
-			return false;
483
-		}
484
-
485
-		$this->invalidateCache($path);
486
-		return true;
487
-	}
488
-
489
-	public function copy($path1, $path2) {
490
-		$path1 = $this->normalizePath($path1);
491
-		$path2 = $this->normalizePath($path2);
492
-
493
-		if ($this->is_file($path1)) {
494
-			try {
495
-				$this->getConnection()->copyObject(array(
496
-					'Bucket' => $this->bucket,
497
-					'Key' => $this->cleanKey($path2),
498
-					'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1)
499
-				));
500
-				$this->testTimeout();
501
-			} catch (S3Exception $e) {
502
-				\OCP\Util::logException('files_external', $e);
503
-				return false;
504
-			}
505
-		} else {
506
-			$this->remove($path2);
507
-
508
-			try {
509
-				$this->getConnection()->copyObject(array(
510
-					'Bucket' => $this->bucket,
511
-					'Key' => $path2 . '/',
512
-					'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/')
513
-				));
514
-				$this->testTimeout();
515
-			} catch (S3Exception $e) {
516
-				\OCP\Util::logException('files_external', $e);
517
-				return false;
518
-			}
519
-
520
-			$dh = $this->opendir($path1);
521
-			if (is_resource($dh)) {
522
-				while (($file = readdir($dh)) !== false) {
523
-					if (\OC\Files\Filesystem::isIgnoredDir($file)) {
524
-						continue;
525
-					}
526
-
527
-					$source = $path1 . '/' . $file;
528
-					$target = $path2 . '/' . $file;
529
-					$this->copy($source, $target);
530
-				}
531
-			}
532
-		}
533
-
534
-		$this->invalidateCache($path2);
535
-
536
-		return true;
537
-	}
538
-
539
-	public function rename($path1, $path2) {
540
-		$path1 = $this->normalizePath($path1);
541
-		$path2 = $this->normalizePath($path2);
542
-
543
-		if ($this->is_file($path1)) {
544
-
545
-			if ($this->copy($path1, $path2) === false) {
546
-				return false;
547
-			}
548
-
549
-			if ($this->unlink($path1) === false) {
550
-				$this->unlink($path2);
551
-				return false;
552
-			}
553
-		} else {
554
-
555
-			if ($this->copy($path1, $path2) === false) {
556
-				return false;
557
-			}
558
-
559
-			if ($this->rmdir($path1) === false) {
560
-				$this->rmdir($path2);
561
-				return false;
562
-			}
563
-		}
564
-
565
-		return true;
566
-	}
567
-
568
-	public function test() {
569
-		$this->getConnection()->headBucket([
570
-			'Bucket' => $this->bucket
571
-		]);
572
-		return true;
573
-	}
574
-
575
-	public function getId() {
576
-		return $this->id;
577
-	}
578
-
579
-	public function writeBack($tmpFile, $path) {
580
-		try {
581
-			$source = fopen($tmpFile, 'r');
582
-			$this->writeObject($path, $source);
583
-			$this->invalidateCache($path);
584
-			fclose($source);
585
-
586
-			unlink($tmpFile);
587
-			return true;
588
-		} catch (S3Exception $e) {
589
-			\OCP\Util::logException('files_external', $e);
590
-			return false;
591
-		}
592
-	}
593
-
594
-	/**
595
-	 * check if curl is installed
596
-	 */
597
-	public static function checkDependencies() {
598
-		return true;
599
-	}
49
+    use S3ConnectionTrait;
50
+    use S3ObjectTrait;
51
+
52
+    public function needsPartFile() {
53
+        return false;
54
+    }
55
+
56
+    /**
57
+     * @var int in seconds
58
+     */
59
+    private $rescanDelay = 10;
60
+
61
+    /** @var CappedMemoryCache|Result[] */
62
+    private $objectCache;
63
+
64
+    public function __construct($parameters) {
65
+        parent::__construct($parameters);
66
+        $this->parseParams($parameters);
67
+        $this->objectCache = new CappedMemoryCache();
68
+    }
69
+
70
+    /**
71
+     * @param string $path
72
+     * @return string correctly encoded path
73
+     */
74
+    private function normalizePath($path) {
75
+        $path = trim($path, '/');
76
+
77
+        if (!$path) {
78
+            $path = '.';
79
+        }
80
+
81
+        return $path;
82
+    }
83
+
84
+    private function isRoot($path) {
85
+        return $path === '.';
86
+    }
87
+
88
+    private function cleanKey($path) {
89
+        if ($this->isRoot($path)) {
90
+            return '/';
91
+        }
92
+        return $path;
93
+    }
94
+
95
+    private function clearCache() {
96
+        $this->objectCache = new CappedMemoryCache();
97
+    }
98
+
99
+    private function invalidateCache($key) {
100
+        unset($this->objectCache[$key]);
101
+        $keys = array_keys($this->objectCache->getData());
102
+        $keyLength = strlen($key);
103
+        foreach ($keys as $existingKey) {
104
+            if (substr($existingKey, 0, $keyLength) === $keys) {
105
+                unset($this->objectCache[$existingKey]);
106
+            }
107
+        }
108
+    }
109
+
110
+    /**
111
+     * @param $key
112
+     * @return Result|boolean
113
+     */
114
+    private function headObject($key) {
115
+        if (!isset($this->objectCache[$key])) {
116
+            try {
117
+                $this->objectCache[$key] = $this->getConnection()->headObject(array(
118
+                    'Bucket' => $this->bucket,
119
+                    'Key' => $key
120
+                ));
121
+            } catch (S3Exception $e) {
122
+                if ($e->getStatusCode() >= 500) {
123
+                    throw $e;
124
+                }
125
+                $this->objectCache[$key] = false;
126
+            }
127
+        }
128
+
129
+        return $this->objectCache[$key];
130
+    }
131
+
132
+    /**
133
+     * Updates old storage ids (v0.2.1 and older) that are based on key and secret to new ones based on the bucket name.
134
+     * TODO Do this in an update.php. requires iterating over all users and loading the mount.json from their home
135
+     *
136
+     * @param array $params
137
+     */
138
+    public function updateLegacyId(array $params) {
139
+        $oldId = 'amazon::' . $params['key'] . md5($params['secret']);
140
+
141
+        // find by old id or bucket
142
+        $stmt = \OC::$server->getDatabaseConnection()->prepare(
143
+            'SELECT `numeric_id`, `id` FROM `*PREFIX*storages` WHERE `id` IN (?, ?)'
144
+        );
145
+        $stmt->execute(array($oldId, $this->id));
146
+        while ($row = $stmt->fetch()) {
147
+            $storages[$row['id']] = $row['numeric_id'];
148
+        }
149
+
150
+        if (isset($storages[$this->id]) && isset($storages[$oldId])) {
151
+            // if both ids exist, delete the old storage and corresponding filecache entries
152
+            \OC\Files\Cache\Storage::remove($oldId);
153
+        } else if (isset($storages[$oldId])) {
154
+            // if only the old id exists do an update
155
+            $stmt = \OC::$server->getDatabaseConnection()->prepare(
156
+                'UPDATE `*PREFIX*storages` SET `id` = ? WHERE `id` = ?'
157
+            );
158
+            $stmt->execute(array($this->id, $oldId));
159
+        }
160
+        // only the bucket based id may exist, do nothing
161
+    }
162
+
163
+    /**
164
+     * Remove a file or folder
165
+     *
166
+     * @param string $path
167
+     * @return bool
168
+     */
169
+    protected function remove($path) {
170
+        // remember fileType to reduce http calls
171
+        $fileType = $this->filetype($path);
172
+        if ($fileType === 'dir') {
173
+            return $this->rmdir($path);
174
+        } else if ($fileType === 'file') {
175
+            return $this->unlink($path);
176
+        } else {
177
+            return false;
178
+        }
179
+    }
180
+
181
+    public function mkdir($path) {
182
+        $path = $this->normalizePath($path);
183
+
184
+        if ($this->is_dir($path)) {
185
+            return false;
186
+        }
187
+
188
+        try {
189
+            $this->getConnection()->putObject(array(
190
+                'Bucket' => $this->bucket,
191
+                'Key' => $path . '/',
192
+                'Body' => '',
193
+                'ContentType' => 'httpd/unix-directory'
194
+            ));
195
+            $this->testTimeout();
196
+        } catch (S3Exception $e) {
197
+            \OCP\Util::logException('files_external', $e);
198
+            return false;
199
+        }
200
+
201
+        $this->invalidateCache($path);
202
+
203
+        return true;
204
+    }
205
+
206
+    public function file_exists($path) {
207
+        return $this->filetype($path) !== false;
208
+    }
209
+
210
+
211
+    public function rmdir($path) {
212
+        $path = $this->normalizePath($path);
213
+
214
+        if ($this->isRoot($path)) {
215
+            return $this->clearBucket();
216
+        }
217
+
218
+        if (!$this->file_exists($path)) {
219
+            return false;
220
+        }
221
+
222
+        $this->invalidateCache($path);
223
+        return $this->batchDelete($path);
224
+    }
225
+
226
+    protected function clearBucket() {
227
+        $this->clearCache();
228
+        try {
229
+            $this->getConnection()->clearBucket($this->bucket);
230
+            return true;
231
+            // clearBucket() is not working with Ceph, so if it fails we try the slower approach
232
+        } catch (\Exception $e) {
233
+            return $this->batchDelete();
234
+        }
235
+    }
236
+
237
+    private function batchDelete($path = null) {
238
+        $params = array(
239
+            'Bucket' => $this->bucket
240
+        );
241
+        if ($path !== null) {
242
+            $params['Prefix'] = $path . '/';
243
+        }
244
+        try {
245
+            // Since there are no real directories on S3, we need
246
+            // to delete all objects prefixed with the path.
247
+            do {
248
+                // instead of the iterator, manually loop over the list ...
249
+                $objects = $this->getConnection()->listObjects($params);
250
+                // ... so we can delete the files in batches
251
+                $this->getConnection()->deleteObjects(array(
252
+                    'Bucket' => $this->bucket,
253
+                    'Objects' => $objects['Contents']
254
+                ));
255
+                $this->testTimeout();
256
+                // we reached the end when the list is no longer truncated
257
+            } while ($objects['IsTruncated']);
258
+        } catch (S3Exception $e) {
259
+            \OCP\Util::logException('files_external', $e);
260
+            return false;
261
+        }
262
+        return true;
263
+    }
264
+
265
+    public function opendir($path) {
266
+        $path = $this->normalizePath($path);
267
+
268
+        if ($this->isRoot($path)) {
269
+            $path = '';
270
+        } else {
271
+            $path .= '/';
272
+        }
273
+
274
+        try {
275
+            $files = array();
276
+            $results = $this->getConnection()->getPaginator('ListObjects', [
277
+                'Bucket' => $this->bucket,
278
+                'Delimiter' => '/',
279
+                'Prefix' => $path,
280
+            ]);
281
+
282
+            foreach ($results as $result) {
283
+                // sub folders
284
+                if (is_array($result['CommonPrefixes'])) {
285
+                    foreach ($result['CommonPrefixes'] as $prefix) {
286
+                        $files[] = substr(trim($prefix['Prefix'], '/'), strlen($path));
287
+                    }
288
+                }
289
+                foreach ($result['Contents'] as $object) {
290
+                    if (isset($object['Key']) && $object['Key'] === $path) {
291
+                        // it's the directory itself, skip
292
+                        continue;
293
+                    }
294
+                    $file = basename(
295
+                        isset($object['Key']) ? $object['Key'] : $object['Prefix']
296
+                    );
297
+                    $files[] = $file;
298
+                }
299
+            }
300
+
301
+            return IteratorDirectory::wrap($files);
302
+        } catch (S3Exception $e) {
303
+            \OCP\Util::logException('files_external', $e);
304
+            return false;
305
+        }
306
+    }
307
+
308
+    public function stat($path) {
309
+        $path = $this->normalizePath($path);
310
+
311
+        try {
312
+            $stat = array();
313
+            if ($this->is_dir($path)) {
314
+                //folders don't really exist
315
+                $stat['size'] = -1; //unknown
316
+                $stat['mtime'] = time() - $this->rescanDelay * 1000;
317
+            } else {
318
+                $result = $this->headObject($path);
319
+
320
+                $stat['size'] = $result['ContentLength'] ? $result['ContentLength'] : 0;
321
+                if (isset($result['Metadata']['lastmodified'])) {
322
+                    $stat['mtime'] = strtotime($result['Metadata']['lastmodified']);
323
+                } else {
324
+                    $stat['mtime'] = strtotime($result['LastModified']);
325
+                }
326
+            }
327
+            $stat['atime'] = time();
328
+
329
+            return $stat;
330
+        } catch (S3Exception $e) {
331
+            \OCP\Util::logException('files_external', $e);
332
+            return false;
333
+        }
334
+    }
335
+
336
+    public function is_dir($path) {
337
+        $path = $this->normalizePath($path);
338
+        try {
339
+            return $this->isRoot($path) || $this->headObject($path . '/');
340
+        } catch (S3Exception $e) {
341
+            \OCP\Util::logException('files_external', $e);
342
+            return false;
343
+        }
344
+    }
345
+
346
+    public function filetype($path) {
347
+        $path = $this->normalizePath($path);
348
+
349
+        if ($this->isRoot($path)) {
350
+            return 'dir';
351
+        }
352
+
353
+        try {
354
+            if ($this->headObject($path)) {
355
+                return 'file';
356
+            }
357
+            if ($this->headObject($path . '/')) {
358
+                return 'dir';
359
+            }
360
+        } catch (S3Exception $e) {
361
+            \OCP\Util::logException('files_external', $e);
362
+            return false;
363
+        }
364
+
365
+        return false;
366
+    }
367
+
368
+    public function getPermissions($path) {
369
+        $type = $this->filetype($path);
370
+        if (!$type) {
371
+            return 0;
372
+        }
373
+        return $type === 'dir' ? Constants::PERMISSION_ALL : Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
374
+    }
375
+
376
+    public function unlink($path) {
377
+        $path = $this->normalizePath($path);
378
+
379
+        if ($this->is_dir($path)) {
380
+            return $this->rmdir($path);
381
+        }
382
+
383
+        try {
384
+            $this->deleteObject($path);
385
+            $this->invalidateCache($path);
386
+        } catch (S3Exception $e) {
387
+            \OCP\Util::logException('files_external', $e);
388
+            return false;
389
+        }
390
+
391
+        return true;
392
+    }
393
+
394
+    public function fopen($path, $mode) {
395
+        $path = $this->normalizePath($path);
396
+
397
+        switch ($mode) {
398
+            case 'r':
399
+            case 'rb':
400
+                try {
401
+                    return $this->readObject($path);
402
+                } catch (S3Exception $e) {
403
+                    \OCP\Util::logException('files_external', $e);
404
+                    return false;
405
+                }
406
+            case 'w':
407
+            case 'wb':
408
+                $tmpFile = \OCP\Files::tmpFile();
409
+
410
+                $handle = fopen($tmpFile, 'w');
411
+                return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
412
+                    $this->writeBack($tmpFile, $path);
413
+                });
414
+            case 'a':
415
+            case 'ab':
416
+            case 'r+':
417
+            case 'w+':
418
+            case 'wb+':
419
+            case 'a+':
420
+            case 'x':
421
+            case 'x+':
422
+            case 'c':
423
+            case 'c+':
424
+                if (strrpos($path, '.') !== false) {
425
+                    $ext = substr($path, strrpos($path, '.'));
426
+                } else {
427
+                    $ext = '';
428
+                }
429
+                $tmpFile = \OCP\Files::tmpFile($ext);
430
+                if ($this->file_exists($path)) {
431
+                    $source = $this->readObject($path);
432
+                    file_put_contents($tmpFile, $source);
433
+                }
434
+
435
+                $handle = fopen($tmpFile, $mode);
436
+                return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
437
+                    $this->writeBack($tmpFile, $path);
438
+                });
439
+        }
440
+        return false;
441
+    }
442
+
443
+    public function touch($path, $mtime = null) {
444
+        $path = $this->normalizePath($path);
445
+
446
+        $metadata = array();
447
+        if (is_null($mtime)) {
448
+            $mtime = time();
449
+        }
450
+        $metadata = [
451
+            'lastmodified' => gmdate(\DateTime::RFC1123, $mtime)
452
+        ];
453
+
454
+        $fileType = $this->filetype($path);
455
+        try {
456
+            if ($fileType !== false) {
457
+                if ($fileType === 'dir' && !$this->isRoot($path)) {
458
+                    $path .= '/';
459
+                }
460
+                $this->getConnection()->copyObject([
461
+                    'Bucket' => $this->bucket,
462
+                    'Key' => $this->cleanKey($path),
463
+                    'Metadata' => $metadata,
464
+                    'CopySource' => $this->bucket . '/' . $path,
465
+                    'MetadataDirective' => 'REPLACE',
466
+                ]);
467
+                $this->testTimeout();
468
+            } else {
469
+                $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path);
470
+                $this->getConnection()->putObject([
471
+                    'Bucket' => $this->bucket,
472
+                    'Key' => $this->cleanKey($path),
473
+                    'Metadata' => $metadata,
474
+                    'Body' => '',
475
+                    'ContentType' => $mimeType,
476
+                    'MetadataDirective' => 'REPLACE',
477
+                ]);
478
+                $this->testTimeout();
479
+            }
480
+        } catch (S3Exception $e) {
481
+            \OCP\Util::logException('files_external', $e);
482
+            return false;
483
+        }
484
+
485
+        $this->invalidateCache($path);
486
+        return true;
487
+    }
488
+
489
+    public function copy($path1, $path2) {
490
+        $path1 = $this->normalizePath($path1);
491
+        $path2 = $this->normalizePath($path2);
492
+
493
+        if ($this->is_file($path1)) {
494
+            try {
495
+                $this->getConnection()->copyObject(array(
496
+                    'Bucket' => $this->bucket,
497
+                    'Key' => $this->cleanKey($path2),
498
+                    'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1)
499
+                ));
500
+                $this->testTimeout();
501
+            } catch (S3Exception $e) {
502
+                \OCP\Util::logException('files_external', $e);
503
+                return false;
504
+            }
505
+        } else {
506
+            $this->remove($path2);
507
+
508
+            try {
509
+                $this->getConnection()->copyObject(array(
510
+                    'Bucket' => $this->bucket,
511
+                    'Key' => $path2 . '/',
512
+                    'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/')
513
+                ));
514
+                $this->testTimeout();
515
+            } catch (S3Exception $e) {
516
+                \OCP\Util::logException('files_external', $e);
517
+                return false;
518
+            }
519
+
520
+            $dh = $this->opendir($path1);
521
+            if (is_resource($dh)) {
522
+                while (($file = readdir($dh)) !== false) {
523
+                    if (\OC\Files\Filesystem::isIgnoredDir($file)) {
524
+                        continue;
525
+                    }
526
+
527
+                    $source = $path1 . '/' . $file;
528
+                    $target = $path2 . '/' . $file;
529
+                    $this->copy($source, $target);
530
+                }
531
+            }
532
+        }
533
+
534
+        $this->invalidateCache($path2);
535
+
536
+        return true;
537
+    }
538
+
539
+    public function rename($path1, $path2) {
540
+        $path1 = $this->normalizePath($path1);
541
+        $path2 = $this->normalizePath($path2);
542
+
543
+        if ($this->is_file($path1)) {
544
+
545
+            if ($this->copy($path1, $path2) === false) {
546
+                return false;
547
+            }
548
+
549
+            if ($this->unlink($path1) === false) {
550
+                $this->unlink($path2);
551
+                return false;
552
+            }
553
+        } else {
554
+
555
+            if ($this->copy($path1, $path2) === false) {
556
+                return false;
557
+            }
558
+
559
+            if ($this->rmdir($path1) === false) {
560
+                $this->rmdir($path2);
561
+                return false;
562
+            }
563
+        }
564
+
565
+        return true;
566
+    }
567
+
568
+    public function test() {
569
+        $this->getConnection()->headBucket([
570
+            'Bucket' => $this->bucket
571
+        ]);
572
+        return true;
573
+    }
574
+
575
+    public function getId() {
576
+        return $this->id;
577
+    }
578
+
579
+    public function writeBack($tmpFile, $path) {
580
+        try {
581
+            $source = fopen($tmpFile, 'r');
582
+            $this->writeObject($path, $source);
583
+            $this->invalidateCache($path);
584
+            fclose($source);
585
+
586
+            unlink($tmpFile);
587
+            return true;
588
+        } catch (S3Exception $e) {
589
+            \OCP\Util::logException('files_external', $e);
590
+            return false;
591
+        }
592
+    }
593
+
594
+    /**
595
+     * check if curl is installed
596
+     */
597
+    public static function checkDependencies() {
598
+        return true;
599
+    }
600 600
 
601 601
 }
Please login to merge, or discard this patch.