Skip to content

Commit

Permalink
refactor: minor readjustment in util
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <[email protected]>
  • Loading branch information
pulsejet committed Oct 6, 2023
1 parent e56828f commit b9dabc1
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ public static function permissionsToStr(int $permissions): string
/**
* 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)
{
Expand All @@ -250,16 +250,9 @@ public static function addOgMetadata(Node $node, string $title, string $url, arr

// 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
Expand All @@ -286,6 +279,26 @@ public static function addOgMetadata(Node $node, string $title, string $url, arr
\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.
Expand Down

0 comments on commit b9dabc1

Please sign in to comment.