1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Aws\S3\Utils; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
7
|
|
|
use Illuminate\Http\File; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Facades\Response; |
10
|
|
|
use Sfneal\Helpers\Aws\S3\Interfaces\S3Actions; |
11
|
|
|
|
12
|
|
|
class S3 extends CloudStorage implements S3Actions |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Upload a file to S3 using automatic streaming or raw file uploading. |
16
|
|
|
* |
17
|
|
|
* @param string $localFilePath |
18
|
|
|
* @param string|null $acl |
19
|
|
|
* @return self |
20
|
|
|
*/ |
21
|
|
|
public function upload(string $localFilePath, string $acl = null): self |
22
|
|
|
{ |
23
|
|
|
// Use streaming for improved performance if enabled |
24
|
|
|
if ($this->isStreamingEnabled()) { |
25
|
|
|
$this->uploadStream($localFilePath, $acl); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Use standard file uploading |
29
|
|
|
else { |
30
|
|
|
$this->uploadRaw(fopen($localFilePath, 'r+'), $acl); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Conditionally delete the local file |
34
|
|
|
$this->deleteLocalFileIfEnabled($localFilePath); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Upload raw file contents to an S3 bucket. |
41
|
|
|
* |
42
|
|
|
* @param $fileContents |
43
|
|
|
* @param string|null $acl |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
|
|
public function uploadRaw($fileContents, string $acl = null): self |
47
|
|
|
{ |
48
|
|
|
$this->storageDisk()->put($this->s3Key, $fileContents, $acl); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Upload a file to S3 using automatic streaming. |
55
|
|
|
* |
56
|
|
|
* @param string $localFilePath |
57
|
|
|
* @param string|null $acl |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
protected function uploadStream(string $localFilePath, string $acl = null): self |
61
|
|
|
{ |
62
|
|
|
// Upload the file |
63
|
|
|
$this->storageDisk()->putFileAs( |
64
|
|
|
dirname($this->s3Key), |
65
|
|
|
new File($localFilePath), |
66
|
|
|
basename($this->s3Key), |
67
|
|
|
$acl |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Download a file from an S3 bucket. |
75
|
|
|
* |
76
|
|
|
* @param string|null $fileName |
77
|
|
|
* @return \Illuminate\Http\Response |
78
|
|
|
* @throws FileNotFoundException|\League\Flysystem\FileNotFoundException |
79
|
|
|
*/ |
80
|
|
|
public function download(string $fileName = null): \Illuminate\Http\Response |
81
|
|
|
{ |
82
|
|
|
$fileName = $fileName ?? basename($this->s3Key); |
83
|
|
|
|
84
|
|
|
return Response::make( |
85
|
|
|
$this->storageDisk()->get($this->s3Key), |
86
|
|
|
200, |
87
|
|
|
[ |
88
|
|
|
'Content-Type' => $this->storageDisk()->getMimetype($this->s3Key), |
89
|
|
|
'Content-Length' => $this->storageDisk()->getSize($this->s3Key), |
90
|
|
|
'Content-Description' => 'File Transfer', |
91
|
|
|
'Content-Disposition' => "attachment; filename={$fileName}", |
92
|
|
|
'Content-Transfer-Encoding' => 'binary', |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Autocomplete an S3 path by providing the known start of a path. |
99
|
|
|
* |
100
|
|
|
* - once path autocompletion is resolved the $s3_key property is replaced with the found path |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function autocompletePath(): self |
105
|
|
|
{ |
106
|
|
|
// Extract the known $base of the path & the $wildcard |
107
|
|
|
$directory = dirname($this->s3Key); |
108
|
|
|
|
109
|
|
|
// Get all of the folders in the base directory |
110
|
|
|
$folders = $this->storageDisk()->directories($directory); |
111
|
|
|
|
112
|
|
|
// Filter folders to find the wildcard path |
113
|
|
|
$folders = array_filter($folders, function ($value) { |
114
|
|
|
return str_starts_with($value, $this->s3Key); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
// return the resolved path |
118
|
|
|
$this->s3Key = collect($folders)->values()->first(); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Retrieve an array of all files in a directory with an optional filtering closure. |
125
|
|
|
* |
126
|
|
|
* @param Closure|null $closure |
127
|
|
|
* @return Collection |
128
|
|
|
*/ |
129
|
|
|
public function allFiles(Closure $closure = null): Collection |
130
|
|
|
{ |
131
|
|
|
// Create array of all files |
132
|
|
|
$allFiles = collect($this->storageDisk()->allFiles($this->s3Key)); |
133
|
|
|
|
134
|
|
|
// Apply filtering closure |
135
|
|
|
if (isset($closure)) { |
136
|
|
|
return $allFiles->filter($closure); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $allFiles; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Retrieve an array of all directories within another directory with an optional filtering closure. |
144
|
|
|
* |
145
|
|
|
* @param Closure|null $closure |
146
|
|
|
* @return Collection |
147
|
|
|
*/ |
148
|
|
|
public function allDirectories(Closure $closure = null): Collection |
149
|
|
|
{ |
150
|
|
|
// Create array of all directories |
151
|
|
|
$allDirectories = collect($this->storageDisk()->allDirectories($this->s3Key)); |
152
|
|
|
|
153
|
|
|
// Apply filtering closure |
154
|
|
|
if (isset($closure)) { |
155
|
|
|
return $allDirectories->filter($closure); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $allDirectories; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|