Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Fixed issue Unable to open URL for downloadable product #21262

Merged
Merged
37 changes: 25 additions & 12 deletions app/code/Magento/Downloadable/Helper/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/**
* Downloadable Products Download Helper
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Download extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -188,19 +189,20 @@ public function getFileSize()
public function getContentType()
{
$this->_getHandle();
if ($this->_linkType == self::LINK_TYPE_FILE) {
if (function_exists(
'mime_content_type'
) && ($contentType = mime_content_type(
$this->_workingDirectory->getAbsolutePath($this->_resourceFile)
))
if ($this->_linkType === self::LINK_TYPE_FILE) {
if (function_exists('mime_content_type')
&& ($contentType = mime_content_type(
$this->_workingDirectory->getAbsolutePath($this->_resourceFile)
))
) {
return $contentType;
} else {
return $this->_downloadableFile->getFileType($this->_resourceFile);
}
} elseif ($this->_linkType == self::LINK_TYPE_URL) {
return $this->_handle->stat($this->_resourceFile)['type'];
return $this->_downloadableFile->getFileType($this->_resourceFile);
}
if ($this->_linkType === self::LINK_TYPE_URL) {
return (is_array($this->_handle->stat($this->_resourceFile)['type'])
? end($this->_handle->stat($this->_resourceFile)['type'])
: $this->_handle->stat($this->_resourceFile)['type']);
}
return $this->_contentType;
}
Expand Down Expand Up @@ -254,10 +256,21 @@ public function setResource($resourceFile, $linkType = self::LINK_TYPE_FILE)
);
}
}

$this->_resourceFile = $resourceFile;

/**
* check header for urls
*/
if ($linkType === self::LINK_TYPE_URL) {
$headers = array_change_key_case(get_headers($this->_resourceFile, 1), CASE_LOWER);
if (isset($headers['location'])) {
$this->_resourceFile = is_array($headers['location']) ? current($headers['location'])
: $headers['location'];
}
}

$this->_linkType = $linkType;

return $this;
}

Expand Down
14 changes: 3 additions & 11 deletions lib/internal/Magento/Framework/Filesystem/Driver/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,18 @@ class Http extends File
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isExists($path)
{
$headers = array_change_key_case(get_headers($this->getScheme() . $path, 1), CASE_LOWER);

$status = $headers[0];

/* Handling 302 redirection */
if (strpos($status, '302 Found') !== false && isset($headers[1])) {
/* Handling 301 or 302 redirection */
if (isset($headers[1]) && preg_match('/30[12]/', $status)) {
$status = $headers[1];
}

if (strpos($status, '200 OK') === false) {
$result = false;
} else {
$result = true;
}

return $result;
return !(strpos($status, '200 OK') === false);
}

/**
Expand Down