-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preload the files metadata and cache photos-size
Signed-off-by: Joas Schilling <coding@schilljs.com>
- Loading branch information
1 parent
fda6853
commit 2d34e92
Showing
5 changed files
with
142 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> | ||
* | ||
* @author Joas Schilling <coding@schilljs.com> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Talk\Share\Helper; | ||
|
||
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException; | ||
use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException; | ||
use OCP\FilesMetadata\IFilesMetadataManager; | ||
use OCP\FilesMetadata\Model\IFilesMetadata; | ||
|
||
class FilesMetadataCache { | ||
/** @var array<int, array{width: int, height: int}> */ | ||
protected array $filesSizeData = []; | ||
|
||
public function __construct( | ||
protected IFilesMetadataManager $filesMetadataManager, | ||
) { | ||
} | ||
|
||
/** | ||
* @param list<int> $fileIds | ||
*/ | ||
public function preloadMetadata(array $fileIds): void { | ||
$missingFileIds = array_diff($fileIds, array_keys($this->filesSizeData)); | ||
if (empty($missingFileIds)) { | ||
return; | ||
} | ||
|
||
$data = $this->filesMetadataManager->getMetadataForFiles($missingFileIds); | ||
foreach ($data as $fileId => $metadata) { | ||
$this->cachePhotosSize($fileId, $metadata); | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
* @psalm-return array{width: int, height: int} | ||
* @throws FilesMetadataNotFoundException | ||
*/ | ||
public function getMetadataPhotosSizeForFileId(int $fileId): array { | ||
if (!array_key_exists($fileId, $this->filesSizeData)) { | ||
try { | ||
$this->cachePhotosSize($fileId, $this->filesMetadataManager->getMetadata($fileId, true)); | ||
} catch (FilesMetadataNotFoundException) { | ||
$this->filesSizeData[$fileId] = null; | ||
Check failure on line 68 in lib/Share/Helper/FilesMetadataCache.php GitHub Actions / NextcloudInvalidPropertyAssignmentValue
|
||
} | ||
} | ||
|
||
if (!isset($this->filesSizeData[$fileId])) { | ||
return throw new FilesMetadataNotFoundException(); | ||
} | ||
|
||
return $this->filesSizeData[$fileId]; | ||
} | ||
|
||
protected function cachePhotosSize(int $fileId, IFilesMetadata $metadata): void { | ||
if ($metadata->hasKey('photos-size')) { | ||
try { | ||
$sizeMetadata = $metadata->getArray('photos-size'); | ||
} catch (FilesMetadataNotFoundException|FilesMetadataTypeException) { | ||
$this->filesSizeData[$fileId] = null; | ||
Check failure on line 84 in lib/Share/Helper/FilesMetadataCache.php GitHub Actions / NextcloudInvalidPropertyAssignmentValue
|
||
return; | ||
} | ||
|
||
if (isset($sizeMetadata['width'], $sizeMetadata['height'])) { | ||
$dimensions = [ | ||
'width' => $sizeMetadata['width'], | ||
'height' => $sizeMetadata['height'], | ||
]; | ||
$this->filesSizeData[$fileId] = $dimensions; | ||
} else { | ||
$this->filesSizeData[$fileId] = null; | ||
Check failure on line 95 in lib/Share/Helper/FilesMetadataCache.php GitHub Actions / NextcloudInvalidPropertyAssignmentValue
|
||
} | ||
} else { | ||
$this->filesSizeData[$fileId] = null; | ||
Check failure on line 98 in lib/Share/Helper/FilesMetadataCache.php GitHub Actions / NextcloudInvalidPropertyAssignmentValue
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters