|
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 Exception; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
use Matecat\SimpleS3\Commands\CommandHandler; |
|
17
|
|
|
use Psr\Http\Message\UriInterface; |
|
18
|
|
|
|
|
19
|
|
|
class GetPublicItemLink extends CommandHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Get the temporary public link of an item. |
|
23
|
|
|
* It return a presigned URL. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $params |
|
26
|
|
|
* |
|
27
|
|
|
* @return UriInterface |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function handle(array $params = []): UriInterface |
|
31
|
|
|
{ |
|
32
|
4 |
|
$bucketName = $params[ 'bucket' ]; |
|
33
|
4 |
|
$keyName = $params[ 'key' ]; |
|
34
|
4 |
|
$expires = (isset($params[ 'expires' ])) ? $params[ 'expires' ] : '+1 hour'; |
|
35
|
|
|
|
|
36
|
4 |
|
if ($this->client->hasEncoder()) { |
|
37
|
4 |
|
$keyName = $this->client->getEncoder()->encode($keyName); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
4 |
|
$cmd = $this->client->getConn()->getCommand('GetObject', [ |
|
42
|
4 |
|
'Bucket' => $bucketName, |
|
43
|
4 |
|
'Key' => $keyName, |
|
44
|
4 |
|
]); |
|
45
|
|
|
|
|
46
|
4 |
|
$link = $this->client->getConn()->createPresignedRequest($cmd, $expires)->getUri(); |
|
47
|
|
|
|
|
48
|
4 |
|
$this->commandHandlerLogger?->log($this, sprintf('Public link of \'%s\' file was successfully obtained from \'%s\' bucket', $keyName, $bucketName)); |
|
49
|
|
|
|
|
50
|
4 |
|
return $link; |
|
51
|
|
|
} catch (InvalidArgumentException $e) { |
|
52
|
|
|
$this->commandHandlerLogger?->logExceptionAndReturnFalse($e); |
|
53
|
|
|
|
|
54
|
|
|
throw $e; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $params |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function validateParams(array $params = []): bool |
|
64
|
|
|
{ |
|
65
|
4 |
|
return ( |
|
66
|
4 |
|
isset($params[ 'bucket' ]) and |
|
67
|
4 |
|
isset($params[ 'key' ]) |
|
68
|
4 |
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|