From bfbbf89d7967526e21e63a014610e035b85505e7 Mon Sep 17 00:00:00 2001 From: SergiX44 Date: Fri, 13 Aug 2021 09:17:24 +0200 Subject: [PATCH] refactoring --- app/Controllers/Auth/LoginController.php | 18 +-- app/Controllers/Auth/RegisterController.php | 4 +- app/Controllers/ClientController.php | 10 +- app/Controllers/Controller.php | 4 +- app/Controllers/DashboardController.php | 16 +-- app/Controllers/ExportController.php | 4 +- app/Controllers/MediaController.php | 105 +++++++++--------- app/Controllers/ProfileController.php | 6 +- app/Controllers/SettingController.php | 8 +- app/Controllers/TagController.php | 6 +- app/Controllers/UploadController.php | 10 +- app/Controllers/UserController.php | 16 +-- app/Database/Migrator.php | 16 +-- .../MediaRepository.php} | 66 ++++++----- .../TagRepository.php} | 6 +- .../UserRepository.php} | 33 +++--- 16 files changed, 171 insertions(+), 157 deletions(-) rename app/Database/{Queries/MediaQuery.php => Repositories/MediaRepository.php} (83%) rename app/Database/{Queries/TagQuery.php => Repositories/TagRepository.php} (96%) rename app/Database/{Queries/UserQuery.php => Repositories/UserRepository.php} (88%) diff --git a/app/Controllers/Auth/LoginController.php b/app/Controllers/Auth/LoginController.php index 76da600..171eb9d 100644 --- a/app/Controllers/Auth/LoginController.php +++ b/app/Controllers/Auth/LoginController.php @@ -2,7 +2,7 @@ namespace App\Controllers\Auth; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\ValidationHelper; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -128,13 +128,13 @@ class LoginController extends AuthController $this->session->alert(lang('ldap_cant_connect'), 'warning'); return $dbUser; } - + //Get LDAP user's (R)DN $userDN=$this->getLdapRdn($username, $server); if (!is_string($userDN)) { return null; } - + //Bind as user to validate password if (@ldap_bind($server, $userDN, $password)) { $this->logger->debug("$userDN authenticated against LDAP sucessfully"); @@ -145,7 +145,7 @@ class LoginController extends AuthController } return null; } - + if (!$dbUser) { $email = $username; if (!filter_var($username, FILTER_VALIDATE_EMAIL)) { @@ -162,18 +162,18 @@ class LoginController extends AuthController $entry = ldap_first_entry($server, $search); $email = @ldap_get_values($server, $entry, 'mail')[0] ?? platform_mail($username.rand(0, 100)); // if the mail is not set, generate a placeholder } - /** @var UserQuery $userQuery */ - $userQuery = make(UserQuery::class); + /** @var UserRepository $userQuery */ + $userQuery = make(UserRepository::class); $userQuery->create($email, $username, $password, 0, 1, (int) $this->getSetting('default_user_quota', -1), null, 1); return $userQuery->get($request, $this->database->getPdo()->lastInsertId()); } - + if ($server) { ldap_close($server); } - + if (!password_verify($password, $dbUser->password)) { - $userQuery = make(UserQuery::class); + $userQuery = make(UserRepository::class); $userQuery->update($dbUser->id, $dbUser->email, $username, $password, $dbUser->is_admin, $dbUser->active, $dbUser->max_disk_quota, $dbUser->ldap); return $userQuery->get($request, $dbUser->id); } diff --git a/app/Controllers/Auth/RegisterController.php b/app/Controllers/Auth/RegisterController.php index ab5f879..aa2f476 100644 --- a/app/Controllers/Auth/RegisterController.php +++ b/app/Controllers/Auth/RegisterController.php @@ -4,7 +4,7 @@ namespace App\Controllers\Auth; use App\Controllers\Controller; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\Mail; use App\Web\ValidationHelper; use Psr\Http\Message\ResponseInterface as Response; @@ -67,7 +67,7 @@ class RegisterController extends AuthController $activateToken = bin2hex(random_bytes(16)); - make(UserQuery::class)->create( + make(UserRepository::class)->create( param($request, 'email'), param($request, 'username'), param($request, 'password'), diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index bff1627..d373cec 100644 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Exception\HttpNotFoundException; @@ -20,9 +20,9 @@ class ClientController extends Controller */ public function getShareXConfig(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id, true); + $user = make(UserRepository::class)->get($request, $id, true); - if ($user->token === null || $user->token === '') { + if (!$user->token) { $this->session->alert(lang('no_upload_token'), 'danger'); return redirect($response, $request->getHeaderLine('Referer')); @@ -96,9 +96,9 @@ class ClientController extends Controller */ public function getBashScript(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id, true); + $user = make(UserRepository::class)->get($request, $id, true); - if ($user->token === null || $user->token === '') { + if (!$user->token) { $this->session->alert(lang('no_upload_token'), 'danger'); return redirect($response, $request->getHeaderLine('Referer')); diff --git a/app/Controllers/Controller.php b/app/Controllers/Controller.php index cd2e939..0d6e5c1 100644 --- a/app/Controllers/Controller.php +++ b/app/Controllers/Controller.php @@ -3,7 +3,7 @@ namespace App\Controllers; use App\Database\DB; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\Lang; use App\Web\Session; use App\Web\ValidationHelper; @@ -89,7 +89,7 @@ abstract class Controller */ protected function updateUserQuota(Request $request, $userId, $fileSize, $dec = false) { - $user = make(UserQuery::class)->get($request, $userId); + $user = make(UserRepository::class)->get($request, $userId); if ($dec) { $tot = max($user->current_disk_quota - $fileSize, 0); diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index 3436bad..1a1a98a 100644 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -2,8 +2,8 @@ namespace App\Controllers; -use App\Database\Queries\MediaQuery; -use App\Database\Queries\TagQuery; +use App\Database\Repositories\MediaRepository; +use App\Database\Repositories\TagRepository; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -41,28 +41,28 @@ class DashboardController extends Controller switch (param($request, 'sort', 'time')) { case 'size': - $order = MediaQuery::ORDER_SIZE; + $order = MediaRepository::ORDER_SIZE; break; case 'name': - $order = MediaQuery::ORDER_NAME; + $order = MediaRepository::ORDER_NAME; break; default: case 'time': - $order = MediaQuery::ORDER_TIME; + $order = MediaRepository::ORDER_TIME; break; } $isAdmin = (bool) $this->session->get('admin', false); - /** @var MediaQuery $query */ - $query = make(MediaQuery::class, ['isAdmin' => $isAdmin]) + /** @var MediaRepository $query */ + $query = make(MediaRepository::class, ['isAdmin' => $isAdmin]) ->orderBy($order, param($request, 'order', 'DESC')) ->withUserId($this->session->get('user_id')) ->search(param($request, 'search', null)) ->filterByTag(param($request, 'tag')) ->run($page); - $tags = make(TagQuery::class, [ + $tags = make(TagRepository::class, [ 'isAdmin' => $isAdmin, 'userId' => $this->session->get('user_id') ])->all(); diff --git a/app/Controllers/ExportController.php b/app/Controllers/ExportController.php index 84093db..c5310c0 100644 --- a/app/Controllers/ExportController.php +++ b/app/Controllers/ExportController.php @@ -3,7 +3,7 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use League\Flysystem\FileNotFoundException; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -21,7 +21,7 @@ class ExportController extends Controller */ public function downloadData(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id, true); + $user = make(UserRepository::class)->get($request, $id, true); $medias = $this->database->query('SELECT `uploads`.`filename`, `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $user->id); diff --git a/app/Controllers/MediaController.php b/app/Controllers/MediaController.php index 86be1a6..bdfffcf 100644 --- a/app/Controllers/MediaController.php +++ b/app/Controllers/MediaController.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\UA; use GuzzleHttp\Psr7\Stream; use Intervention\Image\Constraint; @@ -32,11 +32,17 @@ class MediaController extends Controller * @throws FileNotFoundException * */ - public function show(Request $request, Response $response, string $userCode, string $mediaCode, string $token = null): Response - { + public function show( + Request $request, + Response $response, + string $userCode, + string $mediaCode, + string $token = null + ): Response { $media = $this->getMedia($userCode, $mediaCode, true); - if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', false))) { + if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get('admin', + false))) { throw new HttpNotFoundException($request); } @@ -125,9 +131,9 @@ class MediaController extends Controller $media = $this->getMedia($userCode, $mediaCode, false); if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get( - 'admin', - false - ))) { + 'admin', + false + ))) { throw new HttpNotFoundException($request); } @@ -159,9 +165,9 @@ class MediaController extends Controller $media = $this->getMedia($userCode, $mediaCode, false); if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get( - 'admin', - false - ))) { + 'admin', + false + ))) { throw new HttpNotFoundException($request); } @@ -217,19 +223,18 @@ class MediaController extends Controller throw new HttpNotFoundException($request); } - if ($this->session->get('admin', false) || $media->user_id === $this->session->get('user_id')) { - $this->deleteMedia($request, $media->storage_path, $id, $media->user_id); - - $this->logger->info('User '.$this->session->get('username').' deleted a media.', [$id]); - - if ($media->user_id === $this->session->get('user_id')) { - $user = make(UserQuery::class)->get($request, $media->user_id, true); - $this->setSessionQuotaInfo($user->current_disk_quota, $user->max_disk_quota); - } - } else { + if (!$this->session->get('admin', false) && $media->user_id !== $this->session->get('user_id')) { throw new HttpUnauthorizedException($request); } + $this->deleteMedia($request, $media->storage_path, $id, $media->user_id); + $this->logger->info('User '.$this->session->get('username').' deleted a media.', [$id]); + + if ($media->user_id === $this->session->get('user_id')) { + $user = make(UserRepository::class)->get($request, $media->user_id, true); + $this->setSessionQuotaInfo($user->current_disk_quota, $user->max_disk_quota); + } + if ($request->getMethod() === 'GET') { return redirect($response, route('home')); } @@ -365,10 +370,8 @@ class MediaController extends Controller $this->session->close(); $mime = $storage->getMimetype($media->storage_path); - if ((param($request, 'width') !== null || param($request, 'height') !== null) && explode( - '/', - $mime - )[0] === 'image') { + if ((param($request, 'width') !== null || param($request, 'height') !== null) && explode('/', + $mime)[0] === 'image') { return $this->makeThumbnail( $storage, $media, @@ -376,32 +379,32 @@ class MediaController extends Controller param($request, 'height'), $disposition ); - } else { - $stream = new Stream($storage->readStream($media->storage_path)); + } - if (!in_array(explode('/', $mime)[0], ['image', 'video', 'audio']) || $disposition === 'attachment') { - return $response->withHeader('Content-Type', $mime) - ->withHeader('Content-Disposition', $disposition.'; filename="'.$media->filename.'"') - ->withHeader('Content-Length', $stream->getSize()) - ->withBody($stream); - } - - if (isset($request->getServerParams()['HTTP_RANGE'])) { - return $this->handlePartialRequest( - $response, - $stream, - $request->getServerParams()['HTTP_RANGE'], - $disposition, - $media, - $mime - ); - } + $stream = new Stream($storage->readStream($media->storage_path)); + if (!in_array(explode('/', $mime)[0], ['image', 'video', 'audio']) || $disposition === 'attachment') { return $response->withHeader('Content-Type', $mime) + ->withHeader('Content-Disposition', $disposition.'; filename="'.$media->filename.'"') ->withHeader('Content-Length', $stream->getSize()) - ->withHeader('Accept-Ranges', 'bytes') ->withBody($stream); } + + if (isset($request->getServerParams()['HTTP_RANGE'])) { + return $this->handlePartialRequest( + $response, + $stream, + $request->getServerParams()['HTTP_RANGE'], + $disposition, + $media, + $mime + ); + } + + return $response->withHeader('Content-Type', $mime) + ->withHeader('Content-Length', $stream->getSize()) + ->withHeader('Accept-Ranges', 'bytes') + ->withBody($stream); } /** @@ -473,7 +476,9 @@ class MediaController extends Controller $end = (isset($range[1]) && is_numeric($range[1])) ? (int) $range[1] : $stream->getSize(); } - $end = ($end > $stream->getSize() - 1) ? $stream->getSize() - 1 : $end; + if ($end > $stream->getSize() - 1) { + $end = $stream->getSize() - 1; + } $stream->seek($start); header("Content-Type: $mime"); @@ -484,15 +489,7 @@ class MediaController extends Controller http_response_code(206); ob_end_clean(); - $buffer = 16348; - $readed = $start; - while ($readed < $end) { - if ($readed + $buffer > $end) { - $buffer = $end - $readed + 1; - } - echo $stream->read($buffer); - $readed += $buffer; - } + fpassthru($stream->detach()); exit(0); } diff --git a/app/Controllers/ProfileController.php b/app/Controllers/ProfileController.php index 0bbb323..73e2fdb 100644 --- a/app/Controllers/ProfileController.php +++ b/app/Controllers/ProfileController.php @@ -3,7 +3,7 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\ValidationHelper; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -21,7 +21,7 @@ class ProfileController extends Controller */ public function profile(Request $request, Response $response): Response { - $user = make(UserQuery::class)->get($request, $this->session->get('user_id'), true); + $user = make(UserRepository::class)->get($request, $this->session->get('user_id'), true); return view()->render($response, 'user/edit.twig', [ 'profile' => true, @@ -38,7 +38,7 @@ class ProfileController extends Controller */ public function profileEdit(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id, true); + $user = make(UserRepository::class)->get($request, $id, true); /** @var ValidationHelper $validator */ $validator = make(ValidationHelper::class) diff --git a/app/Controllers/SettingController.php b/app/Controllers/SettingController.php index 56f32d0..4cc9103 100644 --- a/app/Controllers/SettingController.php +++ b/app/Controllers/SettingController.php @@ -3,12 +3,14 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; class SettingController extends Controller { + public const DEFAULT_THEME_URL = 'https://bootswatch.com/4/_vendor/bootstrap/dist/css/bootstrap.min.css'; + /** * @param Request $request * @param Response $response @@ -34,7 +36,7 @@ class SettingController extends Controller // quota $this->updateSetting('quota_enabled', param($request, 'quota_enabled', 'off')); $this->updateSetting('default_user_quota', stringToBytes(param($request, 'default_user_quota', '1G'))); - $user = make(UserQuery::class)->get($request, $this->session->get('user_id')); + $user = make(UserRepository::class)->get($request, $this->session->get('user_id')); $this->setSessionQuotaInfo($user->current_disk_quota, $user->max_disk_quota); $this->updateSetting('custom_head', param($request, 'custom_head')); @@ -78,7 +80,7 @@ class SettingController extends Controller } // if is default, remove setting - if (param($request, 'css') !== 'https://bootswatch.com/4/_vendor/bootstrap/dist/css/bootstrap.min.css') { + if (param($request, 'css') !== self::DEFAULT_THEME_URL) { $this->updateSetting('css', param($request, 'css')); } else { $this->database->query('DELETE FROM `settings` WHERE `key` = \'css\''); diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php index 51e620d..3e55ed5 100644 --- a/app/Controllers/TagController.php +++ b/app/Controllers/TagController.php @@ -3,7 +3,7 @@ namespace App\Controllers; -use App\Database\Queries\TagQuery; +use App\Database\Repositories\TagRepository; use App\Web\ValidationHelper; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -27,7 +27,7 @@ class TagController extends Controller throw new HttpBadRequestException($request); } - [$id, $limit] = make(TagQuery::class)->addTag(param($request, 'tag'), param($request, 'mediaId')); + [$id, $limit] = make(TagRepository::class)->addTag(param($request, 'tag'), param($request, 'mediaId')); $this->logger->info("Tag added $id."); @@ -53,7 +53,7 @@ class TagController extends Controller throw new HttpBadRequestException($request); } - $result = make(TagQuery::class)->removeTag(param($request, 'tagId'), param($request, 'mediaId')); + $result = make(TagRepository::class)->removeTag(param($request, 'tagId'), param($request, 'mediaId')); if ($result === null) { throw new HttpNotFoundException($request); diff --git a/app/Controllers/UploadController.php b/app/Controllers/UploadController.php index b0cf325..e6a62f0 100644 --- a/app/Controllers/UploadController.php +++ b/app/Controllers/UploadController.php @@ -2,8 +2,8 @@ namespace App\Controllers; -use App\Database\Queries\TagQuery; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\TagRepository; +use App\Database\Repositories\UserRepository; use App\Exceptions\ValidationException; use Exception; use Psr\Http\Message\ResponseInterface as Response; @@ -45,7 +45,7 @@ class UploadController extends Controller try { $file = $this->validateFile($request, $response); - $user = make(UserQuery::class)->get($request, $this->session->get('user_id')); + $user = make(UserRepository::class)->get($request, $this->session->get('user_id')); $this->validateUser($request, $response, $file, $user); } catch (ValidationException $e) { @@ -223,8 +223,8 @@ class UploadController extends Controller [$type, $subtype] = explode('/', $mime); - /** @var TagQuery $query */ - $query = make(TagQuery::class); + /** @var TagRepository $query */ + $query = make(TagRepository::class); $query->addTag($type, $mediaId); if ($type === 'application' || $subtype === 'gif') { diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index dabab34..a497787 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -2,7 +2,7 @@ namespace App\Controllers; -use App\Database\Queries\UserQuery; +use App\Database\Repositories\UserRepository; use App\Web\Mail; use App\Web\ValidationHelper; use League\Flysystem\FileNotFoundException; @@ -90,7 +90,7 @@ class UserController extends Controller return redirect($response, route('user.create')); } - make(UserQuery::class)->create( + make(UserRepository::class)->create( param($request, 'email'), param($request, 'username'), param($request, 'password'), @@ -133,7 +133,7 @@ class UserController extends Controller */ public function edit(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id); + $user = make(UserRepository::class)->get($request, $id); return view()->render($response, 'user/edit.twig', [ 'profile' => false, @@ -152,7 +152,7 @@ class UserController extends Controller */ public function update(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id); + $user = make(UserRepository::class)->get($request, $id); $user->max_disk_quota = -1; /** @var ValidationHelper $validator */ @@ -180,7 +180,7 @@ class UserController extends Controller return redirect($response, route('user.edit', ['id' => $id])); } - make(UserQuery::class)->update( + make(UserRepository::class)->update( $user->id, param($request, 'email'), param($request, 'username'), @@ -215,7 +215,7 @@ class UserController extends Controller */ public function delete(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id); + $user = make(UserRepository::class)->get($request, $id); if ($user->id === $this->session->get('user_id')) { $this->session->alert(lang('cannot_delete'), 'danger'); @@ -239,7 +239,7 @@ class UserController extends Controller */ public function clearUserMedia(Request $request, Response $response, int $id): Response { - $user = make(UserQuery::class)->get($request, $id, true); + $user = make(UserRepository::class)->get($request, $id, true); $medias = $this->database->query('SELECT * FROM `uploads` WHERE `user_id` = ?', $user->id); @@ -269,7 +269,7 @@ class UserController extends Controller */ public function refreshToken(Request $request, Response $response, int $id): Response { - $query = make(UserQuery::class); + $query = make(UserRepository::class); $user = $query->get($request, $id, true); $this->logger->info('User '.$this->session->get('username')." refreshed token of user $user->id."); diff --git a/app/Database/Migrator.php b/app/Database/Migrator.php index 30c0dd6..a86048d 100644 --- a/app/Database/Migrator.php +++ b/app/Database/Migrator.php @@ -21,7 +21,7 @@ class Migrator * Migrator constructor. * * @param DB $db - * @param string $schemaPath + * @param string|null $schemaPath */ public function __construct(DB $db, ?string $schemaPath) { @@ -29,13 +29,13 @@ class Migrator $this->schemaPath = $schemaPath; } - public function migrate() + public function migrate(): void { $this->db->getPdo()->exec(file_get_contents($this->schemaPath.DIRECTORY_SEPARATOR.'migrations.sql')); $files = glob($this->schemaPath.'/'.$this->db->getCurrentDriver().'/*.sql'); - $names = array_map(function ($path) { + $names = array_map(static function ($path) { return basename($path); }, $files); @@ -51,11 +51,11 @@ class Migrator if (basename($file) === $migration->name && $migration->migrated) { $continue = true; break; - } else { - if (basename($file) === $migration->name && !$migration->migrated) { - $exists = true; - break; - } + } + + if (basename($file) === $migration->name && !$migration->migrated) { + $exists = true; + break; } } if ($continue) { diff --git a/app/Database/Queries/MediaQuery.php b/app/Database/Repositories/MediaRepository.php similarity index 83% rename from app/Database/Queries/MediaQuery.php rename to app/Database/Repositories/MediaRepository.php index 2721d09..0ce2644 100644 --- a/app/Database/Queries/MediaQuery.php +++ b/app/Database/Repositories/MediaRepository.php @@ -1,20 +1,20 @@ userId = $id; @@ -87,27 +87,31 @@ class MediaQuery * * @return $this */ - public function orderBy(string $type = null, $mode = 'ASC') + public function orderBy(string $type = null, $mode = 'ASC'): MediaRepository { - $this->orderBy = ($type === null) ? self::ORDER_TIME : $type; + $this->orderBy = $type ?? self::ORDER_TIME; $this->orderMode = (strtoupper($mode) === 'ASC') ? 'ASC' : 'DESC'; return $this; } /** - * @param string $text + * @param string|null $text * * @return $this */ - public function search(?string $text) + public function search(?string $text): MediaRepository { $this->text = $text; return $this; } - public function filterByTag($tagId) + /** + * @param $tagId + * @return $this + */ + public function filterByTag($tagId): MediaRepository { if ($tagId !== null) { $this->tagId = (int) $tagId; @@ -117,7 +121,11 @@ class MediaQuery } - public function run(int $page) + /** + * @param int $page + * @return $this + */ + public function run(int $page): MediaRepository { if ($this->orderBy == self::ORDER_SIZE) { $this->runWithFileSort($page); @@ -128,7 +136,11 @@ class MediaQuery return $this; } - public function runWithDbSort(int $page) + /** + * @param int $page + * @return $this + */ + public function runWithDbSort(int $page): MediaRepository { $params = []; if ($this->isAdmin) { @@ -170,7 +182,11 @@ class MediaQuery return $this; } - public function runWithFileSort(int $page) + /** + * @param int $page + * @return $this + */ + public function runWithFileSort(int $page): MediaRepository { $this->storage->addPlugin(new ListWith()); @@ -202,16 +218,14 @@ class MediaQuery $params[] = '%'.htmlentities($this->text).'%'; $paths = array_column($files, 'path'); + } elseif ($this->tagId !== null) { + $paths = array_column($files, 'path'); + $ids = $this->getMediaIdsByTagId($this->tagId); + $queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'") AND `uploads`.`id` IN ('.implode(',', $ids).')'; } else { - if ($this->tagId !== null) { - $paths = array_column($files, 'path'); - $ids = $this->getMediaIdsByTagId($this->tagId); - $queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'") AND `uploads`.`id` IN ('.implode(',', $ids).')'; - } else { - $files = array_slice($files, $offset, $limit, true); - $paths = array_column($files, 'path'); - $queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'")'; - } + $files = array_slice($files, $offset, $limit, true); + $paths = array_column($files, 'path'); + $queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'")'; } $medias = $this->db->query($queryMedia, $params)->fetchAll(); diff --git a/app/Database/Queries/TagQuery.php b/app/Database/Repositories/TagRepository.php similarity index 96% rename from app/Database/Queries/TagQuery.php rename to app/Database/Repositories/TagRepository.php index 5d0a4e7..6981973 100644 --- a/app/Database/Queries/TagQuery.php +++ b/app/Database/Repositories/TagRepository.php @@ -1,14 +1,14 @@ session === null) { - throw new \InvalidArgumentException('The session is null.'); + throw new InvalidArgumentException('The session is null.'); } if ($user->id !== $this->session->get('user_id') && !$this->session->get('admin', false)) { @@ -135,19 +136,19 @@ class UserQuery $copyRaw, $id, ]); - } else { - return $this->database->query('UPDATE `users` SET `email`=?, `username`=?, `is_admin`=?, `active`=?, `max_disk_quota`=?, `ldap`=?, `hide_uploads`=?, `copy_raw`=? WHERE `id` = ?', [ - $email, - $username, - $isAdmin, - $isActive, - $maxUserQuota, - $ldap, - $hideUploads, - $copyRaw, - $id, - ]); } + + return $this->database->query('UPDATE `users` SET `email`=?, `username`=?, `is_admin`=?, `active`=?, `max_disk_quota`=?, `ldap`=?, `hide_uploads`=?, `copy_raw`=? WHERE `id` = ?', [ + $email, + $username, + $isAdmin, + $isActive, + $maxUserQuota, + $ldap, + $hideUploads, + $copyRaw, + $id, + ]); } /**