Add optional code param in upload request

This commit is contained in:
Nick Hansen 2024-08-09 21:44:27 +02:00 committed by GitHub
parent a918c95a56
commit 92ec35700e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -107,7 +107,7 @@ class UploadController extends Controller
}
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 @@ class UploadController extends Controller
/**
* @param Request $request
* @param Response $response
* @param $code
* @return UploadedFileInterface
* @throws ValidationException
*/
@ -182,11 +183,19 @@ class UploadController extends Controller
* @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]";