Skip to content

Commit

Permalink
Avoid storing whole file content in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Nov 22, 2018
1 parent e6b6e39 commit 9515c06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
37 changes: 35 additions & 2 deletions classes/Tools14.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,14 @@ public static function file_exists_cache($filename)
return self::$file_exists_cache[$filename];
}

/**
* Check config & source file to settle which dl method to use
*/
public static function shouldUseFopen($url)
{
return in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url);
}

public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 5)
{
if (!extension_loaded('openssl') and strpos('https://', $url) === true) {
Expand All @@ -1396,7 +1404,7 @@ public static function file_get_contents($url, $use_include_path = false, $strea
if ($stream_context == null && preg_match('/^https?:\/\//', $url)) {
$stream_context = @stream_context_create(array('http' => array('timeout' => $curl_timeout, 'header' => "User-Agent:MyAgent/1.0\r\n")));
}
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url)) {
if (self::shouldUseFopen($url)) {
$var = @file_get_contents($url, $use_include_path, $stream_context);

/* PSCSX-3205 buffer output ? */
Expand Down Expand Up @@ -2435,12 +2443,37 @@ public static function nl2br($str)
return str_replace(array("\r\n", "\r", "\n"), '<br />', $str);
}

/**
* Copy a file to another place
*
* @return bool True if the copy succeded
*/
public static function copy($source, $destination, $stream_context = null)
{
if (is_null($stream_context) && !preg_match('/^https?:\/\//', $source)) {
return @copy($source, $destination);
}

return @file_put_contents($destination, self::file_get_contents($source, false, $stream_context, 60));
$destFile = fopen($destination, 'wb');
if (!is_resource($destFile)) {
return false;
}

if (self::shouldUseFopen($source)) {
$sourceFile = fopen($source);
// If something else than false, the data was stored
$result = (file_put_contents($destination, $sourceFile) !== false);
fclose($sourceFile);
} elseif (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_FILE, $destFile);
$result = curl_exec($ch);
curl_close($ch);
}

fclose($destFile);

return $result;
}
}
11 changes: 10 additions & 1 deletion classes/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace PrestaShop\Module\AutoUpgrade;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Configuration;

class Upgrader
Expand Down Expand Up @@ -104,7 +106,14 @@ public function downloadLast($dest, $filename = 'prestashop.zip')

$destPath = realpath($dest) . DIRECTORY_SEPARATOR . $filename;

Tools14::copy($this->link, $destPath);
try {
$filesystem = new Filesystem();
$filesystem->copy($this->link, $destPath);
} catch (IOException $e) {
// If the Symfony filesystem failed, we can try with
// the legacy method which uses curl.
Tools14::copy($this->link, $destPath);
}

return is_file($destPath);
}
Expand Down

0 comments on commit 9515c06

Please sign in to comment.