Skip to content

Commit

Permalink
Merge pull request #588 from nickhnsn/patch-1
Browse files Browse the repository at this point in the history
Add optional code param in upload request
  • Loading branch information
sergix44 authored Aug 29, 2024
2 parents a00fd5a + 92ec357 commit 3815c19
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 All @@ -118,6 +118,7 @@ public function uploadEndpoint(Request $request, Response $response): Response
/**
* @param Request $request
* @param Response $response
* @param $code
* @return UploadedFileInterface
* @throws ValidationException
*/
Expand Down Expand Up @@ -182,11 +183,19 @@ protected function validateUser(Request $request, Response $response, UploadedFi
* @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 3815c19

Please sign in to comment.