1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
7
|
|
|
use kalanis\UploadPerPartes\Uploader\Calculates; |
8
|
|
|
use kalanis\UploadPerPartes\Uploader\Hashed; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Uploader |
13
|
|
|
* @package kalanis\UploadPerPartes |
14
|
|
|
* Main server library for drive upload per-partes |
15
|
|
|
*/ |
16
|
|
|
class Uploader |
17
|
|
|
{ |
18
|
|
|
/** @var Keys\AKey */ |
19
|
|
|
protected $key = null; |
20
|
|
|
/** @var Interfaces\IUPPTranslations */ |
21
|
|
|
protected $lang = null; |
22
|
|
|
/** @var Interfaces\IInfoStorage */ |
23
|
|
|
protected $infoStorage = null; |
24
|
|
|
/** @var Interfaces\IDataStorage */ |
25
|
|
|
protected $dataStorage = null; |
26
|
|
|
/** @var Uploader\TargetSearch */ |
27
|
|
|
protected $targetSearch = null; |
28
|
|
|
/** @var Calculates */ |
29
|
|
|
protected $calculations = null; |
30
|
|
|
/** @var Hashed */ |
31
|
|
|
protected $hashed = null; |
32
|
|
|
/** @var Uploader\DriveFile must stay for tests */ |
33
|
|
|
protected $driver = null; |
34
|
|
|
/** @var Uploader\Processor */ |
35
|
|
|
protected $processor = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws Exceptions\UploadException |
39
|
|
|
*/ |
40
|
9 |
|
public function __construct() |
41
|
|
|
{ |
42
|
9 |
|
$this->lang = $this->getTranslations(); |
43
|
9 |
|
$this->infoStorage = $this->getInfoStorage(); |
44
|
9 |
|
$this->dataStorage = $this->getDataStorage(); |
45
|
9 |
|
$this->targetSearch = $this->getTarget($this->infoStorage, $this->dataStorage); |
46
|
9 |
|
$this->calculations = $this->getCalc(); |
47
|
9 |
|
$this->hashed = $this->getHashed(); |
48
|
9 |
|
$this->key = $this->getKeyFactory()->getVariant($this->targetSearch, $this->getKeyVariant()); |
49
|
9 |
|
$this->driver = new Uploader\DriveFile($this->infoStorage, $this->getInfoFormatFactory()->getFormat($this->getInfoFormat()), $this->key, $this->lang); |
50
|
9 |
|
$this->processor = $this->getProcessor($this->driver, $this->dataStorage, $this->hashed); |
51
|
9 |
|
} |
52
|
|
|
|
53
|
9 |
|
protected function getTranslations(): Interfaces\IUPPTranslations |
54
|
|
|
{ |
55
|
9 |
|
return new Uploader\Translations(); |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
protected function getInfoStorage(): Interfaces\IInfoStorage |
59
|
|
|
{ |
60
|
9 |
|
return new InfoStorage\Volume($this->lang); |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
protected function getDataStorage(): Interfaces\IDataStorage |
64
|
|
|
{ |
65
|
9 |
|
return new DataStorage\VolumeBasic($this->lang); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
protected function getTarget( |
69
|
|
|
Interfaces\IInfoStorage $infoStorage, |
70
|
|
|
Interfaces\IDataStorage $dataStorage, |
71
|
|
|
Interfaces\IUPPTranslations $lang = null |
72
|
|
|
): Uploader\TargetSearch |
73
|
|
|
{ |
74
|
9 |
|
return new Uploader\TargetSearch($infoStorage, $dataStorage, $lang); |
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
protected function getCalc(): Calculates |
78
|
|
|
{ |
79
|
9 |
|
return new Calculates(262144); |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
protected function getHashed(): Hashed |
83
|
|
|
{ |
84
|
9 |
|
return new Hashed(); |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
protected function getInfoFormatFactory(): InfoFormat\Factory |
88
|
|
|
{ |
89
|
9 |
|
return new InfoFormat\Factory($this->lang); |
90
|
|
|
} |
91
|
|
|
|
92
|
9 |
|
protected function getInfoFormat(): int |
93
|
|
|
{ |
94
|
9 |
|
return InfoFormat\Factory::FORMAT_JSON; |
95
|
|
|
} |
96
|
|
|
|
97
|
9 |
|
protected function getKeyFactory(): Keys\Factory |
98
|
|
|
{ |
99
|
9 |
|
return new Keys\Factory($this->lang); |
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
protected function getKeyVariant(): int |
103
|
|
|
{ |
104
|
9 |
|
return Keys\Factory::VARIANT_VOLUME; |
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
protected function getProcessor( |
108
|
|
|
Uploader\DriveFile $driver, |
109
|
|
|
Interfaces\IDataStorage $storage, |
110
|
|
|
Hashed $hashed, |
111
|
|
|
?Interfaces\IUPPTranslations $lang = null |
112
|
|
|
): Uploader\Processor |
113
|
|
|
{ |
114
|
9 |
|
return new Uploader\Processor($driver, $storage, $hashed, $lang); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Upload file by parts, final status |
119
|
|
|
* @param string $sharedKey |
120
|
|
|
* @return Response\AResponse |
121
|
|
|
*/ |
122
|
5 |
|
public function cancel(string $sharedKey): Response\AResponse |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
5 |
|
$this->processor->cancel($sharedKey); |
126
|
4 |
|
return Response\CancelResponse::initCancel($this->lang, $sharedKey); |
127
|
1 |
|
} catch (Exceptions\UploadException $ex) { |
128
|
1 |
|
return Response\CancelResponse::initError($this->lang, $sharedKey, $ex); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Upload file by parts, final status |
134
|
|
|
* @param string $sharedKey |
135
|
|
|
* @return Response\AResponse |
136
|
|
|
*/ |
137
|
3 |
|
public function done(string $sharedKey): Response\AResponse |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
3 |
|
return Response\DoneResponse::initDone($this->lang, $sharedKey, $this->processor->done($sharedKey)); |
141
|
1 |
|
} catch (Exceptions\UploadException $ex) { |
142
|
1 |
|
return Response\DoneResponse::initError($this->lang, $sharedKey, InfoFormat\Data::init(), $ex); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Upload file by parts, use driving file |
148
|
|
|
* @param string $sharedKey |
149
|
|
|
* @param string $content binary content |
150
|
|
|
* @param int<0, max>|null $segment where it save |
151
|
|
|
* @return Response\AResponse |
152
|
|
|
*/ |
153
|
4 |
|
public function upload(string $sharedKey, string $content, ?int $segment = null): Response\AResponse |
154
|
|
|
{ |
155
|
|
|
try { |
156
|
4 |
|
return Response\UploadResponse::initOK($this->lang, $sharedKey, $this->processor->upload($sharedKey, $content, $segment)); |
157
|
1 |
|
} catch (Exceptions\UploadException $e) { |
158
|
1 |
|
return Response\UploadResponse::initError($this->lang, $sharedKey, InfoFormat\Data::init(), $e); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Delete problematic segments |
164
|
|
|
* @param string $sharedKey |
165
|
|
|
* @param int<0, max> $segment |
166
|
|
|
* @return Response\AResponse |
167
|
|
|
*/ |
168
|
2 |
|
public function truncateFrom(string $sharedKey, int $segment): Response\AResponse |
169
|
|
|
{ |
170
|
|
|
try { |
171
|
2 |
|
return Response\TruncateResponse::initOK($this->lang, $sharedKey, $this->processor->truncateFrom($sharedKey, $segment)); |
172
|
1 |
|
} catch (Exceptions\UploadException $e) { |
173
|
1 |
|
return Response\TruncateResponse::initError($this->lang, $sharedKey, InfoFormat\Data::init(), $e); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Check already uploaded parts |
179
|
|
|
* @param string $sharedKey |
180
|
|
|
* @param int<0, max> $segment |
181
|
|
|
* @return Response\AResponse |
182
|
|
|
*/ |
183
|
2 |
|
public function check(string $sharedKey, int $segment): Response\AResponse |
184
|
|
|
{ |
185
|
|
|
try { |
186
|
2 |
|
return Response\CheckResponse::initOK($this->lang, $sharedKey, $this->processor->check($sharedKey, $segment)); |
187
|
1 |
|
} catch (Exceptions\UploadException $e) { |
188
|
1 |
|
return Response\CheckResponse::initError($this->lang, $sharedKey, $e); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Upload file by parts, create driving file |
194
|
|
|
* @param string $targetPath |
195
|
|
|
* @param string $remoteFileName posted file name |
196
|
|
|
* @param int<0, max> $length complete file size |
197
|
|
|
* @return Response\AResponse |
198
|
|
|
*/ |
199
|
7 |
|
public function init(string $targetPath, string $remoteFileName, int $length): Response\AResponse |
200
|
|
|
{ |
201
|
7 |
|
$partsCounter = $this->calculations->calcParts($length); |
202
|
|
|
try { |
203
|
7 |
|
$this->targetSearch->setTargetDir($targetPath)->setRemoteFileName($remoteFileName)->process(); |
204
|
6 |
|
$this->key->generateKeys(); |
205
|
6 |
|
$dataPack = InfoFormat\Data::init()->setData( |
206
|
6 |
|
$this->targetSearch->getFinalTargetName(), |
207
|
6 |
|
$this->targetSearch->getTemporaryTargetLocation(), |
208
|
|
|
$length, |
209
|
|
|
$partsCounter, |
210
|
6 |
|
$this->calculations->getBytesPerPart() |
211
|
|
|
); |
212
|
6 |
|
return Response\InitResponse::initOk($this->lang, $this->key->getSharedKey(), $this->processor->init($dataPack, $this->key->getSharedKey())); |
213
|
|
|
|
214
|
1 |
|
} catch (Exceptions\UploadException $e) { // obecne neco spatne |
215
|
1 |
|
return Response\InitResponse::initError($this->lang, InfoFormat\Data::init()->setData( |
216
|
1 |
|
$remoteFileName, '', $length, $partsCounter, $this->calculations->getBytesPerPart() |
217
|
|
|
), $e); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|