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

Add plugin setting for SIH distribution URL #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ After setting up the S3 volume follow these instructions to install the plugin.

4. Add `Toggle aws image processor` to your s3 volume field layout.

## Plugin Settings

` Serverless Distribution URL ` (optional)

This field is for the Serverless Image Handler CloudFront Distribution URL, which frees up your asset volume distribution URL to be a regular CloudFront distribution URL. This means that any asset in the volume that does not pass through this plugin (rich text fields, certain SEO plugins, etc) will be served normally.

## Usage

In your twig template you can use:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dutchheight/aws-serverless-image-handler",
"description": "Craft CMS plugin to generate resource URL",
"type": "craft-plugin",
"version": "1.1.3",
"version": "1.1.4",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -32,7 +32,7 @@
"extra": {
"name": "aws-serverless-image-handler",
"handle": "aws-serverless-image-handler",
"hasCpSettings": false,
"hasCpSettings": true,
"hasCpSection": false,
"changelogUrl": "???",
"class": "dutchheight\\awsserverlessimagehandler\\Awsserverlessimagehandler"
Expand Down
24 changes: 22 additions & 2 deletions src/Awsserverlessimagehandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

use craft\web\twig\variables\CraftVariable;


use dutchheight\awsserverlessimagehandler\models\Settings;
use dutchheight\awsserverlessimagehandler\fields\ImageProperties;
use dutchheight\awsserverlessimagehandler\variables\Variable;
use dutchheight\awsserverlessimagehandler\services\HelperService;
Expand Down Expand Up @@ -54,6 +54,11 @@ class Awsserverlessimagehandler extends Plugin
*/
public $schemaVersion = '1.0.0';

/**
* @var bool
*/
public $hasCpSettings = true;

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -82,7 +87,7 @@ public function getHelpers(): HelperService

// Protected Methods
// =========================================================================

protected function registerComponents() {
$this->setComponents([
'helpers' => HelperService::class
Expand Down Expand Up @@ -114,4 +119,19 @@ function (Event $event) {
}
);
}

protected function createSettingsModel()
{
return new Settings();
}

protected function settingsHtml()
{
return Craft::$app->view->renderTemplate(
'aws-serverless-image-handler/settings',
[
'settings' => $this->getSettings()
]
);
}
}
25 changes: 25 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace dutchheight\awsserverlessimagehandler\models;

use dutchheight\awsserverlessimagehandler\Awsserverlessimagehandler;

use Craft;
use craft\base\Model;

class Settings extends Model
{
public $serverlessDistributionURL = '';

public function rules()
{
return [
['serverlessDistributionURL', 'string'],
];
}

public function getSecretKey(): string
{
return Craft::parseEnv($this->serverlessDistributionURL);
}
}
12 changes: 12 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% import "_includes/forms" as forms %}

{{ forms.autosuggestField({
label: 'Serverless Distribution URL',
instructions: 'Enter your AWS Serverless Image Handler CloudFront Distribution URL here.',
name: 'serverlessDistributionURL',
id: 'serverlessDistributionURL',
required: false,
value: settings.serverlessDistributionURL,
suggestEnvVars: true,
suggestAliases: true,
}) }}
9 changes: 6 additions & 3 deletions src/variables/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public function getImgUrl(Asset $image, array $edits = null, bool $allowWebP = t
return $image->url;
}

$volumeSubfolder = (Craft::parseEnv($image->getVolume()->subfolder) ?: $image->getVolume()->subfolder);
$distributionSettingUrl = Craft::parseEnv(Awsserverlessimagehandler::$plugin->settings->serverlessDistributionURL);
$volumeUrl = Craft::parseEnv($image->volume->url);
$distributionUrl = $distributionSettingUrl ?: $volumeUrl;
$volumeSubfolder = Craft::parseEnv($image->getVolume()->subfolder);

$json = [
"bucket" => $image->getVolume()->bucket,
"key" => $volumeSubfolder ? $volumeSubfolder . "/" . $image['filename'] : $image['filename'],
"key" => $volumeSubfolder ? $volumeSubfolder . "/" . $image->getPath() : $image->getPath(),
"edits" => [
"resize" => [
"fit" => (isset($edits['fit']) ? $edits['fit'] : "cover"),
Expand Down Expand Up @@ -97,6 +100,6 @@ public function getImgUrl(Asset $image, array $edits = null, bool $allowWebP = t
$json["edits"]["webp"] = [];
}

echo (Craft::parseEnv($image->volume->url) ?: $image->volume->url) . base64_encode(json_encode($json));
echo $distributionUrl . base64_encode(json_encode($json));
}
}