From 738de2e6915658487df1e2dc829de6f407582044 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Thu, 15 Apr 2010 08:44:25 +0000 Subject: [PATCH] Refactored UploadFromUrl to use HttpFunctions. Errors while fetching the file are now properly handled and also displayed in a reasonable way to the user (bug 22015). --- includes/specials/SpecialUpload.php | 2 +- includes/upload/UploadFromUrl.php | 39 ++++++++--------------------- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index dc57af7f3e..33b53aaba6 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -390,7 +390,7 @@ class SpecialUpload extends SpecialPage { // Fetch the file if required $status = $this->mUpload->fetchFile(); if( !$status->isOK() ) { - $this->showUploadForm( $this->getUploadForm( $wgOut->parse( $status->getWikiText() ) ) ); + $this->showUploadError( $wgOut->parse( $status->getWikiText() ) ); return; } diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index 763dae38d1..5f4912ef53 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -78,44 +78,25 @@ class UploadFromUrl extends UploadBase { if( !self::isValidUrl( $this->mUrl ) ) { return Status::newFatal( 'upload-proto-error' ); } - $res = $this->curlCopy(); - if( $res !== true ) { - return Status::newFatal( $res ); - } - return Status::newGood(); - } - - /** - * Safe copy from URL - * Returns true if there was an error, false otherwise - */ - private function curlCopy() { - global $wgOut; # Open temporary file $this->mCurlDestHandle = @fopen( $this->mTempPath, "wb" ); if( $this->mCurlDestHandle === false ) { # Could not open temporary file to write in - return 'upload-file-error'; + return Status::newFatal( 'upload-file-error' ); } - - $ch = curl_init(); - curl_setopt( $ch, CURLOPT_HTTP_VERSION, 1.0); # Probably not needed, but apparently can work around some bug - curl_setopt( $ch, CURLOPT_TIMEOUT, 10); # 10 seconds timeout - curl_setopt( $ch, CURLOPT_LOW_SPEED_LIMIT, 512); # 0.5KB per second minimum transfer speed - curl_setopt( $ch, CURLOPT_URL, $this->mUrl); - curl_setopt( $ch, CURLOPT_WRITEFUNCTION, array( $this, 'uploadCurlCallback' ) ); - curl_exec( $ch ); - $error = curl_errno( $ch ); - curl_close( $ch ); - + + $options = array( + 'method' => 'GET', + 'timeout' => 10, + ); + $req = HttpRequest::factory( $this->mUrl, $options ); + $req->setCallback( array( $this, 'uploadCurlCallback' ) ); + $status = $req->execute(); fclose( $this->mCurlDestHandle ); unset( $this->mCurlDestHandle ); - if( $error ) - return "upload-curl-error$errornum"; - - return true; + return $status; } /** -- 2.20.1