3 namespace Wikimedia\ParamValidator\Util
;
5 use Psr\Http\Message\UploadedFileInterface
;
7 use Wikimedia\AtEase\AtEase
;
10 * A simple implementation of UploadedFileInterface
12 * This exists so ParamValidator needn't depend on any specific PSR-7
13 * implementation for a class implementing UploadedFileInterface. It shouldn't
14 * be used directly by other code, other than perhaps when implementing
15 * Callbacks::getUploadedFile() when another PSR-7 library is not already in use.
19 class UploadedFile
implements UploadedFileInterface
{
21 /** @var array File data */
27 /** @var UploadedFileStream|null */
28 private $stream = null;
30 /** @var bool Whether moveTo() was called */
31 private $moved = false;
34 * @param array $data Data from $_FILES
35 * @param bool $fromUpload Set false if using this task with data not from
36 * $_FILES. Intended for unit testing.
38 public function __construct( array $data, $fromUpload = true ) {
40 $this->fromUpload
= $fromUpload;
44 * Throw if there was an error
45 * @throws RuntimeException
47 private function checkError() {
48 switch ( $this->data
['error'] ) {
52 case UPLOAD_ERR_INI_SIZE
:
53 throw new RuntimeException( 'Upload exceeded maximum size' );
55 case UPLOAD_ERR_FORM_SIZE
:
56 throw new RuntimeException( 'Upload exceeded form-specified maximum size' );
58 case UPLOAD_ERR_PARTIAL
:
59 throw new RuntimeException( 'File was only partially uploaded' );
61 case UPLOAD_ERR_NO_FILE
:
62 throw new RuntimeException( 'No file was uploaded' );
64 case UPLOAD_ERR_NO_TMP_DIR
:
65 throw new RuntimeException( 'PHP has no temporary folder for storing uploaded files' );
67 case UPLOAD_ERR_CANT_WRITE
:
68 throw new RuntimeException( 'PHP was unable to save the uploaded file' );
70 case UPLOAD_ERR_EXTENSION
:
71 throw new RuntimeException( 'A PHP extension stopped the file upload' );
74 throw new RuntimeException( 'Unknown upload error code ' . $this->data
['error'] );
78 throw new RuntimeException( 'File has already been moved' );
80 if ( !isset( $this->data
['tmp_name'] ) ||
!file_exists( $this->data
['tmp_name'] ) ) {
81 throw new RuntimeException( 'Uploaded file is missing' );
85 public function getStream() {
86 if ( $this->stream
) {
91 $this->stream
= new UploadedFileStream( $this->data
['tmp_name'] );
95 public function moveTo( $targetPath ) {
98 if ( $this->fromUpload
&& !is_uploaded_file( $this->data
['tmp_name'] ) ) {
99 throw new RuntimeException( 'Specified file is not an uploaded file' );
102 // TODO remove the function_exists check once we drop HHVM support
103 if ( function_exists( 'error_clear_last' ) ) {
106 $ret = AtEase
::quietCall(
107 $this->fromUpload ?
'move_uploaded_file' : 'rename',
108 $this->data
['tmp_name'],
111 if ( $ret === false ) {
112 $err = error_get_last();
113 throw new RuntimeException( "Move failed: " . ( $err['message'] ??
'Unknown error' ) );
117 if ( $this->stream
) {
118 $this->stream
->close();
119 $this->stream
= null;
123 public function getSize() {
124 return $this->data
['size'] ??
null;
127 public function getError() {
128 return $this->data
['error'] ?? UPLOAD_ERR_NO_FILE
;
131 public function getClientFilename() {
132 $ret = $this->data
['name'] ??
null;
133 return $ret === '' ?
null : $ret;
136 public function getClientMediaType() {
137 $ret = $this->data
['type'] ??
null;
138 return $ret === '' ?
null : $ret;