pim-community-dev icon indicating copy to clipboard operation
pim-community-dev copied to clipboard

Allow external image URL in import instead of only path to local file.

Open ankit-sf opened this issue 6 years ago • 11 comments

I'm submitting a Feature Request Allow image URL in import instead of the only path to a local file.

Currently: accepted only relative path to files like /tmp/abc.png

Want: accept external URLs also in import file like http://www.example.com/abc.png

ankit-sf avatar Nov 26 '18 14:11 ankit-sf

Hi, I would be a very very nice christmas gift :+1:

grabelle avatar Dec 13 '18 08:12 grabelle

Is this feature released ??

afserali avatar Oct 19 '19 09:10 afserali

It would be very useful

elauna avatar Dec 18 '19 14:12 elauna

`<?php

namespace YOUR\NAME\SPACE; // todo: <----------------

use Akeneo\Tool\Component\Connector\Reader\File\MediaPathTransformer as BaseMediaPathTransformer;

class MediaPathTransformer extends BaseMediaPathTransformer { public function transform(array $attributeValues, $filePath) { $mediaAttributes = $this->attributeRepository->findMediaAttributeCodes();

    foreach ($attributeValues as $code => $values) {
        if (in_array($code, $mediaAttributes)) {
            foreach ($values as $index => $value) {
                if (isset($value['data'])) {
                    $dataFilePath = $value['data'];
                    $attributeValues[$code][$index]['data'] = $dataFilePath ? $this->getPath($filePath, $dataFilePath) : null;
                }
            }
        }
    }

    return $attributeValues;
}

/**
 * @param $filePath
 * @param $data
 * @return string
 */
protected function getPath($filePath, $data)
{
    if (filter_var($data, FILTER_VALIDATE_URL)) {
        return $this->download($data);
    }

    return sprintf('%s%s%s', $filePath, DIRECTORY_SEPARATOR, $data);
}

/**
 * @param string $url
 * @return string|null
 */
protected function download(string $url)
{
    $parsedUrl = parse_url($url);
    $dir = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, $parsedUrl['host']);
    $filename = sprintf('%s.%s', sha1($url), pathinfo($parsedUrl['path'], PATHINFO_EXTENSION));
    $path = sprintf('%s%s%s', $dir, DIRECTORY_SEPARATOR, $filename);

    try {
        $content = file_get_contents($url);

        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        file_put_contents($path, $content);
    } catch (\Exception $e) {
        return null;
    }

    return $path;
}

} `

userz58 avatar Mar 23 '20 19:03 userz58

`<?php

namespace YOUR\NAME\SPACE; // todo: <----------------

use Akeneo\Tool\Component\Connector\Reader\File\MediaPathTransformer as BaseMediaPathTransformer;

class MediaPathTransformer extends BaseMediaPathTransformer { public function transform(array $attributeValues, $filePath) { $mediaAttributes = $this->attributeRepository->findMediaAttributeCodes();

    foreach ($attributeValues as $code => $values) {
        if (in_array($code, $mediaAttributes)) {
            foreach ($values as $index => $value) {
                if (isset($value['data'])) {
                    $dataFilePath = $value['data'];
                    $attributeValues[$code][$index]['data'] = $dataFilePath ? $this->getPath($filePath, $dataFilePath) : null;
                }
            }
        }
    }

    return $attributeValues;
}

/**
 * @param $filePath
 * @param $data
 * @return string
 */
protected function getPath($filePath, $data)
{
    if (filter_var($data, FILTER_VALIDATE_URL)) {
        return $this->download($data);
    }

    return sprintf('%s%s%s', $filePath, DIRECTORY_SEPARATOR, $data);
}

/**
 * @param string $url
 * @return string|null
 */
protected function download(string $url)
{
    $parsedUrl = parse_url($url);
    $dir = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, $parsedUrl['host']);
    $filename = sprintf('%s.%s', sha1($url), pathinfo($parsedUrl['path'], PATHINFO_EXTENSION));
    $path = sprintf('%s%s%s', $dir, DIRECTORY_SEPARATOR, $filename);

    try {
        $content = file_get_contents($url);

        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        file_put_contents($path, $content);
    } catch (\Exception $e) {
        return null;
    }

    return $path;
}

} `

@userz58 Awesome! How to use this? Where would I need to put this in order for Akeneo to include the changes?

t1ll avatar Apr 17 '20 09:04 t1ll

@userz58 Awesome! How to use this? Where would I need to put this in order for Akeneo to include the changes?

you can create your own product import job and use custom media path generator, or you can override only one default service pim_connector.reader.file.media_path_transformer

https://docs.akeneo.com/4.0/import_and_export_data/product-import.html

userz58 avatar Apr 17 '20 13:04 userz58

Hi ! I would like to know if this feature request is part of a roadmap?

Thanks

grimabe avatar Oct 07 '20 21:10 grimabe

@userz58 Awesome! How to use this? Where would I need to put this in order for Akeneo to include the changes?

you can create your own product import job and use custom media path generator, or you can override only one default service pim_connector.reader.file.media_path_transformer

https://docs.akeneo.com/4.0/import_and_export_data/product-import.html

Awesome it works. Now is it possible to do it also for export? so the exported CSV file will contain the URL of the picture on the PIM server?

nivsp avatar May 03 '21 08:05 nivsp

Hi, i didn't know how should i make change ! , Is that correct ? image

Thanks in advance

Fatma3011 avatar May 23 '22 09:05 Fatma3011

We have used the external image URL in our plugin to import the csv file. https://store.webkul.com/akeneo-csv-import-module.html

navneetbhardwaj avatar May 26 '22 08:05 navneetbhardwaj

But this plugin is payed :/

Fatma3011 avatar May 27 '22 13:05 Fatma3011