1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace STS\StorageConnect\Models\Concerns; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use STS\StorageConnect\Contracts\UploadTarget; |
8
|
|
|
use STS\StorageConnect\Events\UploadFailed; |
9
|
|
|
use STS\StorageConnect\Events\UploadInProgress; |
10
|
|
|
use STS\StorageConnect\Events\UploadRetrying; |
11
|
|
|
use STS\StorageConnect\Events\UploadStarted; |
12
|
|
|
use STS\StorageConnect\Events\UploadSucceeded; |
13
|
|
|
use STS\StorageConnect\Exceptions\UploadException; |
14
|
|
|
use STS\StorageConnect\Jobs\CheckUploadStatus; |
15
|
|
|
use STS\StorageConnect\Jobs\UploadFile; |
16
|
|
|
use STS\StorageConnect\UploadRequest; |
17
|
|
|
use STS\StorageConnect\UploadResponse; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @property Carbon uploaded_at |
21
|
|
|
*/ |
22
|
|
|
trait UploadsFiles |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param $source |
26
|
|
|
* @param $destinationPath |
27
|
|
|
* @param bool $shouldQueue |
28
|
|
|
* @param null $queueJob |
29
|
|
|
* |
30
|
|
|
* @return UploadResponse|bool |
31
|
|
|
*/ |
32
|
|
|
public function upload($source, $destinationPath = null, $shouldQueue = true, $queueJob = null) |
33
|
|
|
{ |
34
|
|
|
$this->verify(); |
|
|
|
|
35
|
|
|
|
36
|
|
|
if ($shouldQueue) { |
37
|
|
|
return dispatch(new UploadFile($source, $destinationPath, $this)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$request = new UploadRequest($source, $destinationPath); |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
return $this->handleUpload($request); |
44
|
|
|
} catch (UploadException $exception) { |
45
|
|
|
$this->handleUploadError($exception, $queueJob); |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} finally { |
49
|
|
|
$this->ping(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param UploadRequest $request |
55
|
|
|
* |
56
|
|
|
* @return UploadResponse |
57
|
|
|
*/ |
58
|
|
|
protected function handleUpload(UploadRequest $request) |
59
|
|
|
{ |
60
|
|
|
return $this->processResponse($this->adapter()->upload($request)); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param UploadResponse $response |
65
|
|
|
* @param $queueJob |
66
|
|
|
*/ |
67
|
|
|
public function checkUploadStatus(UploadResponse $response, $queueJob) |
68
|
|
|
{ |
69
|
|
|
$response->incrementStatusCheck(); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$this->processResponse($this->adapter()->checkUploadStatus($response)); |
|
|
|
|
73
|
|
|
} catch (UploadException $exception) { |
74
|
|
|
if($exception->shouldRetry()) { |
75
|
|
|
$queueJob->release(); |
76
|
|
|
} else { |
77
|
|
|
$queueJob->fail($exception); |
78
|
|
|
event(new UploadFailed($this, $exception)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($exception->shouldDisable()) { |
82
|
|
|
$this->disable($exception->getReason()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param UploadResponse $response |
89
|
|
|
* |
90
|
|
|
* @return UploadResponse |
91
|
|
|
*/ |
92
|
|
|
protected function processResponse(UploadResponse $response) |
93
|
|
|
{ |
94
|
|
|
if ($response->isAsync()) { |
95
|
|
|
dispatch(new CheckUploadStatus($this, $response))->delay(15); |
96
|
|
|
|
97
|
|
|
if ($response->getStatusChecks() == 0) { |
98
|
|
|
event(new UploadStarted($this, $response)); |
99
|
|
|
} else { |
100
|
|
|
event(new UploadInProgress($this, $response)); |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
|
|
$this->uploaded_at = Carbon::now(); |
104
|
|
|
$this->save(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
event(new UploadSucceeded($this, $response)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $response; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param UploadException $exception |
114
|
|
|
* @param null $job |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
protected function handleUploadError(UploadException $exception, $job = null) |
119
|
|
|
{ |
120
|
|
|
$exception->setStorage($this); |
121
|
|
|
|
122
|
|
|
if ($exception->shouldRetry() && $job) { |
123
|
|
|
event(new UploadRetrying($this, $exception)); |
124
|
|
|
|
125
|
|
|
$job->release(); |
126
|
|
|
|
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($exception->shouldDisable()) { |
131
|
|
|
$this->disable($exception->getReason()); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($job) { |
135
|
|
|
$job->fail($exception); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
event(new UploadFailed($this, $exception)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $path |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isUploaded($path) |
147
|
|
|
{ |
148
|
|
|
if($path instanceof Model && $path instanceof UploadTarget) { |
149
|
|
|
$path = $path->upload_destination_path; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->adapter()->pathExists( |
|
|
|
|
153
|
|
|
str_start($path, '/') |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |
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.