Add optional code param in upload request
This commit is contained in:
parent
a918c95a56
commit
92ec35700e
1 changed files with 14 additions and 5 deletions
|
@ -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]";
|
||||
|
|
Loading…
Reference in a new issue