From 2ed1196326d46c86d8eb034dd8ab35411d3c6566 Mon Sep 17 00:00:00 2001 From: Brad Bell Date: Tue, 24 May 2022 16:06:45 -0700 Subject: [PATCH] Fixed #847 --- CHANGELOG.md | 6 +++++ src/fields/Assets.php | 55 ++++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4aa8e05..7c6ec2fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes for Feed Me +## Unreleased + +### Fixed +- Fixed a PHP error that could occur when importing a base64-encoded asset. +- Fixed a bug where asset file names were getting normalized before searching for an existing asset when the feed specified a file path. ([#847](https://github.com/craftcms/feed-me/issues/847)) + ## 5.0.3 - 2022-05-17 ### Fixed diff --git a/src/fields/Assets.php b/src/fields/Assets.php index e0aee905..5775d298 100644 --- a/src/fields/Assets.php +++ b/src/fields/Assets.php @@ -130,48 +130,43 @@ public function parseField(): mixed } } - // If we're uploading files, this will need to be an absolute URL. If it is, save until later. - // We also don't check for existing assets here, so break out instantly. - if ($upload && UrlHelper::isAbsoluteUrl($dataValue)) { - $urlsToUpload[$key] = $dataValue; - - // If we're opting to use the already uploaded asset, we can check here - if ($conflict === AssetElement::SCENARIO_INDEX) { - $dataValue = AssetHelper::getRemoteUrlFilename($dataValue); - } - } - // Check if the URL is actually a base64 encoded file. - $matches = []; preg_match('/^data:\w+\/\w+;base64,/i', $dataValue, $matches); if ($upload && count($matches) > 0) { $base64ToUpload[$key] = $dataValue; - } - - $filename = AssetsHelper::prepareAssetName($dataValue); + } else { + // If we're uploading files, this will need to be an absolute URL. If it is, save until later. + // We also don't check for existing assets here, so break out instantly. + if ($upload && UrlHelper::isAbsoluteUrl($dataValue)) { + $urlsToUpload[$key] = $dataValue; + $filename = AssetHelper::getRemoteUrlFilename($dataValue); + } else { + $filename = basename($dataValue); + } - $criteria['status'] = null; - $criteria['folderId'] = $folderIds; - $criteria['kind'] = $settings['allowedKinds']; - $criteria['limit'] = $limit; - $criteria['filename'] = $filename; - $criteria['includeSubfolders'] = true; + $criteria['status'] = null; + $criteria['folderId'] = $folderIds; + $criteria['kind'] = $settings['allowedKinds']; + $criteria['limit'] = $limit; + $criteria['filename'] = $filename; + $criteria['includeSubfolders'] = true; - Craft::configure($query, $criteria); + Craft::configure($query, $criteria); - Plugin::info('Search for existing asset with query `{i}`', ['i' => Json::encode($criteria)]); + Plugin::info('Search for existing asset with query `{i}`', ['i' => Json::encode($criteria)]); - $ids = $query->ids(); - $foundElements = array_merge($foundElements, $ids); + $ids = $query->ids(); + $foundElements = array_merge($foundElements, $ids); - Plugin::info('Found `{i}` existing assets: `{j}`', ['i' => count($foundElements), 'j' => Json::encode($foundElements)]); + Plugin::info('Found `{i}` existing assets: `{j}`', ['i' => count($foundElements), 'j' => Json::encode($foundElements)]); - // Are we uploading, and did we find existing assets? No need to process - if ($upload && $ids && $conflict === AssetElement::SCENARIO_INDEX) { - unset($urlsToUpload[$key]); + // Are we uploading, and did we find existing assets? No need to process + if ($upload && $ids && $conflict === AssetElement::SCENARIO_INDEX) { + unset($urlsToUpload[$key]); - Plugin::info('Skipping asset upload (already exists).'); + Plugin::info('Skipping asset upload (already exists).'); + } } }