From 355b74e19bec45b961fbbd328162eb0e27d33be9 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 4 Dec 2022 09:40:58 -0800 Subject: [PATCH] general: reduce more DI --- lib/Command/Index.php | 2 +- lib/Controller/ApiBase.php | 18 ++++-------------- lib/Controller/DaysController.php | 2 +- lib/Controller/FacesController.php | 5 ++++- lib/Controller/ImageController.php | 10 ++++++---- lib/Db/TimelineWrite.php | 4 ++-- lib/Listeners/PostDeleteListener.php | 5 ++--- lib/Listeners/PostWriteListener.php | 12 +++--------- 8 files changed, 23 insertions(+), 35 deletions(-) diff --git a/lib/Command/Index.php b/lib/Command/Index.php index ebee78f3..7c2d1cb7 100644 --- a/lib/Command/Index.php +++ b/lib/Command/Index.php @@ -81,7 +81,7 @@ class Index extends Command $this->config = $config; $this->connection = $connection; $this->connectionForSchema = $connectionForSchema; - $this->timelineWrite = new TimelineWrite($connection, $preview); + $this->timelineWrite = new TimelineWrite($connection); } protected function configure(): void diff --git a/lib/Controller/ApiBase.php b/lib/Controller/ApiBase.php index 1e707f33..c832430b 100644 --- a/lib/Controller/ApiBase.php +++ b/lib/Controller/ApiBase.php @@ -26,7 +26,6 @@ namespace OCA\Memories\Controller; use OCA\Memories\AppInfo\Application; use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineRoot; -use OCA\Memories\Db\TimelineWrite; use OCA\Memories\Exif; use OCP\App\IAppManager; use OCP\AppFramework\Controller; @@ -37,10 +36,8 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IDBConnection; -use OCP\IPreview; use OCP\IRequest; use OCP\IUserSession; -use OCP\Share\IManager as IShareManager; class ApiBase extends Controller { @@ -49,9 +46,6 @@ class ApiBase extends Controller protected IRootFolder $rootFolder; protected IAppManager $appManager; protected TimelineQuery $timelineQuery; - protected TimelineWrite $timelineWrite; - protected IShareManager $shareManager; - protected IPreview $previewManager; public function __construct( IRequest $request, @@ -59,9 +53,7 @@ class ApiBase extends Controller IUserSession $userSession, IDBConnection $connection, IRootFolder $rootFolder, - IAppManager $appManager, - IShareManager $shareManager, - IPreview $preview + IAppManager $appManager ) { parent::__construct(Application::APPNAME, $request); @@ -70,10 +62,7 @@ class ApiBase extends Controller $this->connection = $connection; $this->rootFolder = $rootFolder; $this->appManager = $appManager; - $this->shareManager = $shareManager; - $this->previewManager = $preview; $this->timelineQuery = new TimelineQuery($connection); - $this->timelineWrite = new TimelineWrite($connection, $preview); } /** Get logged in user's UID or throw HTTP error */ @@ -243,14 +232,15 @@ class ApiBase extends Controller } // Get share by token - $share = $this->shareManager->getShareByToken($token); + $share = \OC::$server->getShareManager()->getShareByToken($token); if (!PublicController::validateShare($share)) { return null; } // Check if share is password protected - $session = \OC::$server->getSession(); if (($password = $share->getPassword()) !== null) { + $session = \OC::$server->getSession(); + // https://github.com/nextcloud/server/blob/0447b53bda9fe95ea0cbed765aa332584605d652/lib/public/AppFramework/PublicShareController.php#L119 if ($session->get('public_link_authenticated_token') !== $token || $session->get('public_link_authenticated_password_hash') !== $password) { diff --git a/lib/Controller/DaysController.php b/lib/Controller/DaysController.php index 9b8889f7..97616af2 100644 --- a/lib/Controller/DaysController.php +++ b/lib/Controller/DaysController.php @@ -121,7 +121,7 @@ class DaysController extends ApiBase // Convert to actual dayIds if month view if ($this->isMonthView()) { - $dayIds = $this->timelineQuery->monthIdToDayIds($dayIds[0]); + $dayIds = $this->timelineQuery->monthIdToDayIds((int) $dayIds[0]); } // Run actual query diff --git a/lib/Controller/FacesController.php b/lib/Controller/FacesController.php index f002e4bc..acdece65 100644 --- a/lib/Controller/FacesController.php +++ b/lib/Controller/FacesController.php @@ -95,6 +95,9 @@ class FacesController extends ApiBase return new DataResponse([], Http::STATUS_NOT_FOUND); } + // Get preview manager + $previewManager = \OC::$server->getPreviewManager(); + // Find the first detection that has a preview /** @var \Imagick */ $image = null; @@ -113,7 +116,7 @@ class FacesController extends ApiBase // Get (hopefully cached) preview image try { - $preview = $this->previewManager->getPreview($files[0], 2048, 2048, false); + $preview = $previewManager->getPreview($files[0], 2048, 2048, false); $image = new \Imagick(); if (!$image->readImageBlob($preview->getContent())) { diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index a0e852b0..df5f8c1e 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace OCA\Memories\Controller; use OCA\Memories\AppInfo\Application; +use OCA\Memories\Db\TimelineWrite; use OCA\Memories\Exif; use OCP\AppFramework\Http; use OCP\AppFramework\Http\FileDisplayResponse; @@ -61,9 +62,9 @@ class ImageController extends ApiBase } try { - $f = $this->previewManager->getPreview($file, $x, $y, !$a, $mode); - $response = new FileDisplayResponse($f, Http::STATUS_OK, [ - 'Content-Type' => $f->getMimeType(), + $preview = \OC::$server->getPreviewManager()->getPreview($file, $x, $y, !$a, $mode); + $response = new FileDisplayResponse($preview, Http::STATUS_OK, [ + 'Content-Type' => $preview->getMimeType(), ]); $response->cacheFor(3600 * 24, false, true); @@ -146,7 +147,8 @@ class ImageController extends ApiBase } // Reprocess the file - $this->timelineWrite->processFile($file, true); + $timelineWrite = new TimelineWrite($this->connection); + $timelineWrite->processFile($file, true); return new JSONResponse([], Http::STATUS_OK); } diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index 4265ea65..37269fd1 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -17,10 +17,10 @@ class TimelineWrite protected IPreview $preview; protected LivePhoto $livePhoto; - public function __construct(IDBConnection $connection, IPreview &$preview) + public function __construct(IDBConnection $connection) { $this->connection = $connection; - $this->preview = $preview; + $this->preview = \OC::$server->getPreviewManager(); $this->livePhoto = new LivePhoto($connection); } diff --git a/lib/Listeners/PostDeleteListener.php b/lib/Listeners/PostDeleteListener.php index dd223214..065682da 100644 --- a/lib/Listeners/PostDeleteListener.php +++ b/lib/Listeners/PostDeleteListener.php @@ -27,15 +27,14 @@ use OCP\EventDispatcher\IEventListener; use OCP\Files\Events\Node\NodeDeletedEvent; use OCP\Files\Folder; use OCP\IDBConnection; -use OCP\IPreview; class PostDeleteListener implements IEventListener { private TimelineWrite $util; - public function __construct(IDBConnection $connection, IPreview $preview) + public function __construct(IDBConnection $connection) { - $this->util = new TimelineWrite($connection, $preview); + $this->util = new TimelineWrite($connection); } public function handle(Event $event): void diff --git a/lib/Listeners/PostWriteListener.php b/lib/Listeners/PostWriteListener.php index a4e24f31..3ff0a566 100644 --- a/lib/Listeners/PostWriteListener.php +++ b/lib/Listeners/PostWriteListener.php @@ -28,20 +28,14 @@ use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\Folder; use OCP\IDBConnection; -use OCP\IPreview; -use OCP\IUserManager; class PostWriteListener implements IEventListener { private TimelineWrite $timelineWrite; - public function __construct( - IDBConnection $connection, - IUserManager $userManager, - IPreview $preview - ) { - $this->userManager = $userManager; - $this->timelineWrite = new TimelineWrite($connection, $preview); + public function __construct(IDBConnection $connection) + { + $this->timelineWrite = new TimelineWrite($connection); } public function handle(Event $event): void