Prepend path prefix to generated short code or custom slug

This commit is contained in:
Alejandro Celaya 2024-02-21 18:06:06 +01:00
parent 467dbdd183
commit f30c74b987
3 changed files with 12 additions and 3 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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,