Index: trunk/phase3/includes/upload/UploadFromUrl.php |
— | — | @@ -105,41 +105,62 @@ |
106 | 106 | protected function makeTemporaryFile() { |
107 | 107 | return tempnam( wfTempDir(), 'URL' ); |
108 | 108 | } |
| 109 | + |
109 | 110 | /** |
110 | | - * Save the result of a HTTP request to the temporary file |
| 111 | + * Callback: save a chunk of the result of a HTTP request to the temporary file |
111 | 112 | * |
112 | | - * @param $req MWHttpRequest |
113 | | - * @return Status |
| 113 | + * @param $req mixed |
| 114 | + * @param $buffer string |
| 115 | + * @return int number of bytes handled |
114 | 116 | */ |
115 | | - private function saveTempFile( $req ) { |
116 | | - if ( $this->mTempPath === false ) { |
117 | | - return Status::newFatal( 'tmp-create-error' ); |
| 117 | + public function saveTempFileChunk( $req, $buffer ) { |
| 118 | + $nbytes = fwrite( $this->mTmpHandle, $buffer ); |
| 119 | + |
| 120 | + if ( $nbytes == strlen( $buffer ) ) { |
| 121 | + $this->mFileSize += $nbytes; |
| 122 | + } else { |
| 123 | + // Well... that's not good! |
| 124 | + fclose( $this->mTmpHandle ); |
| 125 | + $this->mTmpHandle = false; |
118 | 126 | } |
119 | | - if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) { |
120 | | - return Status::newFatal( 'tmp-write-error' ); |
121 | | - } |
122 | 127 | |
123 | | - $this->mFileSize = filesize( $this->mTempPath ); |
| 128 | + return $nbytes; |
| 129 | + } |
124 | 130 | |
125 | | - return Status::newGood(); |
126 | | - } |
127 | 131 | /** |
128 | 132 | * Download the file, save it to the temporary file and update the file |
129 | 133 | * size and set $mRemoveTempFile to true. |
130 | 134 | */ |
131 | 135 | protected function reallyFetchFile() { |
| 136 | + if ( $this->mTempPath === false ) { |
| 137 | + return Status::newFatal( 'tmp-create-error' ); |
| 138 | + } |
| 139 | + |
| 140 | + // Note the temporary file should already be created by makeTemporaryFile() |
| 141 | + $this->mTmpHandle = fopen( $this->mTempPath, 'wb' ); |
| 142 | + if ( !$this->mTmpHandle ) { |
| 143 | + return Status::newFatal( 'tmp-create-error' ); |
| 144 | + } |
| 145 | + |
| 146 | + $this->mRemoveTempFile = true; |
| 147 | + $this->mFileSize = 0; |
| 148 | + |
132 | 149 | $req = MWHttpRequest::factory( $this->mUrl ); |
| 150 | + $req->setCallback( array( $this, 'saveTempFileChunk' ) ); |
133 | 151 | $status = $req->execute(); |
134 | 152 | |
135 | | - if ( !$status->isOk() ) { |
136 | | - return $status; |
| 153 | + if ( $this->mTmpHandle ) { |
| 154 | + // File got written ok... |
| 155 | + fclose( $this->mTmpHandle ); |
| 156 | + $this->mTmpHandle = null; |
| 157 | + } else { |
| 158 | + // We encountered a write error during the download... |
| 159 | + return Status::newFatal( 'tmp-write-error' ); |
137 | 160 | } |
138 | 161 | |
139 | | - $status = $this->saveTempFile( $req ); |
140 | | - if ( !$status->isGood() ) { |
| 162 | + if ( !$status->isOk() ) { |
141 | 163 | return $status; |
142 | 164 | } |
143 | | - $this->mRemoveTempFile = true; |
144 | 165 | |
145 | 166 | return $status; |
146 | 167 | } |
Index: trunk/phase3/includes/HttpFunctions.php |
— | — | @@ -316,11 +316,26 @@ |
317 | 317 | } |
318 | 318 | |
319 | 319 | /** |
320 | | - * Set the callback |
| 320 | + * Set a read callback to accept data read from the HTTP request. |
| 321 | + * By default, data is appended to an internal buffer which can be |
| 322 | + * retrieved through $req->getContent(). |
321 | 323 | * |
| 324 | + * To handle data as it comes in -- especially for large files that |
| 325 | + * would not fit in memory -- you can instead set your own callback, |
| 326 | + * in the form function($resource, $buffer) where the first parameter |
| 327 | + * is the low-level resource being read (implementation specific), |
| 328 | + * and the second parameter is the data buffer. |
| 329 | + * |
| 330 | + * You MUST return the number of bytes handled in the buffer; if fewer |
| 331 | + * bytes are reported handled than were passed to you, the HTTP fetch |
| 332 | + * will be aborted. |
| 333 | + * |
322 | 334 | * @param $callback Callback |
323 | 335 | */ |
324 | 336 | public function setCallback( $callback ) { |
| 337 | + if ( !is_callable( $callback ) ) { |
| 338 | + throw new MWException( 'Invalid MwHttpRequest callback' ); |
| 339 | + } |
325 | 340 | $this->callback = $callback; |
326 | 341 | } |
327 | 342 | |