Skip to content

Commit

Permalink
(fix) Bypass Craft for filename overwrite
Browse files Browse the repository at this point in the history
Turns out this is the most effective way to 'force' our filename
convention. Changing the filename of the assets triggers a cascades of
barrier and code to handle a change of filename, but it this case, we
just need to overwrite it.

We also need to check for the Volume's FS, not the transform one.

This api will change has the function as no reason this live there.
  • Loading branch information
nitriques committed Oct 31, 2024
1 parent 704352a commit 18b4ce2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ function(\craft\events\ModelEvent $event) {
if (ElementHelper::isDraftOrRevision($asset)) {
return;
}
if ($asset->getVolume()->getTransformFs() instanceof \deuxhuithuit\cfimages\fs\CloudflareImagesFs) {
if (!$this->isNewImageAsset($asset)) {
return;
}
if ($this->isAssetOnCloudflareImagesVolume($asset)) {
/** @var \deuxhuithuit\cfimages\fs\CloudflareImagesFs */
$fs = $asset->getVolume()->getTransformFs();
$fs = $asset->getVolume()->getFs();
$fs->saveAsset($asset);
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/behaviors/CloudflareImagesAssetBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

class CloudflareImagesAssetBehavior extends Behavior
{
/**
* @var the id of the file
*/
public string $cfId = '';

/**
* @return string
*/
Expand Down
23 changes: 16 additions & 7 deletions src/fs/CloudflareImagesFs.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ public function writeFileFromStream(string $path, $stream, array $config = []):
*/
public function saveAsset(Asset $asset): void
{
// Have we already saved this instance?
if ($asset->cfId) {
return;
}
// Make sure we have a recent id for this path
$recentId = $this->recentFiles[$asset->getPath()] ?? null;
if (!$recentId) {
Expand All @@ -184,10 +180,23 @@ public function saveAsset(Asset $asset): void
if ($properFilename === $asset->filename) {
return;
}
$asset->cfId = $recentId;

// Update in-memory values
$asset->filename = $properFilename;
$asset->setScenario(Asset::SCENARIO_DEFAULT);
\Craft::$app->getElements()->saveElement($asset, true, true, false);

// We need to bypass Craft's logic to rename the asset because it will try
// to manipulate the FileSystem's representation of the asset, which we don't want.
$result = \Craft::$app->getDb()
->createCommand()
->update('{{%assets}}', ['filename' => $properFilename], ['id' => $asset->id])
->execute();

if (!$result) {
throw new \Exception('Failed to rename Cloudflare Images asset.');
}

// Then we need to update the asset's indexes to make Craft happy
\Craft::$app->getSearch()->indexElementAttributes($asset, ['filename']);
}

/**
Expand Down

0 comments on commit 18b4ce2

Please sign in to comment.