Skip to content

Commit

Permalink
readded missing function createVanity
Browse files Browse the repository at this point in the history
  • Loading branch information
SrS2225a committed Aug 14, 2023
1 parent c8c540f commit 32b8a16
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,43 @@ public function download(Request $request, Response $response, string $userCode,
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
Expand Down

0 comments on commit 32b8a16

Please sign in to comment.