refactoring
This commit is contained in:
parent
bddcf34c6d
commit
bfbbf89d79
16 changed files with 171 additions and 157 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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\'');
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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') {
|
||||
|
|
|
@ -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.");
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Queries;
|
||||
namespace App\Database\Repositories;
|
||||
|
||||
use App\Database\DB;
|
||||
use League\Flysystem\FileNotFoundException;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\Plugin\ListWith;
|
||||
|
||||
class MediaQuery
|
||||
class MediaRepository
|
||||
{
|
||||
const PER_PAGE = 21;
|
||||
const PER_PAGE_ADMIN = 27;
|
||||
public const PER_PAGE = 21;
|
||||
public const PER_PAGE_ADMIN = 27;
|
||||
|
||||
const ORDER_TIME = 0;
|
||||
const ORDER_NAME = 1;
|
||||
const ORDER_SIZE = 2;
|
||||
public const ORDER_TIME = 0;
|
||||
public const ORDER_NAME = 1;
|
||||
public const ORDER_SIZE = 2;
|
||||
|
||||
/** @var DB */
|
||||
protected $db;
|
||||
|
@ -62,7 +62,7 @@ class MediaQuery
|
|||
* @param DB $db
|
||||
* @param bool $isAdmin
|
||||
* @param Filesystem $storage
|
||||
* @return MediaQuery
|
||||
* @return MediaRepository
|
||||
*/
|
||||
public static function make(DB $db, Filesystem $storage, bool $isAdmin)
|
||||
{
|
||||
|
@ -74,7 +74,7 @@ class MediaQuery
|
|||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function withUserId($id)
|
||||
public function withUserId($id): MediaRepository
|
||||
{
|
||||
$this->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();
|
|
@ -1,14 +1,14 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Database\Queries;
|
||||
namespace App\Database\Repositories;
|
||||
|
||||
use App\Database\DB;
|
||||
use PDO;
|
||||
|
||||
class TagQuery
|
||||
class TagRepository
|
||||
{
|
||||
const PER_MEDIA_LIMIT = 10;
|
||||
public const PER_MEDIA_LIMIT = 10;
|
||||
|
||||
/**
|
||||
* @var DB
|
|
@ -1,15 +1,16 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Database\Queries;
|
||||
namespace App\Database\Repositories;
|
||||
|
||||
use App\Database\DB;
|
||||
use App\Web\Session;
|
||||
use InvalidArgumentException;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Exception\HttpUnauthorizedException;
|
||||
|
||||
class UserQuery
|
||||
class UserRepository
|
||||
{
|
||||
/**
|
||||
* @var DB
|
||||
|
@ -34,7 +35,7 @@ class UserQuery
|
|||
/**
|
||||
* @param DB $db
|
||||
* @param Session|null $session
|
||||
* @return UserQuery
|
||||
* @return UserRepository
|
||||
*/
|
||||
public static function make(DB $db, Session $session = null)
|
||||
{
|
||||
|
@ -59,7 +60,7 @@ class UserQuery
|
|||
|
||||
if ($authorize) {
|
||||
if ($this->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,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
Loading…
Reference in a new issue