Skip to content

Commit

Permalink
Add optional code param in upload request
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhnsn authored Aug 6, 2024
1 parent a918c95 commit 7da2cb9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/Controllers/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function uploadEndpoint(Request $request, Response $response): Response
}

try {
$response = $this->saveMedia($response, $file, $user);
$response = $this->saveMedia($response, $file, $user, param($request, 'code'));
} catch (Exception $e) {
$this->updateUserQuota($request, $user->id, $file->getSize(), true);
throw $e;
Expand Down Expand Up @@ -178,15 +178,24 @@ protected function validateUser(Request $request, Response $response, UploadedFi
* @param Response $response
* @param UploadedFileInterface $file
* @param $user
* @param $code
* @return Response
* @throws \League\Flysystem\FileExistsException
* @throws \League\Flysystem\FileNotFoundException
*/
protected function saveMedia(Response $response, UploadedFileInterface $file, $user)
protected function saveMedia(Response $response, UploadedFileInterface $file, $user, $code)
{
do {
$code = humanRandomString();
} while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
if ($code == null) {
do {
$code = humanRandomString();
} while ($this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count > 0);
} else {
$existingCodeCount = $this->database->query('SELECT COUNT(*) AS `count` FROM `uploads` WHERE `code` = ?', $code)->fetch()->count;
if ($existingCodeCount > 0) {
$this->json['message'] = 'Custom url code already exists.';
return json($response, $this->json, 409);
}
}

$fileInfo = pathinfo($file->getClientFilename());
$storagePath = "$user->user_code/$code.$fileInfo[extension]";
Expand Down

0 comments on commit 7da2cb9

Please sign in to comment.