1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* UploadHandler.php |
5
|
|
|
* |
6
|
|
|
* File upload with Ajax. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-upload |
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2017 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Jaxon\Upload; |
16
|
|
|
|
17
|
|
|
use Jaxon\Exception\RequestException; |
18
|
|
|
use Jaxon\Request\Upload\UploadHandlerInterface; |
19
|
|
|
use Jaxon\Upload\Manager\FileStorage; |
20
|
|
|
use Jaxon\Upload\Manager\UploadManager; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Closure; |
23
|
|
|
|
24
|
|
|
use function count; |
25
|
|
|
|
26
|
|
|
class UploadHandler implements UploadHandlerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The uploaded files copied in the user dir |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $aUserFiles = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The constructor |
37
|
|
|
* |
38
|
|
|
* @param FileStorage $xFileStorage |
39
|
|
|
* @param UploadManager $xUploadManager |
40
|
|
|
*/ |
41
|
|
|
public function __construct(private FileStorage $xFileStorage, |
42
|
|
|
private UploadManager $xUploadManager) |
43
|
|
|
{} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the uploaded file name sanitizer |
47
|
|
|
* |
48
|
|
|
* @param Closure $cSanitizer The closure |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function sanitizer(Closure $cSanitizer) |
53
|
|
|
{ |
54
|
|
|
$this->xUploadManager->setNameSanitizer($cSanitizer); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the uploaded files |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function files(): array |
63
|
|
|
{ |
64
|
|
|
return $this->aUserFiles; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if the current request contains uploaded files |
69
|
|
|
* |
70
|
|
|
* @param ServerRequestInterface $xRequest |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function canProcessRequest(ServerRequestInterface $xRequest): bool |
75
|
|
|
{ |
76
|
|
|
return count($xRequest->getUploadedFiles()) > 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Process the uploaded files in the HTTP request |
81
|
|
|
* |
82
|
|
|
* @param ServerRequestInterface $xRequest |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
* @throws RequestException |
86
|
|
|
*/ |
87
|
|
|
public function processRequest(ServerRequestInterface $xRequest): bool |
88
|
|
|
{ |
89
|
|
|
// Copy the uploaded files from the HTTP request. |
90
|
|
|
$this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $sStorage |
96
|
|
|
* @param Closure $cFactory |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function registerStorageAdapter(string $sStorage, Closure $cFactory) |
101
|
|
|
{ |
102
|
|
|
$this->xFileStorage->registerAdapter($sStorage, $cFactory); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|