Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image dimensions not updated when replacing an image #48

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ImageTransformer.php
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ private function getFitValue(ImageTransform $imageTransform): string
'fit' => $imageTransform->upscale ? 'contain' : 'scale-down',
'stretch' => 'cover',
'letterbox' => 'pad',
default => $imageTransform->upscale ? 'cover' : 'crop',
default => 'crop',
};
}

16 changes: 14 additions & 2 deletions src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
@@ -235,6 +235,14 @@ public function actionReplaceFile(): Response
$sourceAssetId = $this->request->getBodyParam('sourceAssetId');
$filename = $this->request->getBodyParam('filename');
$targetFilename = $this->request->getBodyParam('targetFilename');
$size = $this->request->getBodyParam('size');
$width = $this->request->getBodyParam('width');
$height = $this->request->getBodyParam('height');
$lastModifiedMs = (int) $this->request->getBodyParam('lastModified');
$dateModified = $lastModifiedMs
? DateTime::createFromFormat('U', (string) floor($lastModifiedMs / 1000))
: new DateTime();

$assets = Craft::$app->getAssets();

// Must have at least one existing asset (source or target).
@@ -260,6 +268,10 @@ public function actionReplaceFile(): Response

// Handle the Element Action
if ($assetToReplace !== null && $filename) {
$assetToReplace->width = $width;
$assetToReplace->height = $height;
$assetToReplace->size = $size;
$assetToReplace->dateModified = $dateModified;
if (!$this->replaceAssetFile($assetToReplace, $filename, $targetFilename)) {
throw new Exception('Unable to replace asset.');
}
@@ -348,9 +360,9 @@ public function replaceAssetFile(Asset $asset, string $filename, string $targetF
$asset->getVolume()->deleteFile($oldPath);

// Try again, in case the resulting filename has a tmp suffix from `avoidFilenameConflicts`
if ($saved && $oldPath !== $asset->getPath()) {
if ($saved && $targetFilename !== $asset->getFilename()) {
$asset->newFilename = $targetFilename;
$saved = Craft::$app->getElements()->saveElement($asset);
$saved = $this->saveAsset($asset);
}

if ($assets->hasEventHandlers($assets::EVENT_AFTER_REPLACE_ASSET)) {
15 changes: 0 additions & 15 deletions src/runtime/event/SqsHandler.php
Original file line number Diff line number Diff line change
@@ -18,10 +18,7 @@ public function handleSqs(SqsEvent $event, Context $context): void
$this->context = $context;
$records = Collection::make($event->getRecords());

echo "Processing SQS event with {$records->count()} records.";

$records->each(function(SqsRecord $record) {
echo "Handling SQS message: #{$record->getMessageId()}";
$cliHandler = new CliHandler();
$body = json_decode(
$record->getBody(),
@@ -39,8 +36,6 @@ public function handleSqs(SqsEvent $event, Context $context): void
'command' => "cloud/queue/exec {$jobId}",
], $this->context, true);
} catch (ProcessFailedException $e) {
echo "Job #$jobId failed and WILL NOT be retried:\n";
echo "Message: #{$record->getMessageId()}\n";
echo $e->getMessage();

$this->failJob(
@@ -50,17 +45,12 @@ public function handleSqs(SqsEvent $event, Context $context): void
);
} catch (ProcessTimedOutException $e) {
if ($cliHandler->shouldRetry()) {
echo "Job #$jobId timed out and WILL be retried via markAsFailed:\n";
echo "Message: #{$record->getMessageId()}\n";
echo $e->getMessage();

$this->markAsFailed($record);
return;
}

echo "Job #$jobId timed out and WILL NOT be retried:\n";
echo "Message: #{$record->getMessageId()}\n";
echo "Running Time: {$cliHandler->getTotalRunningTime()} seconds\n";
echo $e->getMessage();

$this->failJob(
@@ -69,12 +59,8 @@ public function handleSqs(SqsEvent $event, Context $context): void
'Job exceeded maximum running time: 15 minutes',
);
} catch (\Throwable $e) {
echo "Job #$jobId threw and WILL be retried via markAsFailed:\n";
echo "Message: #{$record->getMessageId()}\n";
echo $e->getMessage();
$this->markAsFailed($record);
} finally {
echo "Done processing job #$jobId (finally).";
}
});
}
@@ -86,7 +72,6 @@ protected function failJob(string $jobId, SqsRecord $record , string $message =
'command' => "cloud/queue/fail {$jobId} --message={$message}",
], $this->context, true);
} catch (\Throwable $e) {
echo "Exception thrown running cloud/queue/fail:\n";
echo $e->getMessage();

// Attempt to retry the whole message