Skip to content

Commit

Permalink
FEATURE: More resilient asset resource sync
Browse files Browse the repository at this point in the history
Previously in order to replace an asset resource, the `PersistentResource`s for
all imported assets where re-imported, no matter whether they existed already.

With this change, we still download the file contents (since there is no other
way to compare asset proxy file contents currently) but we only replace ths
`PersistentResource` if it differs from the previously imported file.
  • Loading branch information
bwaidelich committed May 20, 2022
1 parent a22ef46 commit f819df0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions Classes/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Wwwision\AssetSync;

use Neos\Flow\Persistence\QueryInterface;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface;
Expand Down Expand Up @@ -85,7 +86,7 @@ private function updateAsset(Preset $preset, ImportedAsset $importedAsset): void
} catch (\Throwable $e) {
$this->dispatch(self::EVENT_ERROR, sprintf('Failed to replace resource for imported asset "%s": %s', $importedAsset->getLocalAssetIdentifier(), $e->getMessage()));
}
$this->dispatch(self::EVENT_PROGRESS, $importedAsset, $metadataWasUpdated, $resourceWasReplaced);
$this->dispatch(self::EVENT_PROGRESS, $importedAsset, $metadataWasUpdated ?? null, $resourceWasReplaced ?? false);
}

public function onStart(callable $handler): void
Expand Down Expand Up @@ -135,14 +136,28 @@ private function replaceAssetResource(AssetInterface $localAsset, AssetProxyInte
if ($localAsset instanceof ImageVariant) {
return false;
}
$resourceWasReplaced = false;
$assetResource = $this->resourceManager->importResource($assetProxy->getImportStream());
if ($assetResource->getFilename() !== $assetProxy->getFilename()) {
$assetResource->setFilename($assetProxy->getFilename());
$resourceWasReplaced = true;
$assetProxyStream = $assetProxy->getImportStream();
if (!\is_resource($assetProxyStream)) {
throw new \RuntimeException(sprintf('Failed to open stream for remote asset "%s" from asset source "%s"', $assetProxy->getIdentifier(), $assetProxy->getAssetSource()->getIdentifier()), 1653054583);
}
$this->assetService->replaceAssetResource($localAsset, $assetResource);
return $resourceWasReplaced;
$assetProxyContents = stream_get_contents($assetProxyStream);
if (!\is_string($assetProxyContents)) {
throw new \RuntimeException(sprintf('Failed to read stream for remote asset "%s" from asset source "%s"', $assetProxy->getIdentifier(), $assetProxy->getAssetSource()->getIdentifier()), 1653054707);
}
fclose($assetProxyStream);
$assetProxySha1 = sha1($assetProxyContents);
if ($localAsset->getResource()->getSha1() === $assetProxySha1) {
return false;
}
$assetProxyResource = $this->resourceManager->getResourceBySha1($assetProxySha1);
if ($assetProxyResource === null) {
$assetProxyResource = $this->resourceManager->importResourceFromContent($assetProxyContents, $assetProxy->getFilename(), $localAsset->getResource()->getCollectionName());
}
if (!$assetProxyResource instanceof PersistentResource) {
return false;
}
$this->assetService->replaceAssetResource($localAsset, $assetProxyResource);
return true;
}

private function importedAssetsForPreset(Preset $preset): QueryInterface
Expand Down

0 comments on commit f819df0

Please sign in to comment.