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