pim-community-dev
pim-community-dev copied to clipboard
Allow external image URL in import instead of only path to local file.
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
Hi, I would be a very very nice christmas gift :+1:
Is this feature released ??
It would be very useful
`<?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;
}
} `
`<?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?
@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
Hi ! I would like to know if this feature request is part of a roadmap?
Thanks
@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?
Hi,
i didn't know how should i make change ! , Is that correct ?
Thanks in advance
We have used the external image URL in our plugin to import the csv file. https://store.webkul.com/akeneo-csv-import-module.html
But this plugin is payed :/