yireo / Yireo_NextGenImages

44 stars 26 forks source link

Function getFilenameFromUrl and isLocal not working on multistore #24

Closed mibesoft closed 6 months ago

mibesoft commented 2 years ago

I found module not convert some image in same magento source but different domain. I added some code to convert image for multistores

`public function isLocal(string $url): bool { if (!preg_match('/^https?:\/\//', $url)) { return true; } $stores = $this->storeManager->getStores(); foreach($stores as $store) {

        if (strpos($url, $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)) !== false) {
            return true;
        }
    }

    try {

        foreach($stores as $store) {

            if (strpos($url, $this->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)) !== false) {
                return true;
            }

        }

    } catch (NoSuchEntityException $e) {
        return false;
    }

    return false;
}

/**
 * @param string $url
 * @return string
 */
public function getFilenameFromUrl(string $url): string
{
    $url = preg_replace('/\/static\/version([0-9]+\/)/', '/static/', (string)$url);

    $stores = $this->storeManager->getStores();

    if ($this->isLocal($url) === false) {
        throw new NotFoundException((string)__('URL "' . $url . '" does not appear to be a local file'));
    }

    try {
        foreach($stores as $store) {
            if (strpos($url, $this->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)) !== false) {
                return str_replace($this->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA), $this->getMediaFolder() . '/', $url);
            }
        }

    } catch (FileSystemException | NoSuchEntityException $e) {
        throw new NotFoundException((string)__('Media folder does not exist'));
    }

    foreach($stores as $store) {
        if (strpos($url, $store->getBaseUrl()) !== false) {
            return str_replace($store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB), $this->getBaseFolder() . '/', $url);
        }
    }

    if (preg_match('/^\//', $url)) {
        return $this->getBaseFolder() . $url;
    }

    throw new NotFoundException((string)__('URL "' . $url . '" is not matched with a local file'));
}`
jissereitsma commented 2 years ago

Thanks for your suggestion. There is a refactoring underway, so that this will need to be applied in a different way. Also, I'm a bit worried about the performance of looping through StoreViews - what if there is a large number of those StoreViews? This therefore also needs to be applied optionally (through a system setting) somehow. Anyway, let me get back to you, once the mentioned refactoring is done for.

jissereitsma commented 1 year ago

I'll merge your idea anyway. The performance impact is actually very very very little, so that's totally fine. And your scenario and fix are quite easy to follow and implement. The next release - with this fix - will be available in a few hours.

jissereitsma commented 6 months ago

I'm closing this ticket due to inactivity.