mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-20 01:09:56 +03:00
Updated create shortcode action to accept the custom slug
This commit is contained in:
parent
fd468cd4e9
commit
5f0d281255
3 changed files with 22 additions and 6 deletions
|
@ -93,7 +93,6 @@ class UrlShortener implements UrlShortenerInterface
|
||||||
$this->checkUrlExists($url);
|
$this->checkUrlExists($url);
|
||||||
$customSlug = $this->processCustomSlug($customSlug);
|
$customSlug = $this->processCustomSlug($customSlug);
|
||||||
|
|
||||||
|
|
||||||
// Transactionally insert the short url, then generate the short code and finally update the short code
|
// Transactionally insert the short url, then generate the short code and finally update the short code
|
||||||
try {
|
try {
|
||||||
$this->em->beginTransaction();
|
$this->em->beginTransaction();
|
||||||
|
|
|
@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
@ -57,12 +58,16 @@ class CreateShortcodeAction extends AbstractRestAction
|
||||||
], self::STATUS_BAD_REQUEST);
|
], self::STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
$longUrl = $postData['longUrl'];
|
$longUrl = $postData['longUrl'];
|
||||||
$tags = (array) ($postData['tags'] ?? []);
|
$customSlug = $postData['customSlug'] ?? null;
|
||||||
$validSince = $this->getOptionalDate($postData, 'validSince');
|
|
||||||
$validUntil = $this->getOptionalDate($postData, 'validUntil');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags, $validSince, $validUntil);
|
$shortCode = $this->urlShortener->urlToShortCode(
|
||||||
|
new Uri($longUrl),
|
||||||
|
(array) ($postData['tags'] ?? []),
|
||||||
|
$this->getOptionalDate($postData, 'validSince'),
|
||||||
|
$this->getOptionalDate($postData, 'validUntil'),
|
||||||
|
$customSlug
|
||||||
|
);
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$shortUrl = (new Uri())->withPath($shortCode)
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
->withHost($this->domainConfig['hostname']);
|
->withHost($this->domainConfig['hostname']);
|
||||||
|
@ -81,7 +86,16 @@ class CreateShortcodeAction extends AbstractRestAction
|
||||||
$longUrl
|
$longUrl
|
||||||
),
|
),
|
||||||
], self::STATUS_BAD_REQUEST);
|
], self::STATUS_BAD_REQUEST);
|
||||||
} catch (\Exception $e) {
|
} catch (NonUniqueSlugException $e) {
|
||||||
|
$this->logger->warning('Provided non-unique slug.' . PHP_EOL . $e);
|
||||||
|
return new JsonResponse([
|
||||||
|
'error' => RestUtils::getRestErrorCodeFromException($e),
|
||||||
|
'message' => sprintf(
|
||||||
|
$this->translator->translate('Provided slug %s is already in use. Try with a different one.'),
|
||||||
|
$customSlug
|
||||||
|
),
|
||||||
|
], self::STATUS_BAD_REQUEST);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
$this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e);
|
$this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e);
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'error' => RestUtils::UNKNOWN_ERROR,
|
'error' => RestUtils::UNKNOWN_ERROR,
|
||||||
|
|
|
@ -12,6 +12,7 @@ class RestUtils
|
||||||
const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
|
const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
|
||||||
const INVALID_URL_ERROR = 'INVALID_URL';
|
const INVALID_URL_ERROR = 'INVALID_URL';
|
||||||
const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';
|
const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';
|
||||||
|
const INVALID_SLUG_ERROR = 'INVALID_SLUG';
|
||||||
const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS';
|
const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS';
|
||||||
const INVALID_AUTH_TOKEN_ERROR = 'INVALID_AUTH_TOKEN';
|
const INVALID_AUTH_TOKEN_ERROR = 'INVALID_AUTH_TOKEN';
|
||||||
const INVALID_AUTHORIZATION_ERROR = 'INVALID_AUTHORIZATION';
|
const INVALID_AUTHORIZATION_ERROR = 'INVALID_AUTHORIZATION';
|
||||||
|
@ -26,6 +27,8 @@ class RestUtils
|
||||||
return self::INVALID_SHORTCODE_ERROR;
|
return self::INVALID_SHORTCODE_ERROR;
|
||||||
case $e instanceof Core\InvalidUrlException:
|
case $e instanceof Core\InvalidUrlException:
|
||||||
return self::INVALID_URL_ERROR;
|
return self::INVALID_URL_ERROR;
|
||||||
|
case $e instanceof Core\NonUniqueSlugException:
|
||||||
|
return self::INVALID_SLUG_ERROR;
|
||||||
case $e instanceof Common\InvalidArgumentException:
|
case $e instanceof Common\InvalidArgumentException:
|
||||||
return self::INVALID_ARGUMENT_ERROR;
|
return self::INVALID_ARGUMENT_ERROR;
|
||||||
case $e instanceof Rest\AuthenticationException:
|
case $e instanceof Rest\AuthenticationException:
|
||||||
|
|
Loading…
Add table
Reference in a new issue