Skip to content

Commit

Permalink
Merge branch '1.x' into feature/validate-command
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Helper.php
  • Loading branch information
timkelty committed Mar 23, 2024
2 parents da75423 + 9a24708 commit df7b249
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 133 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "craftcms/cloud",
"type": "yii2-extension",
"require": {
"php": "^8.1",
"bref/bref": "2.1.9",
"bref/extra-php-extensions": "1.3.2",
"craftcms/cms": "^4.6",
Expand Down
28 changes: 0 additions & 28 deletions src/Cdn.php

This file was deleted.

1 change: 1 addition & 0 deletions src/HeaderEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ enum HeaderEnum: string
case CACHE_CONTROL = 'Cache-Control';
case AUTHORIZATION = 'Authorization';
case DEV_MODE = 'Dev-Mode';
case REQUEST_TYPE = 'Request-Type';
}
35 changes: 9 additions & 26 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function createSigningContext(iterable $headers = []): Context
'hmac' => Module::getInstance()->getConfig()->signingKey,
],
'algorithm' => 'hmac-sha256',
'headers' => $headers->push('(request-target)')->all(),
'headers' => $headers->all(),
]);
}

Expand All @@ -178,42 +178,25 @@ public static function validateProjectTypeForEdition(string $projectType, string
return true;
}

public static function makeCdnApiRequest(Collection $headers): ResponseInterface
{
if (!Helper::isCraftCloud()) {
throw new Exception('CDN API requests are only supported in a Craft Cloud environment.');
}

if (Module::getInstance()->getConfig()->getDevMode()) {
$headers->put(HeaderEnum::DEV_MODE->value, '1');
}

$context = Helper::createSigningContext($headers->keys());
$request = new Request(
'HEAD',
(string) Module::getInstance()->getConfig()->cdnBaseUrl,
$headers->all(),
);

return Craft::createGuzzleClient()->send(
$context->signer()->sign($request)
);
}

public static function makeGatewayApiRequest(Collection $headers): ResponseInterface
public static function makeGatewayApiRequest(iterable $headers): ResponseInterface
{
if (!Helper::isCraftCloud()) {
throw new Exception('Gateway API requests are only supported in a Craft Cloud environment.');
}

$headers = Collection::make($headers)
->put(HeaderEnum::REQUEST_TYPE->value, 'api');

if (Module::getInstance()->getConfig()->getDevMode()) {
$headers->put(HeaderEnum::DEV_MODE->value, '1');
}

$url = Module::getInstance()->getConfig()->getPreviewDomainUrl();
$url = Craft::$app->getRequest()->getIsConsoleRequest()
? Module::getInstance()->getConfig()->getPreviewDomainUrl()
: Craft::$app->getRequest()->getHostInfo();

if (!$url) {
throw new Exception('Gateway API requests require a configured preview domain.');
throw new Exception('Gateway API requests require a URL.');
}

$context = Helper::createSigningContext($headers->keys());
Expand Down
4 changes: 0 additions & 4 deletions src/ImageTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public function getTransformUrl(Asset $asset, ImageTransform $imageTransform, bo
$assetUrl = Html::encodeSpaces(Assets::generateUrl($fs, $this->asset));
$mimeType = $asset->getMimeType();

if (!$fs->hasUrls) {
throw new NotSupportedException('The asset’s volume’s transform filesystem doesn’t have URLs.');
}

if ($mimeType === 'image/gif' && !Craft::$app->getConfig()->getGeneral()->transformGifs) {
throw new NotSupportedException('GIF files shouldn’t be transformed.');
}
Expand Down
14 changes: 4 additions & 10 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function bootstrap($app): void
FallbackTransformer::class,
ImageTransformer::class,
);

if ($app->getRequest()->getIsCpRequest()) {
$app->getView()->registerAssetBundle(UploaderAsset::class);
}
}
}

Expand Down Expand Up @@ -132,10 +136,6 @@ protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void
$app->getRequest()->secureHeaders = Collection::make($app->getRequest()->secureHeaders)
->reject(fn(string $header) => $header === 'X-Forwarded-Host')
->all();

if ($app->getRequest()->getIsCpRequest()) {
$app->getView()->registerAssetBundle(UploaderAsset::class);
}
}

/** @var Dispatcher $dispatcher */
Expand Down Expand Up @@ -173,7 +173,6 @@ protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void

$this->setComponents([
'staticCache' => StaticCache::class,
'cdn' => Cdn::class,
]);

$this->registerCloudEventHandlers();
Expand Down Expand Up @@ -261,11 +260,6 @@ protected function validateConfig(): void
}
}

public function getCdn(): Cdn
{
return $this->get('cdn');
}

public function getStaticCache(): StaticCache
{
return $this->get('staticCache');
Expand Down
2 changes: 2 additions & 0 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function purgeAll(): void

protected function prepareTags(iterable $tags): Collection
{
Craft::info(new PsrMessage('Preparing tags', Collection::make($tags)->all()));

// Header value can't exceed 16KB
// https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#a-few-things-to-remember
$bytes = 0;
Expand Down
5 changes: 2 additions & 3 deletions src/cli/controllers/AssetBundlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use craft\cloud\AssetManager;
use craft\cloud\cli\AssetBundlePublisher;
use craft\cloud\Composer;
use craft\cloud\Helper;
use craft\console\Controller;
use craft\helpers\App;
use ReflectionClass;
Expand All @@ -28,8 +27,8 @@ public function init(): void

public function beforeAction($action): bool
{
if (Helper::isCraftCloud()) {
throw new Exception('Asset bundle publishing is not supported in a Craft Cloud environment.');
if (App::isEphemeral()) {
throw new Exception('Asset bundle publishing is not supported in ephemeral environments.');
}

if (App::env('CRAFT_NO_DB')) {
Expand Down
66 changes: 66 additions & 0 deletions src/cli/controllers/AssetsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace craft\cloud\cli\controllers;

use craft\cloud\fs\Fs;
use craft\console\Controller;
use craft\elements\Asset;
use craft\helpers\FileHelper;
use yii\base\Exception;
use yii\console\ExitCode;

class AssetsController extends Controller
{
/**
* @var array<string>|null
*/
public ?array $volume = null;

/**
* @var array<int>|null
*/
public ?array $assetId = null;

public function options($actionID): array
{
return array_merge(parent::options($actionID), match ($actionID) {
'replace-metadata' => ['volume', 'assetId'],
default => []
});
}

public function actionReplaceMetadata(): int
{
$assets = Asset::find()
->volume($this->volume)
->id($this->assetId)
->collect();

$assets->each(function(Asset $asset) {
$this->do(
"Replacing metadata for `$asset->path`",
fn() => $this->replaceAssetMetadata($asset),
);
});

return ExitCode::OK;
}

protected function replaceAssetMetadata(Asset $asset): void
{
$fs = $asset->getVolume()->getFs();

if (!$fs instanceof Fs) {
throw new Exception('Invalid filesystem type.');
}

$path = $asset->getPath();

$config = [
'ContentType' => FileHelper::getMimeType($path),
'MetadataDirective' => 'REPLACE',
];

$fs->replaceMetadata($path, $config);
}
}
35 changes: 0 additions & 35 deletions src/cli/controllers/CdnController.php

This file was deleted.

Loading

0 comments on commit df7b249

Please sign in to comment.