Forum Replies Created

Viewing 1 replies (of 1 total)
  • riotCode

    (@riotcode)

    I got this error from the Media imports, I corrected it by adapting fetch_remote_file() method on wordpress-importer.php:981 to use the WP_Http class:

    	function fetch_remote_file( $url, $post ) {
    		// extract the file name and extension from the url
    		$file_name = basename( $url );
    
    		// get placeholder file in the upload dir with a unique, sanitized filename
    		$upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
    		if ( $upload['error'] )
    			return new WP_Error( 'upload_dir_error', $upload['error'] );
    
    		// fetch the remote url and write it to the placeholder file
    		$wp_http = new WP_Http();
    		$response = $wp_http->request( $url, [ 'stream' => true, 'filename' => $upload['file'] ] );
    		$headers = $response['headers'];
    
    		// request failed
    		if ( ! $response ) {
    			@unlink( $upload['file'] );
    			return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
    		}
    
    		// make sure the fetch was successful
    		if ( $response['response']['code'] != '200' ) {
    			@unlink( $upload['file'] );
    			return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
    		}
    
    		$filesize = filesize( $upload['file'] );
    
    		if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
    			@unlink( $upload['file'] );
    			return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
    		}
    
    		if ( 0 == $filesize ) {
    			@unlink( $upload['file'] );
    			return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
    		}
    
    		$max_size = (int) $this->max_attachment_size();
    		if ( ! empty( $max_size ) && $filesize > $max_size ) {
    			@unlink( $upload['file'] );
    			return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
    		}
    
    		// keep track of the old and new urls so we can substitute them later
    		$this->url_remap[$url] = $upload['url'];
    		$this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
    		// keep track of the destination if the remote url is redirected somewhere else
    		if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
    			$this->url_remap[$headers['x-final-location']] = $upload['url'];
    
    		return $upload;
    	}

    Hopefully this or similar can be included in the core code if the developer has time

Viewing 1 replies (of 1 total)