Merge pull request #1191 from acelaya-forks/feature/default-qr-codes-config

Feature/default qr codes config
This commit is contained in:
Alejandro Celaya 2021-09-26 20:39:09 +02:00 committed by GitHub
commit 3bfa27e682
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 214 additions and 58 deletions

View file

@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
The config generated with the installing tool still has precedence over the env vars, so it cannot be combined. Either you use the tool, or use env vars.
* [#1149](https://github.com/shlinkio/shlink/issues/1149) Allowed to set custom defaults for the QR codes.
### Changed
* [#1142](https://github.com/shlinkio/shlink/issues/1142) Replaced `doctrine/cache` package with `symfony/cache`.
* [#1157](https://github.com/shlinkio/shlink/issues/1157) All routes now support CORS, not only rest ones.

View file

@ -50,7 +50,7 @@
"shlinkio/shlink-config": "^1.2",
"shlinkio/shlink-event-dispatcher": "^2.1",
"shlinkio/shlink-importer": "^2.3.1",
"shlinkio/shlink-installer": "^6.1",
"shlinkio/shlink-installer": "dev-develop#07f1ac8 as 6.2",
"shlinkio/shlink-ip-geolocation": "^2.0",
"symfony/console": "^5.3",
"symfony/filesystem": "^5.3",
@ -83,6 +83,7 @@
"Shlinkio\\Shlink\\Core\\": "module/Core/src"
},
"files": [
"config/constants.php",
"module/Core/functions/functions.php"
]
},

View file

@ -6,7 +6,7 @@ namespace Shlinkio\Shlink;
use function Shlinkio\Shlink\Common\env;
use const Shlinkio\Shlink\Core\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
use const Shlinkio\Shlink\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
return [

View file

@ -50,6 +50,10 @@ return [
Option\Tracking\DisableIpTrackingConfigOption::class,
Option\Tracking\DisableReferrerTrackingConfigOption::class,
Option\Tracking\DisableUaTrackingConfigOption::class,
Option\QrCode\DefaultSizeConfigOption::class,
Option\QrCode\DefaultMarginConfigOption::class,
Option\QrCode\DefaultFormatConfigOption::class,
Option\QrCode\DefaultErrorCorrectionConfigOption::class,
],
'installation_commands' => [

View file

@ -9,7 +9,7 @@ use Symfony\Component\Lock;
use function Shlinkio\Shlink\Common\env;
use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
use const Shlinkio\Shlink\LOCAL_LOCK_FACTORY;
return [

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
use function Shlinkio\Shlink\Common\env;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_SIZE;
return [
'qr_codes' => [
'size' => (int) env('DEFAULT_QR_CODE_SIZE', DEFAULT_QR_CODE_SIZE),
'margin' => (int) env('DEFAULT_QR_CODE_MARGIN', DEFAULT_QR_CODE_MARGIN),
'format' => env('DEFAULT_QR_CODE_FORMAT', DEFAULT_QR_CODE_FORMAT),
'error_correction' => env('DEFAULT_QR_CODE_ERROR_CORRECTION', DEFAULT_QR_CODE_ERROR_CORRECTION),
],
];

View file

@ -4,10 +4,10 @@ declare(strict_types=1);
use function Shlinkio\Shlink\Common\env;
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_CACHE_LIFETIME;
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_STATUS_CODE;
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\DEFAULT_REDIRECT_CACHE_LIFETIME;
use const Shlinkio\Shlink\DEFAULT_REDIRECT_STATUS_CODE;
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
return (static function (): array {
$webhooks = env('VISITS_WEBHOOKS');

20
config/constants.php Normal file
View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink;
use Fig\Http\Message\StatusCodeInterface;
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
const DEFAULT_SHORT_CODES_LENGTH = 5;
const MIN_SHORT_CODES_LENGTH = 4;
const DEFAULT_REDIRECT_STATUS_CODE = StatusCodeInterface::STATUS_FOUND;
const DEFAULT_REDIRECT_CACHE_LIFETIME = 30;
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
const CUSTOM_SLUGS_REGEXP = '/[^\pL\pN._~]/u'; // Any unicode letter or number, plus ".", "_" and "~" chars
const TITLE_TAG_VALUE = '/<title[^>]*>(.*?)<\/title>/i'; // Matches the value inside an html title tag
const DEFAULT_QR_CODE_SIZE = 300;
const DEFAULT_QR_CODE_MARGIN = 0;
const DEFAULT_QR_CODE_FORMAT = 'png';
const DEFAULT_QR_CODE_ERROR_CORRECTION = 'l';

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
use Laminas\ServiceManager\ServiceManager;
use Symfony\Component\Lock;
use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
use const Shlinkio\Shlink\LOCAL_LOCK_FACTORY;
chdir(dirname(__DIR__));

View file

@ -24,7 +24,7 @@ use Symfony\Component\Console as SymfonyCli;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Process\PhpExecutableFinder;
use const Shlinkio\Shlink\Core\LOCAL_LOCK_FACTORY;
use const Shlinkio\Shlink\LOCAL_LOCK_FACTORY;
return [

View file

@ -25,6 +25,7 @@ return [
Options\NotFoundRedirectOptions::class => ConfigAbstractFactory::class,
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
Options\TrackingOptions::class => ConfigAbstractFactory::class,
Options\QrCodeOptions::class => ConfigAbstractFactory::class,
Service\UrlShortener::class => ConfigAbstractFactory::class,
Service\ShortUrlService::class => ConfigAbstractFactory::class,
@ -86,6 +87,7 @@ return [
Options\NotFoundRedirectOptions::class => ['config.not_found_redirects'],
Options\UrlShortenerOptions::class => ['config.url_shortener'],
Options\TrackingOptions::class => ['config.tracking'],
Options\QrCodeOptions::class => ['config.qr_codes'],
Service\UrlShortener::class => [
ShortUrl\Helper\ShortUrlTitleResolutionHelper::class,
@ -138,6 +140,7 @@ return [
Service\ShortUrl\ShortUrlResolver::class,
ShortUrl\Helper\ShortUrlStringifier::class,
'Logger_Shlink',
Options\QrCodeOptions::class,
],
Action\RobotsAction::class => [Crawling\CrawlingHelper::class],

View file

@ -6,7 +6,6 @@ namespace Shlinkio\Shlink\Core;
use Cake\Chronos\Chronos;
use DateTimeInterface;
use Fig\Http\Message\StatusCodeInterface;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Laminas\InputFilter\InputFilter;
use PUGX\Shortid\Factory as ShortIdFactory;
@ -22,15 +21,6 @@ use function str_repeat;
use function str_replace;
use function ucwords;
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
const DEFAULT_SHORT_CODES_LENGTH = 5;
const MIN_SHORT_CODES_LENGTH = 4;
const DEFAULT_REDIRECT_STATUS_CODE = StatusCodeInterface::STATUS_FOUND;
const DEFAULT_REDIRECT_CACHE_LIFETIME = 30;
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
const CUSTOM_SLUGS_REGEXP = '/[^\pL\pN._~]/u'; // Any unicode letter or number, plus ".", "_" and "~" chars
const TITLE_TAG_VALUE = '/<title[^>]*>(.*?)<\/title>/i'; // Matches the value inside an html title tag
function generateRandomShortCode(int $length): string
{
static $shortIdFactory;

View file

@ -14,15 +14,17 @@ use Endroid\QrCode\Writer\SvgWriter;
use Endroid\QrCode\Writer\WriterInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
use function Functional\contains;
use function strtolower;
use function trim;
final class QrCodeParams
{
private const DEFAULT_SIZE = 300;
private const MIN_SIZE = 50;
private const MAX_SIZE = 1000;
private const SUPPORTED_FORMATS = ['png', 'svg'];
private function __construct(
private int $size,
@ -32,22 +34,22 @@ final class QrCodeParams
) {
}
public static function fromRequest(ServerRequestInterface $request): self
public static function fromRequest(ServerRequestInterface $request, QrCodeOptions $defaults): self
{
$query = $request->getQueryParams();
return new self(
self::resolveSize($request, $query),
self::resolveMargin($query),
self::resolveWriter($query),
self::resolveErrorCorrection($query),
self::resolveSize($request, $query, $defaults),
self::resolveMargin($query, $defaults),
self::resolveWriter($query, $defaults),
self::resolveErrorCorrection($query, $defaults),
);
}
private static function resolveSize(Request $request, array $query): int
private static function resolveSize(Request $request, array $query, QrCodeOptions $defaults): int
{
// FIXME Size attribute is deprecated. After v3.0.0, always use the query param instead
$size = (int) $request->getAttribute('size', $query['size'] ?? self::DEFAULT_SIZE);
$size = (int) $request->getAttribute('size', $query['size'] ?? $defaults->size());
if ($size < self::MIN_SIZE) {
return self::MIN_SIZE;
}
@ -55,13 +57,9 @@ final class QrCodeParams
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size;
}
private static function resolveMargin(array $query): int
private static function resolveMargin(array $query, QrCodeOptions $defaults): int
{
$margin = $query['margin'] ?? null;
if ($margin === null) {
return 0;
}
$margin = $query['margin'] ?? (string) $defaults->margin();
$intMargin = (int) $margin;
if ($margin !== (string) $intMargin) {
return 0;
@ -70,18 +68,20 @@ final class QrCodeParams
return $intMargin < 0 ? 0 : $intMargin;
}
private static function resolveWriter(array $query): WriterInterface
private static function resolveWriter(array $query, QrCodeOptions $defaults): WriterInterface
{
$format = strtolower(trim($query['format'] ?? 'png'));
$qFormat = self::normalizeParam($query['format'] ?? '');
$format = contains(self::SUPPORTED_FORMATS, $qFormat) ? $qFormat : self::normalizeParam($defaults->format());
return match ($format) {
'svg' => new SvgWriter(),
default => new PngWriter(),
};
}
private static function resolveErrorCorrection(array $query): ErrorCorrectionLevelInterface
private static function resolveErrorCorrection(array $query, QrCodeOptions $defaults): ErrorCorrectionLevelInterface
{
$errorCorrectionLevel = strtolower(trim($query['errorCorrection'] ?? 'l'));
$errorCorrectionLevel = self::normalizeParam($query['errorCorrection'] ?? $defaults->errorCorrection());
return match ($errorCorrectionLevel) {
'h' => new ErrorCorrectionLevelHigh(),
'q' => new ErrorCorrectionLevelQuartile(),
@ -90,6 +90,11 @@ final class QrCodeParams
};
}
private static function normalizeParam(string $param): string
{
return strtolower(trim($param));
}
public function size(): int
{
return $this->size;

View file

@ -14,6 +14,7 @@ use Shlinkio\Shlink\Common\Response\QrCodeResponse;
use Shlinkio\Shlink\Core\Action\Model\QrCodeParams;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
@ -23,6 +24,7 @@ class QrCodeAction implements MiddlewareInterface
private ShortUrlResolverInterface $urlResolver,
private ShortUrlStringifierInterface $stringifier,
private LoggerInterface $logger,
private QrCodeOptions $defaultOptions,
) {
}
@ -37,7 +39,7 @@ class QrCodeAction implements MiddlewareInterface
return $handler->handle($request);
}
$params = QrCodeParams::fromRequest($request);
$params = QrCodeParams::fromRequest($request, $this->defaultOptions);
$qrCodeBuilder = Builder::create()
->data($this->stringifier->stringify($shortUrl))
->size($params->size())

View file

@ -14,7 +14,7 @@ use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter;
use function Shlinkio\Shlink\Core\getOptionalIntFromInputFilter;
use function Shlinkio\Shlink\Core\parseDateField;
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
final class ShortUrlMeta implements TitleResolutionModelInterface
{

View file

@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Options;
use Laminas\Stdlib\AbstractOptions;
use const Shlinkio\Shlink\Core\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
use const Shlinkio\Shlink\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
class DeleteShortUrlsOptions extends AbstractOptions
{

View file

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Options;
use Laminas\Stdlib\AbstractOptions;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
use const Shlinkio\Shlink\DEFAULT_QR_CODE_SIZE;
class QrCodeOptions extends AbstractOptions
{
private int $size = DEFAULT_QR_CODE_SIZE;
private int $margin = DEFAULT_QR_CODE_MARGIN;
private string $format = DEFAULT_QR_CODE_FORMAT;
private string $errorCorrection = DEFAULT_QR_CODE_ERROR_CORRECTION;
public function size(): int
{
return $this->size;
}
protected function setSize(int $size): void
{
$this->size = $size;
}
public function margin(): int
{
return $this->margin;
}
protected function setMargin(int $margin): void
{
$this->margin = $margin;
}
public function format(): string
{
return $this->format;
}
protected function setFormat(string $format): void
{
$this->format = $format;
}
public function errorCorrection(): string
{
return $this->errorCorrection;
}
protected function setErrorCorrection(string $errorCorrection): void
{
$this->errorCorrection = $errorCorrection;
}
}

View file

@ -8,8 +8,8 @@ use Laminas\Stdlib\AbstractOptions;
use function Functional\contains;
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_CACHE_LIFETIME;
use const Shlinkio\Shlink\Core\DEFAULT_REDIRECT_STATUS_CODE;
use const Shlinkio\Shlink\DEFAULT_REDIRECT_CACHE_LIFETIME;
use const Shlinkio\Shlink\DEFAULT_REDIRECT_STATUS_CODE;
class UrlShortenerOptions extends AbstractOptions
{

View file

@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use function preg_match;
use function trim;
use const Shlinkio\Shlink\Core\TITLE_TAG_VALUE;
use const Shlinkio\Shlink\TITLE_TAG_VALUE;
class UrlValidator implements UrlValidatorInterface, RequestMethodInterface
{

View file

@ -13,8 +13,8 @@ use Shlinkio\Shlink\Common\Validation;
use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use const Shlinkio\Shlink\Core\CUSTOM_SLUGS_REGEXP;
use const Shlinkio\Shlink\Core\MIN_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\CUSTOM_SLUGS_REGEXP;
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
class ShortUrlInputFilter extends InputFilter
{

View file

@ -20,6 +20,7 @@ use Shlinkio\Shlink\Core\Action\QrCodeAction;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Options\QrCodeOptions;
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
@ -31,6 +32,7 @@ class QrCodeActionTest extends TestCase
private QrCodeAction $action;
private ObjectProphecy $urlResolver;
private QrCodeOptions $options;
public function setUp(): void
{
@ -38,11 +40,13 @@ class QrCodeActionTest extends TestCase
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->options = new QrCodeOptions();
$this->action = new QrCodeAction(
$this->urlResolver->reveal(),
new ShortUrlStringifier(['domain' => 'doma.in']),
new NullLogger(),
$this->options,
);
}
@ -85,9 +89,11 @@ class QrCodeActionTest extends TestCase
* @dataProvider provideQueries
*/
public function imageIsReturnedWithExpectedContentTypeBasedOnProvidedFormat(
string $defaultFormat,
array $query,
string $expectedContentType,
): void {
$this->options->setFromArray(['format' => $defaultFormat]);
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(
ShortUrl::createEmpty(),
@ -102,18 +108,26 @@ class QrCodeActionTest extends TestCase
public function provideQueries(): iterable
{
yield 'no format' => [[], 'image/png'];
yield 'png format' => [['format' => 'png'], 'image/png'];
yield 'svg format' => [['format' => 'svg'], 'image/svg+xml'];
yield 'unsupported format' => [['format' => 'jpg'], 'image/png'];
yield 'no format, png default' => ['png', [], 'image/png'];
yield 'no format, svg default' => ['svg', [], 'image/svg+xml'];
yield 'png format, png default' => ['png', ['format' => 'png'], 'image/png'];
yield 'png format, svg default' => ['svg', ['format' => 'png'], 'image/png'];
yield 'svg format, png default' => ['png', ['format' => 'svg'], 'image/svg+xml'];
yield 'svg format, svg default' => ['svg', ['format' => 'svg'], 'image/svg+xml'];
yield 'unsupported format, png default' => ['png', ['format' => 'jpg'], 'image/png'];
yield 'unsupported format, svg default' => ['svg', ['format' => 'jpg'], 'image/svg+xml'];
}
/**
* @test
* @dataProvider provideRequestsWithSize
*/
public function imageIsReturnedWithExpectedSize(ServerRequestInterface $req, int $expectedSize): void
{
public function imageIsReturnedWithExpectedSize(
array $defaults,
ServerRequestInterface $req,
int $expectedSize,
): void {
$this->options->setFromArray($defaults);
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($code, ''))->willReturn(
ShortUrl::createEmpty(),
@ -128,25 +142,59 @@ class QrCodeActionTest extends TestCase
public function provideRequestsWithSize(): iterable
{
yield 'no size' => [ServerRequestFactory::fromGlobals(), 300];
yield 'size in attr' => [ServerRequestFactory::fromGlobals()->withAttribute('size', '400'), 400];
yield 'size in query' => [ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']), 123];
yield 'different margin and size defaults' => [
['size' => 660, 'margin' => 40],
ServerRequestFactory::fromGlobals(),
740,
];
yield 'no size' => [[], ServerRequestFactory::fromGlobals(), 300];
yield 'no size, different default' => [['size' => 500], ServerRequestFactory::fromGlobals(), 500];
yield 'size in attr' => [[], ServerRequestFactory::fromGlobals()->withAttribute('size', '400'), 400];
yield 'size in query' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']), 123];
yield 'size in query, default margin' => [
['margin' => 25],
ServerRequestFactory::fromGlobals()->withQueryParams(['size' => '123']),
173,
];
yield 'size in query and attr' => [
[],
ServerRequestFactory::fromGlobals()->withAttribute('size', '350')->withQueryParams(['size' => '123']),
350,
];
yield 'margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
yield 'margin' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']), 370];
yield 'margin and different default' => [
['size' => 400],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '35']),
470,
];
yield 'margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '100', 'size' => '200']),
400,
];
yield 'negative margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']), 300];
yield 'non-numeric margin' => [ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']), 300];
yield 'negative margin' => [[], ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']), 300];
yield 'negative margin, default margin' => [
['margin' => 10],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-50']),
300,
];
yield 'non-numeric margin' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo']),
300,
];
yield 'negative margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
150,
];
yield 'negative margin and size, default margin' => [
['margin' => 5],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => '-1', 'size' => '150']),
150,
];
yield 'non-numeric margin and size' => [
[],
ServerRequestFactory::fromGlobals()->withQueryParams(['margin' => 'foo', 'size' => '538']),
538,
];

View file

@ -16,7 +16,7 @@ use function Functional\map;
use function range;
use function strlen;
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
class ShortUrlTest extends TestCase
{