Commit a24b81fc authored by hlarget's avatar hlarget 👹
Browse files

Merge branch 'release/2.1.0'

parents f6ba55b5 2a5c6ea3
<?php
/**
* Created by PhpStorm.
* User: albancrepel
* Date: 2019-12-12
* Time: 10:00
*/
namespace Aboutgoods\KonnectBundle;
class Configuration
{
const AUTHENTICATION = "authentication";
const IMGPROXY = "imgproxy";
private $configuration = [
self::AUTHENTICATION => [
"token" => "token",
"secret" => "secret",
"url" => null
],
self::IMGPROXY => [
"key" => "key",
"salt" => "salt",
"url" => null
],
];
public function getConfiguration($name)
{
return $this->configuration[$name];
}
/**
* This method is called in the KonnectExtension.php
* @param $config
*/
public function setConfiguration($config)
{
// Recursive replace with the config passed as parameter
foreach ($this->configuration as $key => $value) {
if(is_array($value)) {
foreach ($value as $subKey => $subValue) {
if (isset($config[$key][$subKey])) {
$this->configuration[$key][$subKey] = $config[$key][$subKey];
}
}
} else {
if (isset($config[$key])) {
$this->configuration[$key] = $config[$key];
}
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: albancrepel
* Date: 2019-12-09
* Time: 14:51
*/
namespace Aboutgoods\KonnectBundle\Controller;
use Aboutgoods\KonnectBundle\ImgProxyClient;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ResourceController extends AbstractController
{
/**
* Get a resource through our imgproxy server
* @Route("/resource")
* @return Response
*/
public function getResource(ImgProxyClient $imgProxyClient, Request $request) {
// we encode the url in base64 on the front to avoid problems when passing URLS to the backend
// so we have to decode it
$url = base64_decode($request->query->get("url"));
$resize = $request->query->get("resize", "fit");
$width = $request->query->get("width", "300");
$height = $request->query->get("height", "300");
$gravity = $request->query->get("gravity", "ce");
$enlarge = $request->query->get("enlarge", 1);
$path = $imgProxyClient->getResourceUrl($url, $resize, $width, $height, $gravity, $enlarge, "png");
return new Response(file_get_contents($path),200,["Content-Type"=>"application/octet-stream"]);
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ namespace Aboutgoods\KonnectBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Aboutgoods\KonnectBundle\Configuration as Config;
/**
* This is the class that validates and merges configuration from your app/config files.
......@@ -20,13 +21,20 @@ class Configuration implements ConfigurationInterface
$rootNode = $treeBuilder->root('konnect');
$rootNode
->children()
->arrayNode("authentication")
->arrayNode(Config::AUTHENTICATION)
->children()
->scalarNode('token')->defaultValue("changeMe")->end()
->scalarNode('secret')->defaultValue("changeMe")->end()
->scalarNode('url')->defaultValue(null)->end()
->end()
->end()
->arrayNode(Config::IMGPROXY)
->children()
->scalarNode('key')->defaultValue("changeMe")->end()
->scalarNode('salt')->defaultValue("changeMe")->end()
->scalarNode('url')->defaultValue(null)->end()
->end()
->end()
->end()
->end()
;
......
......@@ -23,6 +23,6 @@ class KonnectExtension extends Extension
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->getDefinition("konnect.client")->addMethodCall("setConfiguration",[$config]);
$container->getDefinition("configuration")->addMethodCall("setConfiguration",[$config]);
}
}
<?php
/**
* Created by PhpStorm.
* User: albancrepel
* Date: 2019-12-09
* Time: 15:06
*/
namespace Aboutgoods\KonnectBundle;
class ImgProxyClient
{
private $configuration;
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration->getConfiguration(Configuration::IMGPROXY);
}
public function getResourceUrl($url, $resize, $width, $height, $gravity, $enlarge, $extension){
$encodedUrl = rtrim(strtr(base64_encode($url), '+/', '-_'), '=');
$key = $this->configuration["key"];
$salt = $this->configuration["salt"];
$imgProxyUrl = $this->addTrailingSlashIfNotExists($this->configuration["url"]);
$keyBin = pack("H*" , $key);
if(empty($keyBin)) {
die('Key expected to be hex-encoded string');
}
$saltBin = pack("H*" , $salt);
if(empty($saltBin)) {
die('Salt expected to be hex-encoded string');
}
$path = "/rs:{$resize}:{$width}:{$height}/g:{$gravity}/el:{$enlarge}/{$encodedUrl}.{$extension}";
$signature = rtrim(strtr(base64_encode(hash_hmac('sha256', $saltBin.$path, $keyBin, true)), '+/', '-_'), '=');
return $imgProxyUrl . $signature . $path;
}
private function addTrailingSlashIfNotExists($url) {
return substr($url, -1) === "/" ?
$url :
"$url/";
}
}
\ No newline at end of file
......@@ -23,55 +23,21 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
*/
class KonnectClient
{
protected $configuration;
public function getConfiguration()
{
return $this->configuration;
}
private $configuration;
/**
* This method is called in the KonnectExtension.php
* @param $config
*/
public function setConfiguration($config)
public function __construct(Configuration $configuration)
{
$this->configuration = [
"authentication" => [
"token" => "token",
"secret" => "secret",
"url" => null
],
];
// Recursive replace with the config passed as parameter
foreach ($this->configuration as $key => $value) {
if(is_array($value)) {
foreach ($value as $subKey => $subValue) {
if (isset($config[$key][$subKey])) {
$this->configuration[$key][$subKey] = $config[$key][$subKey];
}
}
} else {
if (isset($config[$key])) {
$this->configuration[$key] = $config[$key];
}
}
}
$this->configuration = $configuration->getConfiguration(Configuration::AUTHENTICATION);
}
public function getGuzzleClient()
{
$config = $this->getConfiguration()['authentication'];
return new Client([
"base_uri" => $this->addTrailerSlashIfNotExists($config['url']),
"auth" => [$config['token'], $config['secret']]
]);
"base_uri" => $this->configuration['url'],
"auth" => [
$this->configuration['token'],
$this->configuration['secret']
]
]);
}
private function addTrailerSlashIfNotExists($url) {
return substr($url, -1) === "/" ?
$url :
"$url/";
}
}
\ No newline at end of file
......@@ -2,8 +2,22 @@ services:
konnect.client:
class: Aboutgoods\KonnectBundle\KonnectClient
public: true
arguments:
$configuration: '@Aboutgoods\KonnectBundle\Configuration'
Aboutgoods\KonnectBundle\KonnectClient:
alias: konnect.client
imgproxy.client:
class: Aboutgoods\KonnectBundle\ImgProxyClient
public: true
arguments:
$configuration: '@Aboutgoods\KonnectBundle\Configuration'
Aboutgoods\KonnectBundle\ImgProxyClient:
alias: imgproxy.client
configuration:
class: Aboutgoods\KonnectBundle\Configuration
public: true
Aboutgoods\KonnectBundle\Configuration:
alias: configuration
##
# CONTROLLER
##
......@@ -24,6 +38,12 @@ services:
public: true
Aboutgoods\KonnectBundle\Controller\ServiceController:
alias: konnect.service.controller
konnect.resource.controller:
class: Aboutgoods\KonnectBundle\Controller\ResourceController
public: true
Aboutgoods\KonnectBundle\Controller\ResourceController:
alias: konnect.resource.controller
##
# PROVIDER
##
......
......@@ -48,6 +48,10 @@ konnect:
token: '%env(KONNECT_TOKEN)%' #token of your services on konnect
secret: '%env(KONNECT_SECRET)%' #secret of your services on konnect
url: '%env(KONNECT_URI)%'
imgproxy:
key: '%env(IMGPROXY_KEY)%'
salt: '%env(IMGPROXY_SALT)%'
url: '%env(IMGPROXY_URI)%'
```
In your `.env`:
......@@ -55,10 +59,18 @@ In your `.env`:
KONNECT_TOKEN=YOUR_SERVICE_TOKEN
KONNECT_SECRET=YOUR_SERVICE_SECRET
KONNECT_URI=127.0.0.1:10080
IMGPROXY_KEY=41626F7574676F6F6473 #Aboutgoods
IMGPROXY_SALT=436F6D70616E79 #Company
IMGPROXY_URI=https://images.agkit.io/
```
For the basic services you can find their `TOKEN` and `SECRET` [on the Konnect readme](https://gitlab.agoods.fr/presentation/kweeri/Kweeri-Konnect/blob/dev/README.md#token-and-secret-for-each-services-).
And for how we implemented img-proxy, you can read [our documentation about image storage](https://gitlab.agoods.fr/documentation/dev-documentation/tree/master/Storage/image-storage).
You can also have a look to the [img-proxy documentation](https://docs.imgproxy.net/#/).
##### If you need to use error templates, you need to add this configuration :
In your `twig.yaml` :
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment