From f30c74b9877dfd2a1aad44a347773496f85631b5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 21 Feb 2024 18:06:06 +0100 Subject: [PATCH] Prepend path prefix to generated short code or custom slug --- docs/swagger/paths/v1_short-urls.json | 5 +++++ module/Core/src/ShortUrl/Entity/ShortUrl.php | 8 +++++--- module/Core/src/ShortUrl/Model/ShortUrlCreation.php | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/swagger/paths/v1_short-urls.json b/docs/swagger/paths/v1_short-urls.json index 08e08b67..c9bbe68a 100644 --- a/docs/swagger/paths/v1_short-urls.json +++ b/docs/swagger/paths/v1_short-urls.json @@ -303,6 +303,10 @@ "description": "A unique custom slug to be used instead of the generated short code", "type": "string" }, + "pathPrefix": { + "description": "A prefix that will be prepended to provided custom slug or auto-generated short code", + "type": "string" + }, "findIfExists": { "description": "Will force existing matching URL to be returned if found, instead of creating a new one", "type": "boolean" @@ -382,6 +386,7 @@ "validSince", "validUntil", "customSlug", + "pathPrefix", "maxVisits", "findIfExists", "domain" diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index ee2c5920..411e7bb1 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -33,6 +33,7 @@ use function Shlinkio\Shlink\Core\enumValues; use function Shlinkio\Shlink\Core\generateRandomShortCode; use function Shlinkio\Shlink\Core\normalizeDate; use function Shlinkio\Shlink\Core\normalizeOptionalDate; +use function sprintf; class ShortUrl extends AbstractEntity { @@ -100,9 +101,10 @@ class ShortUrl extends AbstractEntity $instance->maxVisits = $creation->maxVisits; $instance->customSlugWasProvided = $creation->hasCustomSlug(); $instance->shortCodeLength = $creation->shortCodeLength; - $instance->shortCode = $creation->customSlug ?? generateRandomShortCode( - $instance->shortCodeLength, - $creation->shortUrlMode, + $instance->shortCode = sprintf( + '%s%s', + $creation->pathPrefix ?? '', + $creation->customSlug ?? generateRandomShortCode($instance->shortCodeLength, $creation->shortUrlMode), ); $instance->domain = $relationResolver->resolveDomain($creation->domain); $instance->authorApiKey = $creation->apiKey; diff --git a/module/Core/src/ShortUrl/Model/ShortUrlCreation.php b/module/Core/src/ShortUrl/Model/ShortUrlCreation.php index 976973b2..4d22a8be 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlCreation.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlCreation.php @@ -66,6 +66,7 @@ final readonly class ShortUrlCreation implements TitleResolutionModelInterface validSince: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)), validUntil: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)), customSlug: $inputFilter->getValue(ShortUrlInputFilter::CUSTOM_SLUG), + pathPrefix: $inputFilter->getValue(ShortUrlInputFilter::PATH_PREFIX), maxVisits: getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS), findIfExists: $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS) ?? false, domain: getNonEmptyOptionalValueFromInputFilter($inputFilter, ShortUrlInputFilter::DOMAIN), @@ -90,6 +91,7 @@ final readonly class ShortUrlCreation implements TitleResolutionModelInterface validSince: $this->validSince, validUntil: $this->validUntil, customSlug: $this->customSlug, + pathPrefix: $this->pathPrefix, maxVisits: $this->maxVisits, findIfExists: $this->findIfExists, domain: $this->domain,