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", "description": "A unique custom slug to be used instead of the generated short code",
"type": "string" "type": "string"
}, },
"pathPrefix": {
"description": "A prefix that will be prepended to provided custom slug or auto-generated short code",
"type": "string"
},
"findIfExists": { "findIfExists": {
"description": "Will force existing matching URL to be returned if found, instead of creating a new one", "description": "Will force existing matching URL to be returned if found, instead of creating a new one",
"type": "boolean" "type": "boolean"
@ -382,6 +386,7 @@
"validSince", "validSince",
"validUntil", "validUntil",
"customSlug", "customSlug",
"pathPrefix",
"maxVisits", "maxVisits",
"findIfExists", "findIfExists",
"domain" "domain"

View File

@ -33,6 +33,7 @@ use function Shlinkio\Shlink\Core\enumValues;
use function Shlinkio\Shlink\Core\generateRandomShortCode; use function Shlinkio\Shlink\Core\generateRandomShortCode;
use function Shlinkio\Shlink\Core\normalizeDate; use function Shlinkio\Shlink\Core\normalizeDate;
use function Shlinkio\Shlink\Core\normalizeOptionalDate; use function Shlinkio\Shlink\Core\normalizeOptionalDate;
use function sprintf;
class ShortUrl extends AbstractEntity class ShortUrl extends AbstractEntity
{ {
@ -100,9 +101,10 @@ class ShortUrl extends AbstractEntity
$instance->maxVisits = $creation->maxVisits; $instance->maxVisits = $creation->maxVisits;
$instance->customSlugWasProvided = $creation->hasCustomSlug(); $instance->customSlugWasProvided = $creation->hasCustomSlug();
$instance->shortCodeLength = $creation->shortCodeLength; $instance->shortCodeLength = $creation->shortCodeLength;
$instance->shortCode = $creation->customSlug ?? generateRandomShortCode( $instance->shortCode = sprintf(
$instance->shortCodeLength, '%s%s',
$creation->shortUrlMode, $creation->pathPrefix ?? '',
$creation->customSlug ?? generateRandomShortCode($instance->shortCodeLength, $creation->shortUrlMode),
); );
$instance->domain = $relationResolver->resolveDomain($creation->domain); $instance->domain = $relationResolver->resolveDomain($creation->domain);
$instance->authorApiKey = $creation->apiKey; $instance->authorApiKey = $creation->apiKey;

View File

@ -66,6 +66,7 @@ final readonly class ShortUrlCreation implements TitleResolutionModelInterface
validSince: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)), validSince: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_SINCE)),
validUntil: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)), validUntil: normalizeOptionalDate($inputFilter->getValue(ShortUrlInputFilter::VALID_UNTIL)),
customSlug: $inputFilter->getValue(ShortUrlInputFilter::CUSTOM_SLUG), customSlug: $inputFilter->getValue(ShortUrlInputFilter::CUSTOM_SLUG),
pathPrefix: $inputFilter->getValue(ShortUrlInputFilter::PATH_PREFIX),
maxVisits: getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS), maxVisits: getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS),
findIfExists: $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS) ?? false, findIfExists: $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS) ?? false,
domain: getNonEmptyOptionalValueFromInputFilter($inputFilter, ShortUrlInputFilter::DOMAIN), domain: getNonEmptyOptionalValueFromInputFilter($inputFilter, ShortUrlInputFilter::DOMAIN),
@ -90,6 +91,7 @@ final readonly class ShortUrlCreation implements TitleResolutionModelInterface
validSince: $this->validSince, validSince: $this->validSince,
validUntil: $this->validUntil, validUntil: $this->validUntil,
customSlug: $this->customSlug, customSlug: $this->customSlug,
pathPrefix: $this->pathPrefix,
maxVisits: $this->maxVisits, maxVisits: $this->maxVisits,
findIfExists: $this->findIfExists, findIfExists: $this->findIfExists,
domain: $this->domain, domain: $this->domain,