From b9dabc1e144b5bb2f8e7487e67afb426a11396be Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 6 Oct 2023 09:55:39 -0700 Subject: [PATCH] refactor: minor readjustment in util Signed-off-by: Varun Patil --- lib/Util.php | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/Util.php b/lib/Util.php index 28f59317..62650bb7 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -238,10 +238,10 @@ class Util /** * Add OG metadata to a page for a node. * - * @param mixed $node Node to get metadata from - * @param mixed $title Title of the page - * @param mixed $url URL of the page - * @param mixed $previewArgs Preview arguments (e.g. token) + * @param $node Node to get metadata from + * @param $title Title of the page + * @param $url URL of the page + * @param $previewArgs Preview arguments (e.g. token) */ public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs) { @@ -250,16 +250,9 @@ class Util // Get first node if folder if ($node instanceof \OCP\Files\Folder) { - $query = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [ - new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), - new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'video/%'), - ]); - $query = new SearchQuery($query, 1, 0, [], null); - $nodes = $node->search($query); - if (0 === \count($nodes)) { - return; + if (null === ($node = self::getAnyMedia($node))) { + return; // no media in folder } - $node = $nodes[0]; } // Add file type @@ -286,6 +279,26 @@ class Util \OCP\Util::addHeader('meta', ['property' => 'og:image', 'content' => $preview]); } + /** + * Get a random image or video from a given folder. + * + * @param $folder Folder to search + */ + public static function getAnyMedia(\OCP\Files\Folder $folder): Node + { + $query = new SearchQuery(new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [ + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'video/%'), + ]), 1, 0, [], null); + + $nodes = $folder->search($query); + if (0 === \count($nodes)) { + return null; + } + + return $nodes[0]; + } + /** * Check if any encryption is enabled that we can not cope with * such as end-to-end encryption.