refactor: minor readjustment in util

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/807/merge
Varun Patil 2023-10-06 09:55:39 -07:00
parent e56828f3e8
commit b9dabc1e14
1 changed files with 26 additions and 13 deletions

View File

@ -238,10 +238,10 @@ class Util
/** /**
* Add OG metadata to a page for a node. * Add OG metadata to a page for a node.
* *
* @param mixed $node Node to get metadata from * @param $node Node to get metadata from
* @param mixed $title Title of the page * @param $title Title of the page
* @param mixed $url URL of the page * @param $url URL of the page
* @param mixed $previewArgs Preview arguments (e.g. token) * @param $previewArgs Preview arguments (e.g. token)
*/ */
public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs) public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs)
{ {
@ -250,16 +250,9 @@ class Util
// Get first node if folder // Get first node if folder
if ($node instanceof \OCP\Files\Folder) { if ($node instanceof \OCP\Files\Folder) {
$query = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [ if (null === ($node = self::getAnyMedia($node))) {
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), return; // no media in folder
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'video/%'),
]);
$query = new SearchQuery($query, 1, 0, [], null);
$nodes = $node->search($query);
if (0 === \count($nodes)) {
return;
} }
$node = $nodes[0];
} }
// Add file type // Add file type
@ -286,6 +279,26 @@ class Util
\OCP\Util::addHeader('meta', ['property' => 'og:image', 'content' => $preview]); \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 * Check if any encryption is enabled that we can not cope with
* such as end-to-end encryption. * such as end-to-end encryption.