1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Simple S3 package. |
4
|
|
|
* |
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Commands\Handlers; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\UriInterface; |
15
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
16
|
|
|
use Matecat\SimpleS3\Helpers\File; |
17
|
|
|
|
18
|
|
|
class OpenItem extends CommandHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Open an item and return the content of it. |
22
|
|
|
* |
23
|
|
|
* @param array $params |
24
|
|
|
* |
25
|
|
|
* @return mixed|UriInterface |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
2 |
|
public function handle($params = []) |
29
|
|
|
{ |
30
|
2 |
|
$bucketName = $params['bucket']; |
31
|
2 |
|
$keyName = $params['key']; |
32
|
|
|
|
33
|
|
|
try { |
34
|
2 |
|
$url = $this->client->getPublicItemLink(['bucket' => $bucketName, 'key' => $keyName]); |
35
|
2 |
|
$content = File::loadFile($url, $this->client->hasSslVerify()); |
36
|
|
|
|
37
|
2 |
|
if (false === $content) { |
38
|
|
|
if (null !== $this->commandHandlerLogger) { |
39
|
|
|
$this->commandHandlerLogger->log($this, sprintf('Something went wrong during getting content of \'%s\' item from \'%s\' bucket', $keyName, $bucketName), 'warning'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
if (null !== $this->commandHandlerLogger) { |
46
|
2 |
|
$this->commandHandlerLogger->log($this, sprintf('Content from \'%s\' item was successfully obtained from \'%s\' bucket', $keyName, $bucketName)); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return $content; |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
if (null !== $this->commandHandlerLogger) { |
52
|
|
|
$this->commandHandlerLogger->logExceptionAndReturnFalse($e); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw $e; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $params |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
2 |
|
public function validateParams($params = []) |
65
|
|
|
{ |
66
|
|
|
return ( |
67
|
2 |
|
isset($params['bucket']) and |
68
|
2 |
|
isset($params['key']) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|