diff --git a/app/Controllers/UploadController.php b/app/Controllers/UploadController.php index f2ecee9..d25ce9c 100644 --- a/app/Controllers/UploadController.php +++ b/app/Controllers/UploadController.php @@ -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; @@ -118,6 +118,7 @@ public function uploadEndpoint(Request $request, Response $response): Response /** * @param Request $request * @param Response $response + * @param $code * @return UploadedFileInterface * @throws ValidationException */ @@ -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]";