1
|
|
|
<?php |
2
|
|
|
namespace STS\StorageConnect\Models\Concerns; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use STS\StorageConnect\Contracts\UploadTarget; |
7
|
|
|
use STS\StorageConnect\Events\UploadFailed; |
8
|
|
|
use STS\StorageConnect\Events\UploadRetrying; |
9
|
|
|
use STS\StorageConnect\Events\UploadSucceeded; |
10
|
|
|
use STS\StorageConnect\Exceptions\UploadException; |
11
|
|
|
use STS\StorageConnect\Jobs\UploadFile; |
12
|
|
|
|
13
|
|
|
trait UploadsFiles |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param $source |
17
|
|
|
* @param $destinationPath |
18
|
|
|
* @param bool $shouldQueue |
19
|
|
|
* @param null $queueJob |
20
|
|
|
* |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public function upload($source, $destinationPath = null, $shouldQueue = true, $queueJob = null) |
24
|
|
|
{ |
25
|
|
|
$this->verify(); |
|
|
|
|
26
|
|
|
|
27
|
|
|
if ($shouldQueue) { |
28
|
|
|
return dispatch(new UploadFile($source, $destinationPath, $this)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
list($sourcePath, $destinationPath, $targetModel) = $this->preparePaths($source, $destinationPath); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
return $this->handleUpload($sourcePath, $destinationPath, $targetModel); |
35
|
|
|
} catch (UploadException $exception) { |
36
|
|
|
$this->handleUploadError($exception, $queueJob, $targetModel); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->ping(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $source |
46
|
|
|
* @param null $destinationPath |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function preparePaths($source, $destinationPath = null) |
51
|
|
|
{ |
52
|
|
|
if($source instanceof Model && $source instanceof UploadTarget) { |
53
|
|
|
return [ |
54
|
|
|
$source->upload_source_path, |
55
|
|
|
$destinationPath ?: $source->upload_destination_path, |
56
|
|
|
$source |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
(string) $source, |
62
|
|
|
$destinationPath, |
63
|
|
|
null |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $sourcePath |
69
|
|
|
* @param $destinationPath |
70
|
|
|
* @param null $targetModel |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
protected function handleUpload($sourcePath, $destinationPath, $targetModel = null) |
75
|
|
|
{ |
76
|
|
|
if (starts_with($sourcePath, "s3://")) { |
77
|
|
|
app('aws')->createClient('s3')->registerStreamWrapper(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->adapter()->upload($sourcePath, $destinationPath); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->uploaded_at = Carbon::now(); |
|
|
|
|
83
|
|
|
$this->save(); |
|
|
|
|
84
|
|
|
|
85
|
|
|
event(new UploadSucceeded($this, $sourcePath, $destinationPath, $targetModel)); |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param UploadException $exception |
92
|
|
|
* @param null $job |
93
|
|
|
* @param null $targetModel |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
protected function handleUploadError(UploadException $exception, $job = null, $targetModel = null) |
98
|
|
|
{ |
99
|
|
|
$exception->setStorage($this); |
100
|
|
|
|
101
|
|
|
if ($exception->shouldRetry() && $job) { |
102
|
|
|
event(new UploadRetrying($this, $exception, $targetModel)); |
103
|
|
|
|
104
|
|
|
$job->release(); |
105
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($exception->shouldDisable()) { |
110
|
|
|
$this->disable($exception->getReason()); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($job) { |
114
|
|
|
$job->fail($exception); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
event(new UploadFailed($this, $exception, $targetModel)); |
118
|
|
|
} |
119
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.