Skip to content

Commit

Permalink
Fixed #847
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybrad committed May 24, 2022
1 parent 47c6461 commit 2ed1196
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
55 changes: 25 additions & 30 deletions src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).');
}
}
}

Expand Down

0 comments on commit 2ed1196

Please sign in to comment.