|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Aws\S3\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
7
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use Sfneal\Helpers\Aws\S3\Interfaces\S3Accessors; |
|
10
|
|
|
use Sfneal\Helpers\Aws\S3\Utils\Traits\LocalFileDeletion; |
|
11
|
|
|
use Sfneal\Helpers\Aws\S3\Utils\Traits\UploadStreaming; |
|
12
|
|
|
|
|
13
|
|
|
class CloudStorage implements S3Accessors |
|
14
|
|
|
{ |
|
15
|
|
|
use LocalFileDeletion; |
|
16
|
|
|
use UploadStreaming; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string AWS S3 file key |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $s3Key; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Storage S3 cloud disk name |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $disk; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* S3 constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $s3Key |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(string $s3Key) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->s3Key = $s3Key; |
|
36
|
|
|
$this->disk = config('filesystem.cloud', 's3'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieve the S3 key (useful in conjunctions with `autocompletePath()` method). |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getKey(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->s3Key; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the filesystem disk. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $disk |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setDisk(string $disk): self |
|
56
|
|
|
{ |
|
57
|
|
|
$this->disk = $disk; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Return either an S3 file url. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function url(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->storageDisk()->url($this->s3Key); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return either a temporary S3 file url. |
|
74
|
|
|
* |
|
75
|
|
|
* @param DateTimeInterface|null $expiration |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function urlTemp(DateTimeInterface $expiration = null): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->storageDisk()->temporaryUrl( |
|
81
|
|
|
$this->s3Key, |
|
82
|
|
|
$expiration ?? config('s3-helpers.expiration') |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve a Filesystem instance for the specified disk. |
|
88
|
|
|
* |
|
89
|
|
|
* @return Filesystem|FilesystemAdapter |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function storageDisk(): FilesystemAdapter |
|
92
|
|
|
{ |
|
93
|
|
|
return Storage::disk($this->disk); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|