diff --git a/app/Controllers/MediaController.php b/app/Controllers/MediaController.php index db2e12b..57b4825 100644 --- a/app/Controllers/MediaController.php +++ b/app/Controllers/MediaController.php @@ -188,6 +188,43 @@ class MediaController extends Controller return $this->streamMedia($request, $response, $this->storage, $media, 'attachment'); } + /** + * @param Request $request + * @param Response $response + * @param string $vanity + * @param string $id + * + * @return Response + * @throws HttpNotFoundException + * @throws HttpBadRequestException + */ + public function createVanity(Request $request, Response $response, int $id): Response + { + $media = $this->database->query('SELECT * FROM `uploads` WHERE `id` = ? LIMIT 1', $id)->fetch(); + + $vanity = param($request, 'vanity'); + $vanity = preg_replace('/[^a-z0-9]+/', '-', strtolower($vanity)); + + //handle collisions + $collision = $this->database->query('SELECT * FROM `uploads` WHERE `code` = ? AND `id` != ? LIMIT 1', [$vanity, $id])->fetch(); + + if (!$media) { + throw new HttpNotFoundException($request); + } + + if ($vanity === '' || $collision) { + throw new HttpBadRequestException($request); + } + + $this->database->query('UPDATE `uploads` SET `code` = ? WHERE `id` = ?', [$vanity, $media->id]); + $media->code = $vanity; + $response->getBody()->write(json_encode($media)); + + $this->logger->info('User '.$this->session->get('username').' created a vanity link for media '.$media->id); + + return $response; + } + /** * @param Request $request * @param Response $response diff --git a/app/routes.php b/app/routes.php old mode 100644 new mode 100755 index cb76175..9aca211 --- a/app/routes.php +++ b/app/routes.php @@ -62,6 +62,7 @@ $app->group('', function (RouteCollectorProxy $group) { $group->post('/upload/{id}/publish', [MediaController::class, 'togglePublish'])->setName('upload.publish'); $group->post('/upload/{id}/unpublish', [MediaController::class, 'togglePublish'])->setName('upload.unpublish'); + $group->post('/upload/{id}/vanity', [MediaController::class, 'createVanity'])->setName('upload.vanity'); $group->get('/upload/{id}/raw', [MediaController::class, 'getRawById'])->add(AdminMiddleware::class)->setName('upload.raw'); $group->map(['GET', 'POST'], '/upload/{id}/delete', [MediaController::class, 'delete'])->setName('upload.delete'); diff --git a/resources/lang/en.lang.php b/resources/lang/en.lang.php old mode 100644 new mode 100755 index 4b52a9e..95c3650 --- a/resources/lang/en.lang.php +++ b/resources/lang/en.lang.php @@ -30,6 +30,8 @@ return [ 'download' => 'Download', 'upload' => 'Upload', 'delete' => 'Delete', + 'confirm' => 'Confirm', + 'vanity_url' => 'Custom URL', 'publish' => 'Publish', 'hide' => 'Hide', 'files' => 'Files', @@ -58,7 +60,6 @@ return [ 'reg_date' => 'Registration Date', 'none' => 'None', 'open' => 'Open', - 'confirm' => 'Confirmation', 'confirm_string' => 'Are you sure?', 'installed' => 'Installation completed successfully!', 'bad_login' => 'Wrong credentials.', diff --git a/resources/templates/comp/modal_vanity.twig b/resources/templates/comp/modal_vanity.twig new file mode 100755 index 0000000..b0f5f01 --- /dev/null +++ b/resources/templates/comp/modal_vanity.twig @@ -0,0 +1,19 @@ + \ No newline at end of file diff --git a/resources/templates/dashboard/grid.twig b/resources/templates/dashboard/grid.twig old mode 100644 new mode 100755 index 718d15f..b4ae560 --- a/resources/templates/dashboard/grid.twig +++ b/resources/templates/dashboard/grid.twig @@ -28,6 +28,7 @@ {% else %} {% endif %} + @@ -68,4 +69,5 @@
{{ lang('no_media') }}
{% endif %} + {% include 'comp/modal_vanity.twig' %} {% endblock %} diff --git a/resources/templates/dashboard/list.twig b/resources/templates/dashboard/list.twig old mode 100644 new mode 100755 index 68cb08e..dc3f699 --- a/resources/templates/dashboard/list.twig +++ b/resources/templates/dashboard/list.twig @@ -6,6 +6,7 @@ {% include 'comp/navbar.twig' %}
{% include 'comp/alert.twig' %} + {% include 'comp/modal_vanity.twig' %}
@@ -77,6 +78,7 @@ {% else %} {% endif %} +
diff --git a/resources/templates/upload/public.twig b/resources/templates/upload/public.twig old mode 100644 new mode 100755 index a3c17b3..0cbd73c --- a/resources/templates/upload/public.twig +++ b/resources/templates/upload/public.twig @@ -49,6 +49,7 @@ {% if session.get('logged') %} + {% endif %}
@@ -162,4 +163,5 @@
{% include 'comp/modal_delete.twig' %} + {% include 'comp/modal_vanity.twig' %} {% endblock %} diff --git a/src/js/app.js b/src/js/app.js old mode 100644 new mode 100755 index a2a2eea..692e055 --- a/src/js/app.js +++ b/src/js/app.js @@ -29,8 +29,10 @@ var app = { $('.user-delete').click(app.modalDelete); $('.public-delete').click(app.modalDelete); + $('.public-vanity').click(app.modalVanity); $('.media-delete').click(app.mediaDelete); $('.publish-toggle').click(app.publishToggle); + $('.refresh-token').click(app.refreshToken); $('#themes').mousedown(app.loadThemes); $('.checkForUpdatesButton').click(app.checkForUpdates); @@ -57,6 +59,30 @@ var app = { $('#modalDelete-link').attr('href', $(this).data('link')); $('#modalDelete').modal('show'); }, + modalVanity: function () { + var id = $(this).data('id'); + $('#modalVanity').modal('show'); + $('#modalVanity-link').click(function () { + var $callerButton = $(this); + $.post(window.AppConfig.base_url + '/upload/' + id + '/vanity', {vanity: $('#modalVanity-input').val()}, function (responseData, status) { + $callerButton.tooltip('dispose'); + $('#modalVanity').modal('hide'); + $('#modalVanity-input').val(''); + var parsedData = JSON.parse(responseData); + if ($('#media_' + id).find('.btn-group').length > 0) { + $('#media_' + id).find('.btn-group').find('a').each(function (item) { + var oldUrl = $(this).attr('href'); + var newUrl = oldUrl.replace(oldUrl.substr(oldUrl.lastIndexOf('/') + 1), parsedData.code.code); + $(this).attr('href', newUrl); + }); + } else { + var oldUrl = window.location.href; + var newUrl = oldUrl.replace(oldUrl.substr(oldUrl.lastIndexOf('/') + 1), parsedData.code.code); + window.location.href = newUrl; + } + }); + }); + }, publishToggle: function () { var id = $(this).data('id'); var $callerButton = $(this);