Skip to content

Commit

Permalink
Move findBinaryFinder and isFunctionEnabled away from OC_Helper
Browse files Browse the repository at this point in the history
findBinaryFinder is now a service that is still private but with some
minor optimization (remove the hasKey check).

isFunctionEnabled is now in OCP\Util

Both function are still keep but all internal usage in nextcloud/server
were migrated to the new usage, so that we can remove it in 26

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Aug 18, 2022
1 parent 604c175 commit 8b99dbf
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 125 deletions.
6 changes: 3 additions & 3 deletions lib/private/App/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OC\App;

use OCP\IConfig;
use OC\BinaryFinder;

/**
* Class Platform
Expand Down Expand Up @@ -70,9 +71,8 @@ public function getOS(): string {
/**
* @param $command
*/
public function isCommandKnown($command): bool {
$path = \OC_Helper::findBinaryPath($command);
return ($path !== null);
public function isCommandKnown(string $command): bool {
return \OCP\Server::get(BinaryFinder::class)->findBinaryPath($command) !== false;
}

public function getLibraryVersion(string $name): ?string {
Expand Down
70 changes: 70 additions & 0 deletions lib/private/BinaryFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types = 1);
/**
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
* @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 OC;

use OCP\ICache;
use OCP\ICacheFactory;
use Symfony\Component\Process\ExecutableFinder;

/**
* Service that find the binary path for a program
*/
class BinaryFinder {
private ICache $cache;

public function __construct(ICacheFactory $cacheFactory) {
$this->cache = $cacheFactory->createLocal('findBinaryPath');
}

/**
* Try to find a program
*
* @return false|string
*/
public function findBinaryPath(string $program) {
$result = $this->cache->get($program);
if ($result !== null) {
return $result;
}
$result = false;
if (\OCP\Util::isFunctionEnabled('exec')) {
$exeSniffer = new ExecutableFinder();
// Returns null if nothing is found
$result = $exeSniffer->find($program, null, [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/opt/bin',
]);
if ($result === null) {
$result = false;
}
}
// store the value for 5 minutes
$this->cache->set($program, $result, 300);
return $result;
}
}
4 changes: 2 additions & 2 deletions lib/private/LargeFileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getFileSizeViaCurl($fileName) {
* null on failure.
*/
public function getFileSizeViaExec($filename) {
if (\OC_Helper::is_function_enabled('exec')) {
if (\OCP\Util::isFunctionEnabled('exec')) {
$os = strtolower(php_uname('s'));
$arg = escapeshellarg($filename);
$result = null;
Expand Down Expand Up @@ -195,7 +195,7 @@ public function getFileMtime($fullPath) {
$result = - 1;
}
if ($result < 0) {
if (\OC_Helper::is_function_enabled('exec')) {
if (\OCP\Util::isFunctionEnabled('exec')) {
$os = strtolower(php_uname('s'));
if (strpos($os, 'linux') !== false) {
return $this->exec('stat -c %Y ' . escapeshellarg($fullPath));
Expand Down
18 changes: 7 additions & 11 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Mail\IMessage;
use OC\BinaryFinder;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -71,19 +72,14 @@
class Mailer implements IMailer {
/** @var \Swift_Mailer Cached mailer */
private $instance = null;
/** @var IConfig */
private $config;
private IConfig $config;
private LoggerInterface $logger;
/** @var Defaults */
private $defaults;
/** @var IURLGenerator */
private $urlGenerator;
/** @var IL10N */
private $l10n;
/** @var IEventDispatcher */
private $dispatcher;
/** @var IFactory */
private $l10nFactory;
private IURLGenerator $urlGenerator;
private IL10N $l10n;
private IEventDispatcher $dispatcher;
private IFactory $l10nFactory;

public function __construct(IConfig $config,
LoggerInterface $logger,
Expand Down Expand Up @@ -309,7 +305,7 @@ protected function getSendMailInstance(): \Swift_SendmailTransport {
$binaryPath = '/var/qmail/bin/sendmail';
break;
default:
$sendmail = \OC_Helper::findBinaryPath('sendmail');
$sendmail = \OCP\Server::get(BinaryFinder::class)->findBinaryPath('sendmail');
if ($sendmail === null) {
$sendmail = '/usr/sbin/sendmail';
}
Expand Down
112 changes: 38 additions & 74 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,73 +47,41 @@
use function array_key_exists;

class PreviewManager implements IPreview {
/** @var IConfig */
protected $config;

/** @var IRootFolder */
protected $rootFolder;

/** @var IAppData */
protected $appData;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

/** @var Generator */
private $generator;

/** @var GeneratorHelper */
private $helper;

/** @var bool */
protected $providerListDirty = false;

/** @var bool */
protected $registeredCoreProviders = false;

/** @var array */
protected $providers = [];
protected IConfig $config;
protected IRootFolder $rootFolder;
protected IAppData $appData;
protected EventDispatcherInterface $eventDispatcher;
private ?Generator $generator = null;
private GeneratorHelper $helper;
protected bool $providerListDirty = false;
protected bool $registeredCoreProviders = false;
protected array $providers = [];

/** @var array mime type => support status */
protected $mimeTypeSupportMap = [];

/** @var array */
protected $defaultProviders;

/** @var string */
protected $userId;

/** @var Coordinator */
private $bootstrapCoordinator;
protected array $mimeTypeSupportMap = [];
protected array $defaultProviders = [];
protected ?string $userId;
private Coordinator $bootstrapCoordinator;

/**
* Hash map (without value) of loaded bootstrap providers
*
* @var null[]
* @psalm-var array<string, null>
*/
private $loadedBootstrapProviders = [];

/** @var IServerContainer */
private $container;

/**
* PreviewManager constructor.
*
* @param IConfig $config
* @param IRootFolder $rootFolder
* @param IAppData $appData
* @param EventDispatcherInterface $eventDispatcher
* @param string $userId
*/
public function __construct(IConfig $config,
IRootFolder $rootFolder,
IAppData $appData,
EventDispatcherInterface $eventDispatcher,
GeneratorHelper $helper,
$userId,
Coordinator $bootstrapCoordinator,
IServerContainer $container) {
private array $loadedBootstrapProviders = [];
private IServerContainer $container;
private BinaryFinder $binaryFinder;

public function __construct(
IConfig $config,
IRootFolder $rootFolder,
IAppData $appData,
EventDispatcherInterface $eventDispatcher,
GeneratorHelper $helper,
?string $userId,
Coordinator $bootstrapCoordinator,
IServerContainer $container,
BinaryFinder $binaryFinder
) {
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
Expand All @@ -122,6 +90,7 @@ public function __construct(IConfig $config,
$this->userId = $userId;
$this->bootstrapCoordinator = $bootstrapCoordinator;
$this->container = $container;
$this->binaryFinder = $binaryFinder;
}

/**
Expand All @@ -134,7 +103,7 @@ public function __construct(IConfig $config,
* @param \Closure $callable
* @return void
*/
public function registerProvider($mimeTypeRegex, \Closure $callable) {
public function registerProvider($mimeTypeRegex, \Closure $callable): void {
if (!$this->config->getSystemValue('enable_previews', true)) {
return;
}
Expand All @@ -148,9 +117,8 @@ public function registerProvider($mimeTypeRegex, \Closure $callable) {

/**
* Get all providers
* @return array
*/
public function getProviders() {
public function getProviders(): array {
if (!$this->config->getSystemValue('enable_previews', true)) {
return [];
}
Expand All @@ -168,9 +136,8 @@ public function getProviders() {

/**
* Does the manager have any providers
* @return bool
*/
public function hasProviders() {
public function hasProviders(): bool {
$this->registerCoreProviders();
return !empty($this->providers);
}
Expand Down Expand Up @@ -257,11 +224,8 @@ public function isMimeSupported($mimeType = '*') {

/**
* Check if a preview can be generated for a file
*
* @param \OCP\Files\FileInfo $file
* @return bool
*/
public function isAvailable(\OCP\Files\FileInfo $file) {
public function isAvailable(\OCP\Files\FileInfo $file): bool {
if (!$this->config->getSystemValue('enable_previews', true)) {
return false;
}
Expand Down Expand Up @@ -421,10 +385,10 @@ protected function registerCoreProviders() {
// Office requires openoffice or libreoffice
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null);
if (is_null($officeBinary)) {
$officeBinary = \OC_Helper::findBinaryPath('libreoffice');
$officeBinary = $this->binaryFinder->findBinaryPath('libreoffice');
}
if (is_null($officeBinary)) {
$officeBinary = \OC_Helper::findBinaryPath('openoffice');
$officeBinary = $this->binaryFinder->findBinaryPath('openoffice');
}

if (is_string($officeBinary)) {
Expand All @@ -439,9 +403,9 @@ protected function registerCoreProviders() {

// Video requires avconv or ffmpeg
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
$movieBinary = \OC_Helper::findBinaryPath('avconv');
$movieBinary = $this->binaryFinder->findBinaryPath('avconv');
if (is_null($movieBinary)) {
$movieBinary = \OC_Helper::findBinaryPath('ffmpeg');
$movieBinary = $this->binaryFinder->findBinaryPath('ffmpeg');
}

if (is_string($movieBinary)) {
Expand Down Expand Up @@ -469,7 +433,7 @@ private function registerBootstrapProviders(): void {

$this->registerProvider($provider->getMimeTypeRegex(), function () use ($provider) {
try {
return $this->container->query($provider->getService());
return $this->container->get($provider->getService());
} catch (QueryException $e) {
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use OC\Authentication\LoginCredentials\Store;
use OC\Authentication\Token\IProvider;
use OC\Avatar\AvatarManager;
use OC\BinaryFinder;
use OC\Collaboration\Collaborators\GroupPlugin;
use OC\Collaboration\Collaborators\MailPlugin;
use OC\Collaboration\Collaborators\RemoteGroupPlugin;
Expand Down Expand Up @@ -335,7 +336,8 @@ public function __construct($webRoot, \OC\Config $config) {
$c->get(GeneratorHelper::class),
$c->get(ISession::class)->get('user_id'),
$c->get(Coordinator::class),
$c->get(IServerContainer::class)
$c->get(IServerContainer::class),
$c->get(BinaryFinder::class)
);
});
/** @deprecated 19.0.0 */
Expand Down
Loading

0 comments on commit 8b99dbf

Please sign in to comment.