From b34bacd0718fa24c67a8ef0aa6f3b824a9b525bb Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 12 May 2016 12:07:06 +0200 Subject: Move Files app to PSR-4 (#24569) * Move lib/ of Files app to PSR-4 * Move tests to PSR-4 --- apps/files/appinfo/application.php | 94 ---- apps/files/command/deleteorphanedfiles.php | 84 ---- apps/files/command/scan.php | 310 ------------- apps/files/command/transferownership.php | 240 ---------- apps/files/controller/apicontroller.php | 239 ---------- apps/files/controller/viewcontroller.php | 304 ------------- apps/files/lib/Activity.php | 424 ++++++++++++++++++ apps/files/lib/ActivityHelper.php | 84 ++++ apps/files/lib/App.php | 47 ++ apps/files/lib/AppInfo/Application.php | 94 ++++ apps/files/lib/BackgroundJob/CleanupFileLocks.php | 57 +++ .../lib/BackgroundJob/DeleteOrphanedItems.php | 153 +++++++ apps/files/lib/BackgroundJob/ScanFiles.php | 114 +++++ apps/files/lib/Capabilities.php | 60 +++ apps/files/lib/Command/DeleteOrphanedFiles.php | 84 ++++ apps/files/lib/Command/Scan.php | 310 +++++++++++++ apps/files/lib/Command/TransferOwnership.php | 240 ++++++++++ apps/files/lib/Controller/ApiController.php | 239 ++++++++++ apps/files/lib/Controller/ViewController.php | 304 +++++++++++++ apps/files/lib/Helper.php | 248 +++++++++++ apps/files/lib/Service/TagService.php | 114 +++++ apps/files/lib/activity.php | 424 ------------------ apps/files/lib/activityhelper.php | 84 ---- apps/files/lib/app.php | 47 -- apps/files/lib/backgroundjob/cleanupfilelocks.php | 57 --- .../lib/backgroundjob/deleteorphaneditems.php | 153 ------- apps/files/lib/backgroundjob/scanfiles.php | 114 ----- apps/files/lib/capabilities.php | 60 --- apps/files/lib/helper.php | 248 ----------- apps/files/service/tagservice.php | 114 ----- apps/files/tests/ActivityTest.php | 375 ++++++++++++++++ .../BackgroundJob/DeleteOrphanedItemsJobTest.php | 259 +++++++++++ apps/files/tests/BackgroundJob/ScanFilesTest.php | 133 ++++++ .../tests/Command/DeleteOrphanedFilesTest.php | 130 ++++++ apps/files/tests/Controller/ApiControllerTest.php | 399 +++++++++++++++++ apps/files/tests/Controller/ViewControllerTest.php | 482 +++++++++++++++++++++ apps/files/tests/HelperTest.php | 114 +++++ apps/files/tests/Service/TagServiceTest.php | 127 ++++++ apps/files/tests/activitytest.php | 375 ---------------- .../backgroundjob/DeleteOrphanedItemsJobTest.php | 259 ----------- apps/files/tests/backgroundjob/ScanFilesTest.php | 133 ------ .../tests/command/deleteorphanedfilestest.php | 130 ------ apps/files/tests/controller/ViewControllerTest.php | 481 -------------------- apps/files/tests/controller/apicontrollertest.php | 399 ----------------- apps/files/tests/helper.php | 114 ----- apps/files/tests/service/tagservice.php | 129 ------ 46 files changed, 4591 insertions(+), 4592 deletions(-) delete mode 100644 apps/files/appinfo/application.php delete mode 100644 apps/files/command/deleteorphanedfiles.php delete mode 100644 apps/files/command/scan.php delete mode 100644 apps/files/command/transferownership.php delete mode 100644 apps/files/controller/apicontroller.php delete mode 100644 apps/files/controller/viewcontroller.php create mode 100644 apps/files/lib/Activity.php create mode 100644 apps/files/lib/ActivityHelper.php create mode 100644 apps/files/lib/App.php create mode 100644 apps/files/lib/AppInfo/Application.php create mode 100644 apps/files/lib/BackgroundJob/CleanupFileLocks.php create mode 100644 apps/files/lib/BackgroundJob/DeleteOrphanedItems.php create mode 100644 apps/files/lib/BackgroundJob/ScanFiles.php create mode 100644 apps/files/lib/Capabilities.php create mode 100644 apps/files/lib/Command/DeleteOrphanedFiles.php create mode 100644 apps/files/lib/Command/Scan.php create mode 100644 apps/files/lib/Command/TransferOwnership.php create mode 100644 apps/files/lib/Controller/ApiController.php create mode 100644 apps/files/lib/Controller/ViewController.php create mode 100644 apps/files/lib/Helper.php create mode 100644 apps/files/lib/Service/TagService.php delete mode 100644 apps/files/lib/activity.php delete mode 100644 apps/files/lib/activityhelper.php delete mode 100644 apps/files/lib/app.php delete mode 100644 apps/files/lib/backgroundjob/cleanupfilelocks.php delete mode 100644 apps/files/lib/backgroundjob/deleteorphaneditems.php delete mode 100644 apps/files/lib/backgroundjob/scanfiles.php delete mode 100644 apps/files/lib/capabilities.php delete mode 100644 apps/files/lib/helper.php delete mode 100644 apps/files/service/tagservice.php create mode 100644 apps/files/tests/ActivityTest.php create mode 100644 apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php create mode 100644 apps/files/tests/BackgroundJob/ScanFilesTest.php create mode 100644 apps/files/tests/Command/DeleteOrphanedFilesTest.php create mode 100644 apps/files/tests/Controller/ApiControllerTest.php create mode 100644 apps/files/tests/Controller/ViewControllerTest.php create mode 100644 apps/files/tests/HelperTest.php create mode 100644 apps/files/tests/Service/TagServiceTest.php delete mode 100644 apps/files/tests/activitytest.php delete mode 100644 apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php delete mode 100644 apps/files/tests/backgroundjob/ScanFilesTest.php delete mode 100644 apps/files/tests/command/deleteorphanedfilestest.php delete mode 100644 apps/files/tests/controller/ViewControllerTest.php delete mode 100644 apps/files/tests/controller/apicontrollertest.php delete mode 100644 apps/files/tests/helper.php delete mode 100644 apps/files/tests/service/tagservice.php (limited to 'apps') diff --git a/apps/files/appinfo/application.php b/apps/files/appinfo/application.php deleted file mode 100644 index b2faa43cc3c..00000000000 --- a/apps/files/appinfo/application.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @author Tobias Kaminsky - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ -namespace OCA\Files\AppInfo; - -use OCA\Files\Controller\ApiController; -use OCP\AppFramework\App; -use \OCA\Files\Service\TagService; -use \OCP\IContainer; -use OCA\Files\Controller\ViewController; - -class Application extends App { - public function __construct(array $urlParams=array()) { - parent::__construct('files', $urlParams); - $container = $this->getContainer(); - $server = $container->getServer(); - - /** - * Controllers - */ - $container->registerService('APIController', function (IContainer $c) use ($server) { - return new ApiController( - $c->query('AppName'), - $c->query('Request'), - $server->getUserSession(), - $c->query('TagService'), - $server->getPreviewManager(), - $server->getShareManager(), - $server->getConfig() - ); - }); - - $container->registerService('ViewController', function (IContainer $c) use ($server) { - return new ViewController( - $c->query('AppName'), - $c->query('Request'), - $server->getURLGenerator(), - $server->getNavigationManager(), - $c->query('L10N'), - $server->getConfig(), - $server->getEventDispatcher(), - $server->getUserSession(), - $server->getAppManager(), - $server->getRootFolder() - ); - }); - - /** - * Core - */ - $container->registerService('L10N', function(IContainer $c) { - return $c->query('ServerContainer')->getL10N($c->query('AppName')); - }); - - /** - * Services - */ - $container->registerService('Tagger', function(IContainer $c) { - return $c->query('ServerContainer')->getTagManager()->load('files'); - }); - $container->registerService('TagService', function(IContainer $c) { - $homeFolder = $c->query('ServerContainer')->getUserFolder(); - return new TagService( - $c->query('ServerContainer')->getUserSession(), - $c->query('Tagger'), - $homeFolder - ); - }); - - /* - * Register capabilities - */ - $container->registerCapability('OCA\Files\Capabilities'); - } -} diff --git a/apps/files/command/deleteorphanedfiles.php b/apps/files/command/deleteorphanedfiles.php deleted file mode 100644 index 91043471ce5..00000000000 --- a/apps/files/command/deleteorphanedfiles.php +++ /dev/null @@ -1,84 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Command; - -use Doctrine\DBAL\Platforms\PostgreSqlPlatform; -use Doctrine\DBAL\Platforms\SqlitePlatform; -use OCP\IDBConnection; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Delete all file entries that have no matching entries in the storage table. - */ -class DeleteOrphanedFiles extends Command { - - const CHUNK_SIZE = 200; - - /** - * @var IDBConnection - */ - protected $connection; - - public function __construct(IDBConnection $connection) { - $this->connection = $connection; - parent::__construct(); - } - - protected function configure() { - $this - ->setName('files:cleanup') - ->setDescription('cleanup filecache'); - } - - public function execute(InputInterface $input, OutputInterface $output) { - $deletedEntries = 0; - - $query = $this->connection->getQueryBuilder(); - $query->select('fc.fileid') - ->from('filecache', 'fc') - ->where($query->expr()->isNull('s.numeric_id')) - ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id')) - ->setMaxResults(self::CHUNK_SIZE); - - $deleteQuery = $this->connection->getQueryBuilder(); - $deleteQuery->delete('filecache') - ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid'))); - - $deletedInLastChunk = self::CHUNK_SIZE; - while ($deletedInLastChunk === self::CHUNK_SIZE) { - $deletedInLastChunk = 0; - $result = $query->execute(); - while ($row = $result->fetch()) { - $deletedInLastChunk++; - $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid']) - ->execute(); - } - $result->closeCursor(); - } - - $output->writeln("$deletedEntries orphaned file cache entries deleted"); - } - -} diff --git a/apps/files/command/scan.php b/apps/files/command/scan.php deleted file mode 100644 index 1ae04c585bb..00000000000 --- a/apps/files/command/scan.php +++ /dev/null @@ -1,310 +0,0 @@ - - * @author Jörn Friedrich Dreyer - * @author martin.mattel@diemattels.at - * @author Morris Jobke - * @author Robin Appelman - * @author Thomas Müller - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Command; - -use OC\Core\Command\Base; -use OC\ForbiddenException; -use OCP\Files\StorageNotAvailableException; -use OCP\IUserManager; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Helper\Table; - -class Scan extends Base { - - /** @var IUserManager $userManager */ - private $userManager; - /** @var float */ - protected $execTime = 0; - /** @var int */ - protected $foldersCounter = 0; - /** @var int */ - protected $filesCounter = 0; - - public function __construct(IUserManager $userManager) { - $this->userManager = $userManager; - parent::__construct(); - } - - protected function configure() { - parent::configure(); - - $this - ->setName('files:scan') - ->setDescription('rescan filesystem') - ->addArgument( - 'user_id', - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, - 'will rescan all files of the given user(s)' - ) - ->addOption( - 'path', - 'p', - InputArgument::OPTIONAL, - 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' - ) - ->addOption( - 'quiet', - 'q', - InputOption::VALUE_NONE, - 'suppress any output' - ) - ->addOption( - 'verbose', - '-v|vv|vvv', - InputOption::VALUE_NONE, - 'verbose the output' - ) - ->addOption( - 'all', - null, - InputOption::VALUE_NONE, - 'will rescan all files of all known users' - ); - } - - public function checkScanWarning($fullPath, OutputInterface $output) { - $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); - $path = basename($fullPath); - - if ($normalizedPath !== $path) { - $output->writeln("\tEntry \"" . $fullPath . '" will not be accessible due to incompatible encoding'); - } - } - - protected function scanFiles($user, $path, $verbose, OutputInterface $output) { - $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); - # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception - # printout and count - if ($verbose) { - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { - $output->writeln("\tFile $path"); - $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new \Exception('ctrl-c'); - } - }); - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { - $output->writeln("\tFolder $path"); - $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new \Exception('ctrl-c'); - } - }); - $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); - }); - # count only - } else { - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { - $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new \Exception('ctrl-c'); - } - }); - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { - $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new \Exception('ctrl-c'); - } - }); - } - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { - $this->checkScanWarning($path, $output); - }); - $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { - $this->checkScanWarning($path, $output); - }); - - try { - $scanner->scan($path); - } catch (ForbiddenException $e) { - $output->writeln("Home storage for user $user not writable"); - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); - } catch (\Exception $e) { - if ($e->getMessage() !== 'ctrl-c') { - $output->writeln('Exception while scanning: ' . $e->getMessage() . "\n" . $e->getTraceAsString() . ''); - } - return; - } - } - - - protected function execute(InputInterface $input, OutputInterface $output) { - $inputPath = $input->getOption('path'); - if ($inputPath) { - $inputPath = '/' . trim($inputPath, '/'); - list (, $user,) = explode('/', $inputPath, 3); - $users = array($user); - } else if ($input->getOption('all')) { - $users = $this->userManager->search(''); - } else { - $users = $input->getArgument('user_id'); - } - - # no messaging level option means: no full printout but statistics - # $quiet means no print at all - # $verbose means full printout including statistics - # -q -v full stat - # 0 0 no yes - # 0 1 yes yes - # 1 -- no no (quiet overrules verbose) - $verbose = $input->getOption('verbose'); - $quiet = $input->getOption('quiet'); - # restrict the verbosity level to VERBOSITY_VERBOSE - if ($output->getVerbosity()>OutputInterface::VERBOSITY_VERBOSE) { - $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); - } - if ($quiet) { - $verbose = false; - } - - # check quantity of users to be process and show it on the command line - $users_total = count($users); - if ($users_total === 0) { - $output->writeln("Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\""); - return; - } else { - if ($users_total > 1) { - $output->writeln("\nScanning files for $users_total users"); - } - } - - $this->initTools(); - - $user_count = 0; - foreach ($users as $user) { - if (is_object($user)) { - $user = $user->getUID(); - } - $path = $inputPath ? $inputPath : '/' . $user; - $user_count += 1; - if ($this->userManager->userExists($user)) { - # add an extra line when verbose is set to optical separate users - if ($verbose) {$output->writeln(""); } - $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); - # full: printout data if $verbose was set - $this->scanFiles($user, $path, $verbose, $output); - } else { - $output->writeln("Unknown user $user_count $user"); - } - # check on each user if there was a user interrupt (ctrl-c) and exit foreach - if ($this->hasBeenInterrupted()) { - break; - } - } - - # stat: printout statistics if $quiet was not set - if (!$quiet) { - $this->presentStats($output); - } - } - - /** - * Initialises some useful tools for the Command - */ - protected function initTools() { - // Start the timer - $this->execTime = -microtime(true); - // Convert PHP errors to exceptions - set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); - } - - /** - * Processes PHP errors as exceptions in order to be able to keep track of problems - * - * @see https://secure.php.net/manual/en/function.set-error-handler.php - * - * @param int $severity the level of the error raised - * @param string $message - * @param string $file the filename that the error was raised in - * @param int $line the line number the error was raised - * - * @throws \ErrorException - */ - public function exceptionErrorHandler($severity, $message, $file, $line) { - if (!(error_reporting() & $severity)) { - // This error code is not included in error_reporting - return; - } - throw new \ErrorException($message, 0, $severity, $file, $line); - } - - /** - * @param OutputInterface $output - */ - protected function presentStats(OutputInterface $output) { - // Stop the timer - $this->execTime += microtime(true); - $output->writeln(""); - - $headers = [ - 'Folders', 'Files', 'Elapsed time' - ]; - - $this->showSummary($headers, null, $output); - } - - /** - * Shows a summary of operations - * - * @param string[] $headers - * @param string[] $rows - * @param OutputInterface $output - */ - protected function showSummary($headers, $rows, OutputInterface $output) { - $niceDate = $this->formatExecTime(); - if (!$rows) { - $rows = [ - $this->foldersCounter, - $this->filesCounter, - $niceDate, - ]; - } - $table = new Table($output); - $table - ->setHeaders($headers) - ->setRows([$rows]); - $table->render(); - } - - - /** - * Formats microtime into a human readable format - * - * @return string - */ - protected function formatExecTime() { - list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); - - # if you want to have microseconds add this: . '.' . $tens; - return date('H:i:s', $secs); - } - -} diff --git a/apps/files/command/transferownership.php b/apps/files/command/transferownership.php deleted file mode 100644 index 1f46efdde0d..00000000000 --- a/apps/files/command/transferownership.php +++ /dev/null @@ -1,240 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Command; - -use OC\Files\Filesystem; -use OC\Files\View; -use OCP\Files\FileInfo; -use OCP\Files\Mount\IMountManager; -use OCP\IUserManager; -use OCP\Share\IManager; -use OCP\Share\IShare; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\ProgressBar; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -class TransferOwnership extends Command { - - /** @var IUserManager $userManager */ - private $userManager; - - /** @var IManager */ - private $shareManager; - - /** @var IMountManager */ - private $mountManager; - - /** @var FileInfo[] */ - private $allFiles = []; - - /** @var FileInfo[] */ - private $encryptedFiles = []; - - /** @var IShare[] */ - private $shares = []; - - /** @var string */ - private $sourceUser; - - /** @var string */ - private $destinationUser; - - /** @var string */ - private $finalTarget; - - public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager) { - $this->userManager = $userManager; - $this->shareManager = $shareManager; - $this->mountManager = $mountManager; - parent::__construct(); - } - - protected function configure() { - $this - ->setName('files:transfer-ownership') - ->setDescription('All files and folders are moved to another user - shares are moved as well.') - ->addArgument( - 'source-user', - InputArgument::REQUIRED, - 'owner of files which shall be moved' - ) - ->addArgument( - 'destination-user', - InputArgument::REQUIRED, - 'user who will be the new owner of the files' - ); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $this->sourceUser = $input->getArgument('source-user'); - $this->destinationUser = $input->getArgument('destination-user'); - if (!$this->userManager->userExists($this->sourceUser)) { - $output->writeln("Unknown source user $this->sourceUser"); - return; - } - if (!$this->userManager->userExists($this->destinationUser)) { - $output->writeln("Unknown destination user $this->destinationUser"); - return; - } - - // target user has to be ready - if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { - $output->writeln("The target user is not ready to accept files. The user has at least to be logged in once."); - return; - } - - $date = date('c'); - $this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; - - // setup filesystem - Filesystem::initMountPoints($this->sourceUser); - Filesystem::initMountPoints($this->destinationUser); - - // analyse source folder - $this->analyse($output); - - // collect all the shares - $this->collectUsersShares($output); - - // transfer the files - $this->transfer($output); - - // restore the shares - $this->restoreShares($output); - } - - private function walkFiles(View $view, $path, \Closure $callBack) { - foreach ($view->getDirectoryContent($path) as $fileInfo) { - if (!$callBack($fileInfo)) { - return; - } - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { - $this->walkFiles($view, $fileInfo->getPath(), $callBack); - } - } - } - - /** - * @param OutputInterface $output - * @throws \Exception - */ - protected function analyse(OutputInterface $output) { - $view = new View(); - $output->writeln("Analysing files of $this->sourceUser ..."); - $progress = new ProgressBar($output); - $progress->start(); - $self = $this; - $this->walkFiles($view, "$this->sourceUser/files", - function (FileInfo $fileInfo) use ($progress, $self) { - if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { - return true; - } - $progress->advance(); - $this->allFiles[] = $fileInfo; - if ($fileInfo->isEncrypted()) { - $this->encryptedFiles[] = $fileInfo; - } - return true; - }); - $progress->finish(); - $output->writeln(''); - - // no file is allowed to be encrypted - if (!empty($this->encryptedFiles)) { - $output->writeln("Some files are encrypted - please decrypt them first"); - foreach($this->encryptedFiles as $encryptedFile) { - /** @var FileInfo $encryptedFile */ - $output->writeln(" " . $encryptedFile->getPath()); - } - throw new \Exception('Execution terminated.'); - } - - } - - /** - * @param OutputInterface $output - */ - private function collectUsersShares(OutputInterface $output) { - $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); - - $progress = new ProgressBar($output, count($this->shares)); - foreach([\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE] as $shareType) { - $offset = 0; - while (true) { - $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); - $progress->advance(count($sharePage)); - if (empty($sharePage)) { - break; - } - $this->shares = array_merge($this->shares, $sharePage); - $offset += 50; - } - } - - $progress->finish(); - $output->writeln(''); - } - - /** - * @param OutputInterface $output - */ - protected function transfer(OutputInterface $output) { - $view = new View(); - $output->writeln("Transferring files to $this->finalTarget ..."); - $view->rename("$this->sourceUser/files", $this->finalTarget); - // because the files folder is moved away we need to recreate it - $view->mkdir("$this->sourceUser/files"); - } - - /** - * @param OutputInterface $output - */ - private function restoreShares(OutputInterface $output) { - $output->writeln("Restoring shares ..."); - $progress = new ProgressBar($output, count($this->shares)); - - foreach($this->shares as $share) { - if ($share->getSharedWith() === $this->destinationUser) { - // Unmount the shares before deleting, so we don't try to get the storage later on. - $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); - if ($shareMountPoint) { - $this->mountManager->removeMount($shareMountPoint->getMountPoint()); - } - $this->shareManager->deleteShare($share); - } else { - if ($share->getShareOwner() === $this->sourceUser) { - $share->setShareOwner($this->destinationUser); - } - if ($share->getSharedBy() === $this->sourceUser) { - $share->setSharedBy($this->destinationUser); - } - - $this->shareManager->updateShare($share); - } - $progress->advance(); - } - $progress->finish(); - $output->writeln(''); - } -} diff --git a/apps/files/controller/apicontroller.php b/apps/files/controller/apicontroller.php deleted file mode 100644 index 072498c7b5f..00000000000 --- a/apps/files/controller/apicontroller.php +++ /dev/null @@ -1,239 +0,0 @@ - - * @author Christoph Wurst - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Tobias Kaminsky - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Controller; - -use OCP\AppFramework\Http; -use OCP\AppFramework\Controller; -use OCP\IConfig; -use OCP\IRequest; -use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Http\DataDisplayResponse; -use OCP\AppFramework\Http\Response; -use OCA\Files\Service\TagService; -use OCP\IPreview; -use OCP\Share\IManager; -use OCP\Files\Node; -use OCP\IUserSession; - -/** - * Class ApiController - * - * @package OCA\Files\Controller - */ -class ApiController extends Controller { - /** @var TagService */ - private $tagService; - /** @var IManager **/ - private $shareManager; - /** @var IPreview */ - private $previewManager; - /** IUserSession */ - private $userSession; - /** IConfig */ - private $config; - - /** - * @param string $appName - * @param IRequest $request - * @param TagService $tagService - * @param IPreview $previewManager - */ - public function __construct($appName, - IRequest $request, - IUserSession $userSession, - TagService $tagService, - IPreview $previewManager, - IManager $shareManager, - IConfig $config) { - parent::__construct($appName, $request); - $this->userSession = $userSession; - $this->tagService = $tagService; - $this->previewManager = $previewManager; - $this->shareManager = $shareManager; - $this->config = $config; - } - - /** - * Gets a thumbnail of the specified file - * - * @since API version 1.0 - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @param int $x - * @param int $y - * @param string $file URL-encoded filename - * @return DataResponse|DataDisplayResponse - */ - public function getThumbnail($x, $y, $file) { - if($x < 1 || $y < 1) { - return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); - } - - $preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true); - if ($preview->valid()) { - return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']); - } else { - return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); - } - } - - /** - * Updates the info of the specified file path - * The passed tags are absolute, which means they will - * replace the actual tag selection. - * - * @NoAdminRequired - * - * @param string $path path - * @param array|string $tags array of tags - * @return DataResponse - */ - public function updateFileTags($path, $tags = null) { - $result = []; - // if tags specified or empty array, update tags - if (!is_null($tags)) { - try { - $this->tagService->updateFileTags($path, $tags); - } catch (\OCP\Files\NotFoundException $e) { - return new DataResponse([ - 'message' => $e->getMessage() - ], Http::STATUS_NOT_FOUND); - } catch (\OCP\Files\StorageNotAvailableException $e) { - return new DataResponse([ - 'message' => $e->getMessage() - ], Http::STATUS_SERVICE_UNAVAILABLE); - } catch (\Exception $e) { - return new DataResponse([ - 'message' => $e->getMessage() - ], Http::STATUS_NOT_FOUND); - } - $result['tags'] = $tags; - } - return new DataResponse($result); - } - - /** - * Returns a list of all files tagged with the given tag. - * - * @NoAdminRequired - * - * @param string $tagName tag name to filter by - * @return DataResponse - */ - public function getFilesByTag($tagName) { - $files = array(); - $nodes = $this->tagService->getFilesByTag($tagName); - foreach ($nodes as &$node) { - $shareTypes = $this->getShareTypes($node); - $fileInfo = $node->getFileInfo(); - $file = \OCA\Files\Helper::formatFileInfo($fileInfo); - $parts = explode('/', dirname($fileInfo->getPath()), 4); - if(isset($parts[3])) { - $file['path'] = '/' . $parts[3]; - } else { - $file['path'] = '/'; - } - $file['tags'] = [$tagName]; - if (!empty($shareTypes)) { - $file['shareTypes'] = $shareTypes; - } - $files[] = $file; - } - return new DataResponse(['files' => $files]); - } - - /** - * Return a list of share types for outgoing shares - * - * @param Node $node file node - * - * @return int[] array of share types - */ - private function getShareTypes(Node $node) { - $userId = $this->userSession->getUser()->getUID(); - $shareTypes = []; - $requestedShareTypes = [ - \OCP\Share::SHARE_TYPE_USER, - \OCP\Share::SHARE_TYPE_GROUP, - \OCP\Share::SHARE_TYPE_LINK, - \OCP\Share::SHARE_TYPE_REMOTE - ]; - foreach ($requestedShareTypes as $requestedShareType) { - // one of each type is enough to find out about the types - $shares = $this->shareManager->getSharesBy( - $userId, - $requestedShareType, - $node, - false, - 1 - ); - if (!empty($shares)) { - $shareTypes[] = $requestedShareType; - } - } - return $shareTypes; - } - - /** - * Change the default sort mode - * - * @NoAdminRequired - * - * @param string $mode - * @param string $direction - * @return Response - */ - public function updateFileSorting($mode, $direction) { - $allowedMode = ['name', 'size', 'mtime']; - $allowedDirection = ['asc', 'desc']; - if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { - $response = new Response(); - $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); - return $response; - } - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); - return new Response(); - } - - /** - * Toggle default for showing/hiding hidden files - * - * @NoAdminRequired - * - * @param bool $show - */ - public function showHiddenFiles($show) { - $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int) $show); - return new Response(); - } - -} diff --git a/apps/files/controller/viewcontroller.php b/apps/files/controller/viewcontroller.php deleted file mode 100644 index 7539a3ab649..00000000000 --- a/apps/files/controller/viewcontroller.php +++ /dev/null @@ -1,304 +0,0 @@ - - * @author Lukas Reschke - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Controller; - -use OC\AppFramework\Http\Request; -use OCP\AppFramework\Controller; -use OCP\AppFramework\Http\ContentSecurityPolicy; -use OCP\AppFramework\Http\Response; -use OCP\AppFramework\Http\RedirectResponse; -use OCP\AppFramework\Http\TemplateResponse; -use OCP\IConfig; -use OCP\IL10N; -use OCP\INavigationManager; -use OCP\IRequest; -use OCP\IURLGenerator; -use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use OCP\AppFramework\Http\NotFoundResponse; -use OCP\Files\Folder; -use OCP\App\IAppManager; - -/** - * Class ViewController - * - * @package OCA\Files\Controller - */ -class ViewController extends Controller { - /** @var string */ - protected $appName; - /** @var IRequest */ - protected $request; - /** @var IURLGenerator */ - protected $urlGenerator; - /** @var INavigationManager */ - protected $navigationManager; - /** @var IL10N */ - protected $l10n; - /** @var IConfig */ - protected $config; - /** @var EventDispatcherInterface */ - protected $eventDispatcher; - /** @var IUserSession */ - protected $userSession; - /** @var IAppManager */ - protected $appManager; - /** @var \OCP\Files\Folder */ - protected $rootFolder; - - /** - * @param string $appName - * @param IRequest $request - * @param IURLGenerator $urlGenerator - * @param INavigationManager $navigationManager - * @param IL10N $l10n - * @param IConfig $config - * @param EventDispatcherInterface $eventDispatcherInterface - * @param IUserSession $userSession - * @param IAppManager $appManager - * @param Folder $rootFolder - */ - public function __construct($appName, - IRequest $request, - IURLGenerator $urlGenerator, - INavigationManager $navigationManager, - IL10N $l10n, - IConfig $config, - EventDispatcherInterface $eventDispatcherInterface, - IUserSession $userSession, - IAppManager $appManager, - Folder $rootFolder - ) { - parent::__construct($appName, $request); - $this->appName = $appName; - $this->request = $request; - $this->urlGenerator = $urlGenerator; - $this->navigationManager = $navigationManager; - $this->l10n = $l10n; - $this->config = $config; - $this->eventDispatcher = $eventDispatcherInterface; - $this->userSession = $userSession; - $this->appManager = $appManager; - $this->rootFolder = $rootFolder; - } - - /** - * @param string $appName - * @param string $scriptName - * @return string - */ - protected function renderScript($appName, $scriptName) { - $content = ''; - $appPath = \OC_App::getAppPath($appName); - $scriptPath = $appPath . '/' . $scriptName; - if (file_exists($scriptPath)) { - // TODO: sanitize path / script name ? - ob_start(); - include $scriptPath; - $content = ob_get_contents(); - @ob_end_clean(); - } - return $content; - } - - /** - * FIXME: Replace with non static code - * - * @return array - * @throws \OCP\Files\NotFoundException - */ - protected function getStorageInfo() { - $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); - return \OC_Helper::getStorageInfo('/', $dirInfo); - } - - /** - * @NoCSRFRequired - * @NoAdminRequired - * - * @param string $dir - * @param string $view - * @param string $fileid - * @return TemplateResponse - * @throws \OCP\Files\NotFoundException - */ - public function index($dir = '', $view = '', $fileid = null) { - if ($fileid !== null) { - return $this->showFile($fileid); - } - - $nav = new \OCP\Template('files', 'appnavigation', ''); - - // Load the files we need - \OCP\Util::addStyle('files', 'files'); - \OCP\Util::addStyle('files', 'upload'); - \OCP\Util::addStyle('files', 'mobile'); - \OCP\Util::addscript('files', 'app'); - \OCP\Util::addscript('files', 'file-upload'); - \OCP\Util::addscript('files', 'newfilemenu'); - \OCP\Util::addscript('files', 'jquery.fileupload'); - \OCP\Util::addscript('files', 'jquery-visibility'); - \OCP\Util::addscript('files', 'fileinfomodel'); - \OCP\Util::addscript('files', 'filesummary'); - \OCP\Util::addscript('files', 'breadcrumb'); - \OCP\Util::addscript('files', 'filelist'); - \OCP\Util::addscript('files', 'search'); - - \OCP\Util::addScript('files', 'favoritesfilelist'); - \OCP\Util::addScript('files', 'tagsplugin'); - \OCP\Util::addScript('files', 'favoritesplugin'); - - \OCP\Util::addScript('files', 'detailfileinfoview'); - \OCP\Util::addScript('files', 'detailtabview'); - \OCP\Util::addScript('files', 'mainfileinfodetailview'); - \OCP\Util::addScript('files', 'detailsview'); - \OCP\Util::addStyle('files', 'detailsView'); - - \OC_Util::addVendorScript('core', 'handlebars/handlebars'); - - \OCP\Util::addscript('files', 'fileactions'); - \OCP\Util::addscript('files', 'fileactionsmenu'); - \OCP\Util::addscript('files', 'files'); - \OCP\Util::addscript('files', 'keyboardshortcuts'); - \OCP\Util::addscript('files', 'navigation'); - - // if IE8 and "?dir=path&view=someview" was specified, reformat the URL to use a hash like "#?dir=path&view=someview" - $isIE8 = $this->request->isUserAgent([Request::USER_AGENT_IE_8]); - if ($isIE8 && ($dir !== '' || $view !== '')) { - $dir = !empty($dir) ? $dir : '/'; - $view = !empty($view) ? $view : 'files'; - $hash = '#?dir=' . \OCP\Util::encodePath($dir); - if ($view !== 'files') { - $hash .= '&view=' . urlencode($view); - } - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index') . $hash); - } - - // mostly for the home storage's free space - // FIXME: Make non static - $storageInfo = $this->getStorageInfo(); - - \OCA\Files\App::getNavigationManager()->add( - [ - 'id' => 'favorites', - 'appname' => 'files', - 'script' => 'simplelist.php', - 'order' => 5, - 'name' => $this->l10n->t('Favorites') - ] - ); - - $navItems = \OCA\Files\App::getNavigationManager()->getAll(); - usort($navItems, function($item1, $item2) { - return $item1['order'] - $item2['order']; - }); - $nav->assign('navigationItems', $navItems); - - $contentItems = []; - - // render the container content for every navigation item - foreach ($navItems as $item) { - $content = ''; - if (isset($item['script'])) { - $content = $this->renderScript($item['appname'], $item['script']); - } - $contentItem = []; - $contentItem['id'] = $item['id']; - $contentItem['content'] = $content; - $contentItems[] = $contentItem; - } - - $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts'); - - $params = []; - $params['usedSpacePercent'] = (int)$storageInfo['relative']; - $params['owner'] = $storageInfo['owner']; - $params['ownerDisplayName'] = $storageInfo['ownerDisplayName']; - $params['isPublic'] = false; - $params['mailNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no'); - $params['mailPublicNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no'); - $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'); - $user = $this->userSession->getUser()->getUID(); - $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); - $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); - $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); - $params['showHiddenFiles'] = $showHidden ? 1 : 0; - $params['appNavigation'] = $nav; - $params['appContents'] = $contentItems; - $this->navigationManager->setActiveEntry('files_index'); - - $response = new TemplateResponse( - $this->appName, - 'index', - $params - ); - $policy = new ContentSecurityPolicy(); - $policy->addAllowedFrameDomain('\'self\''); - $response->setContentSecurityPolicy($policy); - - return $response; - } - - /** - * Redirects to the file list and highlight the given file id - * - * @param string $fileId file id to show - * @return Response redirect response or not found response - * - * @NoCSRFRequired - * @NoAdminRequired - */ - public function showFile($fileId) { - try { - $uid = $this->userSession->getUser()->getUID(); - $baseFolder = $this->rootFolder->get($uid . '/files/'); - $files = $baseFolder->getById($fileId); - $params = []; - - if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { - $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); - $files = $baseFolder->getById($fileId); - $params['view'] = 'trashbin'; - } - - if (!empty($files)) { - $file = current($files); - if ($file instanceof Folder) { - // set the full path to enter the folder - $params['dir'] = $baseFolder->getRelativePath($file->getPath()); - } else { - // set parent path as dir - $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); - // and scroll to the entry - $params['scrollto'] = $file->getName(); - } - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); - } - } catch (\OCP\Files\NotFoundException $e) { - return new NotFoundResponse(); - } - return new NotFoundResponse(); - } -} diff --git a/apps/files/lib/Activity.php b/apps/files/lib/Activity.php new file mode 100644 index 00000000000..1cbd6c3b973 --- /dev/null +++ b/apps/files/lib/Activity.php @@ -0,0 +1,424 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files; + +use OCP\IDBConnection; +use OCP\L10N\IFactory; +use OCP\Activity\IExtension; +use OCP\Activity\IManager; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IURLGenerator; + +class Activity implements IExtension { + const APP_FILES = 'files'; + const FILTER_FILES = 'files'; + const FILTER_FAVORITES = 'files_favorites'; + + const TYPE_SHARE_CREATED = 'file_created'; + const TYPE_SHARE_CHANGED = 'file_changed'; + const TYPE_SHARE_DELETED = 'file_deleted'; + const TYPE_SHARE_RESTORED = 'file_restored'; + const TYPE_FAVORITES = 'files_favorites'; + + /** @var IL10N */ + protected $l; + + /** @var IFactory */ + protected $languageFactory; + + /** @var IURLGenerator */ + protected $URLGenerator; + + /** @var \OCP\Activity\IManager */ + protected $activityManager; + + /** @var \OCP\IDBConnection */ + protected $connection; + + /** @var \OCP\IConfig */ + protected $config; + + /** @var \OCA\Files\ActivityHelper */ + protected $helper; + + /** + * @param IFactory $languageFactory + * @param IURLGenerator $URLGenerator + * @param IManager $activityManager + * @param ActivityHelper $helper + * @param IDBConnection $connection + * @param IConfig $config + */ + public function __construct(IFactory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ActivityHelper $helper, IDBConnection $connection, IConfig $config) { + $this->languageFactory = $languageFactory; + $this->URLGenerator = $URLGenerator; + $this->l = $this->getL10N(); + $this->activityManager = $activityManager; + $this->helper = $helper; + $this->connection = $connection; + $this->config = $config; + } + + /** + * @param string|null $languageCode + * @return IL10N + */ + protected function getL10N($languageCode = null) { + return $this->languageFactory->get(self::APP_FILES, $languageCode); + } + + /** + * The extension can return an array of additional notification types. + * If no additional types are to be added false is to be returned + * + * @param string $languageCode + * @return array|false Array "stringID of the type" => "translated string description for the setting" + * or Array "stringID of the type" => [ + * 'desc' => "translated string description for the setting" + * 'methods' => [self::METHOD_*], + * ] + */ + public function getNotificationTypes($languageCode) { + $l = $this->getL10N($languageCode); + return [ + self::TYPE_SHARE_CREATED => (string) $l->t('A new file or folder has been created'), + self::TYPE_SHARE_CHANGED => (string) $l->t('A file or folder has been changed'), + self::TYPE_FAVORITES => [ + 'desc' => (string) $l->t('Limit notifications about creation and changes to your favorite files (Stream only)'), + 'methods' => [self::METHOD_STREAM], + ], + self::TYPE_SHARE_DELETED => (string) $l->t('A file or folder has been deleted'), + self::TYPE_SHARE_RESTORED => (string) $l->t('A file or folder has been restored'), + ]; + } + + /** + * For a given method additional types to be displayed in the settings can be returned. + * In case no additional types are to be added false is to be returned. + * + * @param string $method + * @return array|false + */ + public function getDefaultTypes($method) { + if ($method === self::METHOD_STREAM) { + $settings = array(); + $settings[] = self::TYPE_SHARE_CREATED; + $settings[] = self::TYPE_SHARE_CHANGED; + $settings[] = self::TYPE_SHARE_DELETED; + $settings[] = self::TYPE_SHARE_RESTORED; + return $settings; + } + + return false; + } + + /** + * The extension can translate a given message to the requested languages. + * If no translation is available false is to be returned. + * + * @param string $app + * @param string $text + * @param array $params + * @param boolean $stripPath + * @param boolean $highlightParams + * @param string $languageCode + * @return string|false + */ + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { + if ($app !== self::APP_FILES) { + return false; + } + + $l = $this->getL10N($languageCode); + + if ($this->activityManager->isFormattingFilteredObject()) { + $translation = $this->translateShort($text, $l, $params); + if ($translation !== false) { + return $translation; + } + } + + return $this->translateLong($text, $l, $params); + } + + /** + * @param string $text + * @param IL10N $l + * @param array $params + * @return string|false + */ + protected function translateLong($text, IL10N $l, array $params) { + switch ($text) { + case 'created_self': + return (string) $l->t('You created %1$s', $params); + case 'created_by': + return (string) $l->t('%2$s created %1$s', $params); + case 'created_public': + return (string) $l->t('%1$s was created in a public folder', $params); + case 'changed_self': + return (string) $l->t('You changed %1$s', $params); + case 'changed_by': + return (string) $l->t('%2$s changed %1$s', $params); + case 'deleted_self': + return (string) $l->t('You deleted %1$s', $params); + case 'deleted_by': + return (string) $l->t('%2$s deleted %1$s', $params); + case 'restored_self': + return (string) $l->t('You restored %1$s', $params); + case 'restored_by': + return (string) $l->t('%2$s restored %1$s', $params); + + default: + return false; + } + } + + /** + * @param string $text + * @param IL10N $l + * @param array $params + * @return string|false + */ + protected function translateShort($text, IL10N $l, array $params) { + switch ($text) { + case 'changed_by': + return (string) $l->t('Changed by %2$s', $params); + case 'deleted_by': + return (string) $l->t('Deleted by %2$s', $params); + case 'restored_by': + return (string) $l->t('Restored by %2$s', $params); + + default: + return false; + } + } + + /** + * The extension can define the type of parameters for translation + * + * Currently known types are: + * * file => will strip away the path of the file and add a tooltip with it + * * username => will add the avatar of the user + * + * @param string $app + * @param string $text + * @return array|false + */ + function getSpecialParameterList($app, $text) { + if ($app === self::APP_FILES) { + switch ($text) { + case 'created_self': + case 'created_by': + case 'created_public': + case 'changed_self': + case 'changed_by': + case 'deleted_self': + case 'deleted_by': + case 'restored_self': + case 'restored_by': + return [ + 0 => 'file', + 1 => 'username', + ]; + } + } + + return false; + } + + /** + * A string naming the css class for the icon to be used can be returned. + * If no icon is known for the given type false is to be returned. + * + * @param string $type + * @return string|false + */ + public function getTypeIcon($type) { + switch ($type) { + case self::TYPE_SHARE_CHANGED: + return 'icon-change'; + case self::TYPE_SHARE_CREATED: + return 'icon-add-color'; + case self::TYPE_SHARE_DELETED: + return 'icon-delete-color'; + + default: + return false; + } + } + + /** + * The extension can define the parameter grouping by returning the index as integer. + * In case no grouping is required false is to be returned. + * + * @param array $activity + * @return integer|false + */ + public function getGroupParameter($activity) { + if ($activity['app'] === self::APP_FILES) { + switch ($activity['subject']) { + case 'created_self': + case 'created_by': + case 'changed_self': + case 'changed_by': + case 'deleted_self': + case 'deleted_by': + case 'restored_self': + case 'restored_by': + return 0; + } + } + + return false; + } + + /** + * The extension can define additional navigation entries. The array returned has to contain two keys 'top' + * and 'apps' which hold arrays with the relevant entries. + * If no further entries are to be added false is no be returned. + * + * @return array|false + */ + public function getNavigation() { + return [ + 'top' => [ + self::FILTER_FAVORITES => [ + 'id' => self::FILTER_FAVORITES, + 'name' => (string) $this->l->t('Favorites'), + 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FAVORITES]), + ], + ], + 'apps' => [ + self::FILTER_FILES => [ + 'id' => self::FILTER_FILES, + 'name' => (string) $this->l->t('Files'), + 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FILES]), + ], + ], + ]; + } + + /** + * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not. + * + * @param string $filterValue + * @return boolean + */ + public function isFilterValid($filterValue) { + return $filterValue === self::FILTER_FILES || $filterValue === self::FILTER_FAVORITES; + } + + /** + * The extension can filter the types based on the filter if required. + * In case no filter is to be applied false is to be returned unchanged. + * + * @param array $types + * @param string $filter + * @return array|false + */ + public function filterNotificationTypes($types, $filter) { + if ($filter === self::FILTER_FILES || $filter === self::FILTER_FAVORITES) { + return array_intersect([ + self::TYPE_SHARE_CREATED, + self::TYPE_SHARE_CHANGED, + self::TYPE_SHARE_DELETED, + self::TYPE_SHARE_RESTORED, + ], $types); + } + return false; + } + + /** + * For a given filter the extension can specify the sql query conditions including parameters for that query. + * In case the extension does not know the filter false is to be returned. + * The query condition and the parameters are to be returned as array with two elements. + * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); + * + * @param string $filter + * @return array|false + */ + public function getQueryForFilter($filter) { + $user = $this->activityManager->getCurrentUserId(); + // Display actions from all files + if ($filter === self::FILTER_FILES) { + return ['`app` = ?', [self::APP_FILES]]; + } + + if (!$user) { + // Remaining filters only work with a user/token + return false; + } + + // Display actions from favorites only + if ($filter === self::FILTER_FAVORITES || in_array($filter, ['all', 'by', 'self']) && $this->userSettingFavoritesOnly($user)) { + try { + $favorites = $this->helper->getFavoriteFilePaths($user); + } catch (\RuntimeException $e) { + // Too many favorites, can not put them into one query anymore... + return ['`app` = ?', [self::APP_FILES]]; + } + + /* + * Display activities only, when they are not `type` create/change + * or `file` is a favorite or in a favorite folder + */ + $parameters = $fileQueryList = []; + $parameters[] = self::APP_FILES; + $parameters[] = self::APP_FILES; + + $fileQueryList[] = '(`type` <> ? AND `type` <> ?)'; + $parameters[] = self::TYPE_SHARE_CREATED; + $parameters[] = self::TYPE_SHARE_CHANGED; + + foreach ($favorites['items'] as $favorite) { + $fileQueryList[] = '`file` = ?'; + $parameters[] = $favorite; + } + foreach ($favorites['folders'] as $favorite) { + $fileQueryList[] = '`file` LIKE ?'; + $parameters[] = $this->connection->escapeLikeParameter($favorite) . '/%'; + } + + return [ + ' CASE ' + . 'WHEN `app` <> ? THEN 1 ' + . 'WHEN `app` = ? AND (' . implode(' OR ', $fileQueryList) . ') THEN 1 ' + . 'ELSE 0 ' + . 'END = 1 ', + $parameters, + ]; + } + return false; + } + + /** + * Is the file actions favorite limitation enabled? + * + * @param string $user + * @return bool + */ + protected function userSettingFavoritesOnly($user) { + return (bool) $this->config->getUserValue($user, 'activity', 'notify_' . self::METHOD_STREAM . '_' . self::TYPE_FAVORITES, false); + } +} diff --git a/apps/files/lib/ActivityHelper.php b/apps/files/lib/ActivityHelper.php new file mode 100644 index 00000000000..046dd59bc76 --- /dev/null +++ b/apps/files/lib/ActivityHelper.php @@ -0,0 +1,84 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files; + +use OCP\Files\Folder; +use OCP\ITagManager; + +class ActivityHelper { + /** If a user has a lot of favorites the query might get too slow and long */ + const FAVORITE_LIMIT = 50; + + /** @var \OCP\ITagManager */ + protected $tagManager; + + /** + * @param ITagManager $tagManager + */ + public function __construct(ITagManager $tagManager) { + $this->tagManager = $tagManager; + } + + /** + * Returns an array with the favorites + * + * @param string $user + * @return array + * @throws \RuntimeException when too many or no favorites where found + */ + public function getFavoriteFilePaths($user) { + $tags = $this->tagManager->load('files', [], false, $user); + $favorites = $tags->getFavorites(); + + if (empty($favorites)) { + throw new \RuntimeException('No favorites', 1); + } else if (isset($favorites[self::FAVORITE_LIMIT])) { + throw new \RuntimeException('Too many favorites', 2); + } + + // Can not DI because the user is not known on instantiation + $rootFolder = \OC::$server->getUserFolder($user); + $folders = $items = []; + foreach ($favorites as $favorite) { + $nodes = $rootFolder->getById($favorite); + if (!empty($nodes)) { + /** @var \OCP\Files\Node $node */ + $node = array_shift($nodes); + $path = substr($node->getPath(), strlen($user . '/files/')); + + $items[] = $path; + if ($node instanceof Folder) { + $folders[] = $path; + } + } + } + + if (empty($items)) { + throw new \RuntimeException('No favorites', 1); + } + + return [ + 'items' => $items, + 'folders' => $folders, + ]; + } +} diff --git a/apps/files/lib/App.php b/apps/files/lib/App.php new file mode 100644 index 00000000000..981c41ff413 --- /dev/null +++ b/apps/files/lib/App.php @@ -0,0 +1,47 @@ + + * @author Morris Jobke + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + + +namespace OCA\Files; + +class App { + /** + * @var \OCP\INavigationManager + */ + private static $navigationManager; + + /** + * Returns the app's navigation manager + * + * @return \OCP\INavigationManager + */ + public static function getNavigationManager() { + // TODO: move this into a service in the Application class + if (self::$navigationManager === null) { + self::$navigationManager = new \OC\NavigationManager(); + } + return self::$navigationManager; + } + +} diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php new file mode 100644 index 00000000000..b2faa43cc3c --- /dev/null +++ b/apps/files/lib/AppInfo/Application.php @@ -0,0 +1,94 @@ + + * @author Tobias Kaminsky + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ +namespace OCA\Files\AppInfo; + +use OCA\Files\Controller\ApiController; +use OCP\AppFramework\App; +use \OCA\Files\Service\TagService; +use \OCP\IContainer; +use OCA\Files\Controller\ViewController; + +class Application extends App { + public function __construct(array $urlParams=array()) { + parent::__construct('files', $urlParams); + $container = $this->getContainer(); + $server = $container->getServer(); + + /** + * Controllers + */ + $container->registerService('APIController', function (IContainer $c) use ($server) { + return new ApiController( + $c->query('AppName'), + $c->query('Request'), + $server->getUserSession(), + $c->query('TagService'), + $server->getPreviewManager(), + $server->getShareManager(), + $server->getConfig() + ); + }); + + $container->registerService('ViewController', function (IContainer $c) use ($server) { + return new ViewController( + $c->query('AppName'), + $c->query('Request'), + $server->getURLGenerator(), + $server->getNavigationManager(), + $c->query('L10N'), + $server->getConfig(), + $server->getEventDispatcher(), + $server->getUserSession(), + $server->getAppManager(), + $server->getRootFolder() + ); + }); + + /** + * Core + */ + $container->registerService('L10N', function(IContainer $c) { + return $c->query('ServerContainer')->getL10N($c->query('AppName')); + }); + + /** + * Services + */ + $container->registerService('Tagger', function(IContainer $c) { + return $c->query('ServerContainer')->getTagManager()->load('files'); + }); + $container->registerService('TagService', function(IContainer $c) { + $homeFolder = $c->query('ServerContainer')->getUserFolder(); + return new TagService( + $c->query('ServerContainer')->getUserSession(), + $c->query('Tagger'), + $homeFolder + ); + }); + + /* + * Register capabilities + */ + $container->registerCapability('OCA\Files\Capabilities'); + } +} diff --git a/apps/files/lib/BackgroundJob/CleanupFileLocks.php b/apps/files/lib/BackgroundJob/CleanupFileLocks.php new file mode 100644 index 00000000000..b5cf8e94551 --- /dev/null +++ b/apps/files/lib/BackgroundJob/CleanupFileLocks.php @@ -0,0 +1,57 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OC\Lock\DBLockingProvider; + +/** + * Clean up all file locks that are expired for the DB file locking provider + */ +class CleanupFileLocks extends TimedJob { + + /** + * Default interval in minutes + * + * @var int $defaultIntervalMin + **/ + protected $defaultIntervalMin = 5; + + /** + * sets the correct interval for this timed job + */ + public function __construct() { + $this->interval = $this->defaultIntervalMin * 60; + } + + /** + * Makes the background job do its work + * + * @param array $argument unused argument + */ + public function run($argument) { + $lockingProvider = \OC::$server->getLockingProvider(); + if($lockingProvider instanceof DBLockingProvider) { + $lockingProvider->cleanExpiredLocks(); + } + } +} diff --git a/apps/files/lib/BackgroundJob/DeleteOrphanedItems.php b/apps/files/lib/BackgroundJob/DeleteOrphanedItems.php new file mode 100644 index 00000000000..1eef9c24e0c --- /dev/null +++ b/apps/files/lib/BackgroundJob/DeleteOrphanedItems.php @@ -0,0 +1,153 @@ + + * @author Joas Schilling + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OCP\DB\QueryBuilder\IQueryBuilder; + +/** + * Delete all share entries that have no matching entries in the file cache table. + */ +class DeleteOrphanedItems extends TimedJob { + + const CHUNK_SIZE = 200; + + /** @var \OCP\IDBConnection */ + protected $connection; + + /** @var \OCP\ILogger */ + protected $logger; + + /** + * Default interval in minutes + * + * @var int $defaultIntervalMin + **/ + protected $defaultIntervalMin = 60; + + /** + * sets the correct interval for this timed job + */ + public function __construct() { + $this->interval = $this->defaultIntervalMin * 60; + $this->connection = \OC::$server->getDatabaseConnection(); + $this->logger = \OC::$server->getLogger(); + } + + /** + * Makes the background job do its work + * + * @param array $argument unused argument + */ + public function run($argument) { + $this->cleanSystemTags(); + $this->cleanUserTags(); + $this->cleanComments(); + $this->cleanCommentMarkers(); + } + + /** + * Deleting orphaned system tag mappings + * + * @param string $table + * @param string $idCol + * @param string $typeCol + * @return int Number of deleted entries + */ + protected function cleanUp($table, $idCol, $typeCol) { + $deletedEntries = 0; + + $query = $this->connection->getQueryBuilder(); + $query->select('t1.' . $idCol) + ->from($table, 't1') + ->where($query->expr()->eq($typeCol, $query->expr()->literal('files'))) + ->andWhere($query->expr()->isNull('t2.fileid')) + ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid')) + ->groupBy('t1.' . $idCol) + ->setMaxResults(self::CHUNK_SIZE); + + $deleteQuery = $this->connection->getQueryBuilder(); + $deleteQuery->delete($table) + ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid'))); + + $deletedInLastChunk = self::CHUNK_SIZE; + while ($deletedInLastChunk === self::CHUNK_SIZE) { + $result = $query->execute(); + $deletedInLastChunk = 0; + while ($row = $result->fetch()) { + $deletedInLastChunk++; + $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row[$idCol]) + ->execute(); + } + $result->closeCursor(); + } + + return $deletedEntries; + } + + /** + * Deleting orphaned system tag mappings + * + * @return int Number of deleted entries + */ + protected function cleanSystemTags() { + $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype'); + $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } + + /** + * Deleting orphaned user tag mappings + * + * @return int Number of deleted entries + */ + protected function cleanUserTags() { + $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type'); + $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } + + /** + * Deleting orphaned comments + * + * @return int Number of deleted entries + */ + protected function cleanComments() { + $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type'); + $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } + + /** + * Deleting orphaned comment read markers + * + * @return int Number of deleted entries + */ + protected function cleanCommentMarkers() { + $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type'); + $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } + +} diff --git a/apps/files/lib/BackgroundJob/ScanFiles.php b/apps/files/lib/BackgroundJob/ScanFiles.php new file mode 100644 index 00000000000..dcc180bcfbe --- /dev/null +++ b/apps/files/lib/BackgroundJob/ScanFiles.php @@ -0,0 +1,114 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\BackgroundJob; + +use OC\Files\Utils\Scanner; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\ILogger; +use OCP\IUser; +use OCP\IUserManager; + +/** + * Class ScanFiles is a background job used to run the file scanner over the user + * accounts to ensure integrity of the file cache. + * + * @package OCA\Files\BackgroundJob + */ +class ScanFiles extends \OC\BackgroundJob\TimedJob { + /** @var IConfig */ + private $config; + /** @var IUserManager */ + private $userManager; + /** @var IDBConnection */ + private $dbConnection; + /** @var ILogger */ + private $logger; + /** Amount of users that should get scanned per execution */ + const USERS_PER_SESSION = 500; + + /** + * @param IConfig|null $config + * @param IUserManager|null $userManager + * @param IDBConnection|null $dbConnection + * @param ILogger|null $logger + */ + public function __construct(IConfig $config = null, + IUserManager $userManager = null, + IDBConnection $dbConnection = null, + ILogger $logger = null) { + // Run once per 10 minutes + $this->setInterval(60 * 10); + + if (is_null($userManager) || is_null($config)) { + $this->fixDIForJobs(); + } else { + $this->config = $config; + $this->userManager = $userManager; + $this->logger = $logger; + } + } + + protected function fixDIForJobs() { + $this->config = \OC::$server->getConfig(); + $this->userManager = \OC::$server->getUserManager(); + $this->logger = \OC::$server->getLogger(); + } + + /** + * @param IUser $user + */ + protected function runScanner(IUser $user) { + try { + $scanner = new Scanner( + $user->getUID(), + $this->dbConnection, + $this->logger + ); + $scanner->backgroundScan(''); + } catch (\Exception $e) { + $this->logger->logException($e, ['app' => 'files']); + } + \OC_Util::tearDownFS(); + } + + /** + * @param $argument + * @throws \Exception + */ + protected function run($argument) { + $offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0); + $users = $this->userManager->search('', self::USERS_PER_SESSION, $offset); + if (!count($users)) { + // No users found, reset offset and retry + $offset = 0; + $users = $this->userManager->search('', self::USERS_PER_SESSION); + } + + $offset += self::USERS_PER_SESSION; + $this->config->setAppValue('files', 'cronjob_scan_files', $offset); + + foreach ($users as $user) { + $this->runScanner($user); + } + } +} diff --git a/apps/files/lib/Capabilities.php b/apps/files/lib/Capabilities.php new file mode 100644 index 00000000000..dc49ca174b3 --- /dev/null +++ b/apps/files/lib/Capabilities.php @@ -0,0 +1,60 @@ + + * @author Roeland Jago Douma + * @author Tom Needham + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files; + +use OCP\Capabilities\ICapability; +use OCP\IConfig; + +/** + * Class Capabilities + * + * @package OCA\Files + */ +class Capabilities implements ICapability { + /** @var IConfig */ + protected $config; + + /** + * Capabilities constructor. + * + * @param IConfig $config + */ + public function __construct(IConfig $config) { + $this->config = $config; + } + + /** + * Return this classes capabilities + * + * @return array + */ + public function getCapabilities() { + return [ + 'files' => [ + 'bigfilechunking' => true, + 'blacklisted_files' => $this->config->getSystemValue('blacklisted_files', ['.htaccess']), + ], + ]; + } +} diff --git a/apps/files/lib/Command/DeleteOrphanedFiles.php b/apps/files/lib/Command/DeleteOrphanedFiles.php new file mode 100644 index 00000000000..91043471ce5 --- /dev/null +++ b/apps/files/lib/Command/DeleteOrphanedFiles.php @@ -0,0 +1,84 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Command; + +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; +use Doctrine\DBAL\Platforms\SqlitePlatform; +use OCP\IDBConnection; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Delete all file entries that have no matching entries in the storage table. + */ +class DeleteOrphanedFiles extends Command { + + const CHUNK_SIZE = 200; + + /** + * @var IDBConnection + */ + protected $connection; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('files:cleanup') + ->setDescription('cleanup filecache'); + } + + public function execute(InputInterface $input, OutputInterface $output) { + $deletedEntries = 0; + + $query = $this->connection->getQueryBuilder(); + $query->select('fc.fileid') + ->from('filecache', 'fc') + ->where($query->expr()->isNull('s.numeric_id')) + ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id')) + ->setMaxResults(self::CHUNK_SIZE); + + $deleteQuery = $this->connection->getQueryBuilder(); + $deleteQuery->delete('filecache') + ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid'))); + + $deletedInLastChunk = self::CHUNK_SIZE; + while ($deletedInLastChunk === self::CHUNK_SIZE) { + $deletedInLastChunk = 0; + $result = $query->execute(); + while ($row = $result->fetch()) { + $deletedInLastChunk++; + $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid']) + ->execute(); + } + $result->closeCursor(); + } + + $output->writeln("$deletedEntries orphaned file cache entries deleted"); + } + +} diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php new file mode 100644 index 00000000000..1ae04c585bb --- /dev/null +++ b/apps/files/lib/Command/Scan.php @@ -0,0 +1,310 @@ + + * @author Jörn Friedrich Dreyer + * @author martin.mattel@diemattels.at + * @author Morris Jobke + * @author Robin Appelman + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Command; + +use OC\Core\Command\Base; +use OC\ForbiddenException; +use OCP\Files\StorageNotAvailableException; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Helper\Table; + +class Scan extends Base { + + /** @var IUserManager $userManager */ + private $userManager; + /** @var float */ + protected $execTime = 0; + /** @var int */ + protected $foldersCounter = 0; + /** @var int */ + protected $filesCounter = 0; + + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + parent::configure(); + + $this + ->setName('files:scan') + ->setDescription('rescan filesystem') + ->addArgument( + 'user_id', + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, + 'will rescan all files of the given user(s)' + ) + ->addOption( + 'path', + 'p', + InputArgument::OPTIONAL, + 'limit rescan to this path, eg. --path="/alice/files/Music", the user_id is determined by the path and the user_id parameter and --all are ignored' + ) + ->addOption( + 'quiet', + 'q', + InputOption::VALUE_NONE, + 'suppress any output' + ) + ->addOption( + 'verbose', + '-v|vv|vvv', + InputOption::VALUE_NONE, + 'verbose the output' + ) + ->addOption( + 'all', + null, + InputOption::VALUE_NONE, + 'will rescan all files of all known users' + ); + } + + public function checkScanWarning($fullPath, OutputInterface $output) { + $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath)); + $path = basename($fullPath); + + if ($normalizedPath !== $path) { + $output->writeln("\tEntry \"" . $fullPath . '" will not be accessible due to incompatible encoding'); + } + } + + protected function scanFiles($user, $path, $verbose, OutputInterface $output) { + $scanner = new \OC\Files\Utils\Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger()); + # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception + # printout and count + if ($verbose) { + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { + $output->writeln("\tFile $path"); + $this->filesCounter += 1; + if ($this->hasBeenInterrupted()) { + throw new \Exception('ctrl-c'); + } + }); + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { + $output->writeln("\tFolder $path"); + $this->foldersCounter += 1; + if ($this->hasBeenInterrupted()) { + throw new \Exception('ctrl-c'); + } + }); + $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { + $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); + }); + # count only + } else { + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { + $this->filesCounter += 1; + if ($this->hasBeenInterrupted()) { + throw new \Exception('ctrl-c'); + } + }); + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { + $this->foldersCounter += 1; + if ($this->hasBeenInterrupted()) { + throw new \Exception('ctrl-c'); + } + }); + } + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { + $this->checkScanWarning($path, $output); + }); + $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) { + $this->checkScanWarning($path, $output); + }); + + try { + $scanner->scan($path); + } catch (ForbiddenException $e) { + $output->writeln("Home storage for user $user not writable"); + $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); + } catch (\Exception $e) { + if ($e->getMessage() !== 'ctrl-c') { + $output->writeln('Exception while scanning: ' . $e->getMessage() . "\n" . $e->getTraceAsString() . ''); + } + return; + } + } + + + protected function execute(InputInterface $input, OutputInterface $output) { + $inputPath = $input->getOption('path'); + if ($inputPath) { + $inputPath = '/' . trim($inputPath, '/'); + list (, $user,) = explode('/', $inputPath, 3); + $users = array($user); + } else if ($input->getOption('all')) { + $users = $this->userManager->search(''); + } else { + $users = $input->getArgument('user_id'); + } + + # no messaging level option means: no full printout but statistics + # $quiet means no print at all + # $verbose means full printout including statistics + # -q -v full stat + # 0 0 no yes + # 0 1 yes yes + # 1 -- no no (quiet overrules verbose) + $verbose = $input->getOption('verbose'); + $quiet = $input->getOption('quiet'); + # restrict the verbosity level to VERBOSITY_VERBOSE + if ($output->getVerbosity()>OutputInterface::VERBOSITY_VERBOSE) { + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); + } + if ($quiet) { + $verbose = false; + } + + # check quantity of users to be process and show it on the command line + $users_total = count($users); + if ($users_total === 0) { + $output->writeln("Please specify the user id to scan, \"--all\" to scan for all users or \"--path=...\""); + return; + } else { + if ($users_total > 1) { + $output->writeln("\nScanning files for $users_total users"); + } + } + + $this->initTools(); + + $user_count = 0; + foreach ($users as $user) { + if (is_object($user)) { + $user = $user->getUID(); + } + $path = $inputPath ? $inputPath : '/' . $user; + $user_count += 1; + if ($this->userManager->userExists($user)) { + # add an extra line when verbose is set to optical separate users + if ($verbose) {$output->writeln(""); } + $output->writeln("Starting scan for user $user_count out of $users_total ($user)"); + # full: printout data if $verbose was set + $this->scanFiles($user, $path, $verbose, $output); + } else { + $output->writeln("Unknown user $user_count $user"); + } + # check on each user if there was a user interrupt (ctrl-c) and exit foreach + if ($this->hasBeenInterrupted()) { + break; + } + } + + # stat: printout statistics if $quiet was not set + if (!$quiet) { + $this->presentStats($output); + } + } + + /** + * Initialises some useful tools for the Command + */ + protected function initTools() { + // Start the timer + $this->execTime = -microtime(true); + // Convert PHP errors to exceptions + set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); + } + + /** + * Processes PHP errors as exceptions in order to be able to keep track of problems + * + * @see https://secure.php.net/manual/en/function.set-error-handler.php + * + * @param int $severity the level of the error raised + * @param string $message + * @param string $file the filename that the error was raised in + * @param int $line the line number the error was raised + * + * @throws \ErrorException + */ + public function exceptionErrorHandler($severity, $message, $file, $line) { + if (!(error_reporting() & $severity)) { + // This error code is not included in error_reporting + return; + } + throw new \ErrorException($message, 0, $severity, $file, $line); + } + + /** + * @param OutputInterface $output + */ + protected function presentStats(OutputInterface $output) { + // Stop the timer + $this->execTime += microtime(true); + $output->writeln(""); + + $headers = [ + 'Folders', 'Files', 'Elapsed time' + ]; + + $this->showSummary($headers, null, $output); + } + + /** + * Shows a summary of operations + * + * @param string[] $headers + * @param string[] $rows + * @param OutputInterface $output + */ + protected function showSummary($headers, $rows, OutputInterface $output) { + $niceDate = $this->formatExecTime(); + if (!$rows) { + $rows = [ + $this->foldersCounter, + $this->filesCounter, + $niceDate, + ]; + } + $table = new Table($output); + $table + ->setHeaders($headers) + ->setRows([$rows]); + $table->render(); + } + + + /** + * Formats microtime into a human readable format + * + * @return string + */ + protected function formatExecTime() { + list($secs, $tens) = explode('.', sprintf("%.1f", ($this->execTime))); + + # if you want to have microseconds add this: . '.' . $tens; + return date('H:i:s', $secs); + } + +} diff --git a/apps/files/lib/Command/TransferOwnership.php b/apps/files/lib/Command/TransferOwnership.php new file mode 100644 index 00000000000..1f46efdde0d --- /dev/null +++ b/apps/files/lib/Command/TransferOwnership.php @@ -0,0 +1,240 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Command; + +use OC\Files\Filesystem; +use OC\Files\View; +use OCP\Files\FileInfo; +use OCP\Files\Mount\IMountManager; +use OCP\IUserManager; +use OCP\Share\IManager; +use OCP\Share\IShare; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class TransferOwnership extends Command { + + /** @var IUserManager $userManager */ + private $userManager; + + /** @var IManager */ + private $shareManager; + + /** @var IMountManager */ + private $mountManager; + + /** @var FileInfo[] */ + private $allFiles = []; + + /** @var FileInfo[] */ + private $encryptedFiles = []; + + /** @var IShare[] */ + private $shares = []; + + /** @var string */ + private $sourceUser; + + /** @var string */ + private $destinationUser; + + /** @var string */ + private $finalTarget; + + public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager) { + $this->userManager = $userManager; + $this->shareManager = $shareManager; + $this->mountManager = $mountManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('files:transfer-ownership') + ->setDescription('All files and folders are moved to another user - shares are moved as well.') + ->addArgument( + 'source-user', + InputArgument::REQUIRED, + 'owner of files which shall be moved' + ) + ->addArgument( + 'destination-user', + InputArgument::REQUIRED, + 'user who will be the new owner of the files' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $this->sourceUser = $input->getArgument('source-user'); + $this->destinationUser = $input->getArgument('destination-user'); + if (!$this->userManager->userExists($this->sourceUser)) { + $output->writeln("Unknown source user $this->sourceUser"); + return; + } + if (!$this->userManager->userExists($this->destinationUser)) { + $output->writeln("Unknown destination user $this->destinationUser"); + return; + } + + // target user has to be ready + if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { + $output->writeln("The target user is not ready to accept files. The user has at least to be logged in once."); + return; + } + + $date = date('c'); + $this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; + + // setup filesystem + Filesystem::initMountPoints($this->sourceUser); + Filesystem::initMountPoints($this->destinationUser); + + // analyse source folder + $this->analyse($output); + + // collect all the shares + $this->collectUsersShares($output); + + // transfer the files + $this->transfer($output); + + // restore the shares + $this->restoreShares($output); + } + + private function walkFiles(View $view, $path, \Closure $callBack) { + foreach ($view->getDirectoryContent($path) as $fileInfo) { + if (!$callBack($fileInfo)) { + return; + } + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { + $this->walkFiles($view, $fileInfo->getPath(), $callBack); + } + } + } + + /** + * @param OutputInterface $output + * @throws \Exception + */ + protected function analyse(OutputInterface $output) { + $view = new View(); + $output->writeln("Analysing files of $this->sourceUser ..."); + $progress = new ProgressBar($output); + $progress->start(); + $self = $this; + $this->walkFiles($view, "$this->sourceUser/files", + function (FileInfo $fileInfo) use ($progress, $self) { + if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { + return true; + } + $progress->advance(); + $this->allFiles[] = $fileInfo; + if ($fileInfo->isEncrypted()) { + $this->encryptedFiles[] = $fileInfo; + } + return true; + }); + $progress->finish(); + $output->writeln(''); + + // no file is allowed to be encrypted + if (!empty($this->encryptedFiles)) { + $output->writeln("Some files are encrypted - please decrypt them first"); + foreach($this->encryptedFiles as $encryptedFile) { + /** @var FileInfo $encryptedFile */ + $output->writeln(" " . $encryptedFile->getPath()); + } + throw new \Exception('Execution terminated.'); + } + + } + + /** + * @param OutputInterface $output + */ + private function collectUsersShares(OutputInterface $output) { + $output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); + + $progress = new ProgressBar($output, count($this->shares)); + foreach([\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE] as $shareType) { + $offset = 0; + while (true) { + $sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); + $progress->advance(count($sharePage)); + if (empty($sharePage)) { + break; + } + $this->shares = array_merge($this->shares, $sharePage); + $offset += 50; + } + } + + $progress->finish(); + $output->writeln(''); + } + + /** + * @param OutputInterface $output + */ + protected function transfer(OutputInterface $output) { + $view = new View(); + $output->writeln("Transferring files to $this->finalTarget ..."); + $view->rename("$this->sourceUser/files", $this->finalTarget); + // because the files folder is moved away we need to recreate it + $view->mkdir("$this->sourceUser/files"); + } + + /** + * @param OutputInterface $output + */ + private function restoreShares(OutputInterface $output) { + $output->writeln("Restoring shares ..."); + $progress = new ProgressBar($output, count($this->shares)); + + foreach($this->shares as $share) { + if ($share->getSharedWith() === $this->destinationUser) { + // Unmount the shares before deleting, so we don't try to get the storage later on. + $shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); + if ($shareMountPoint) { + $this->mountManager->removeMount($shareMountPoint->getMountPoint()); + } + $this->shareManager->deleteShare($share); + } else { + if ($share->getShareOwner() === $this->sourceUser) { + $share->setShareOwner($this->destinationUser); + } + if ($share->getSharedBy() === $this->sourceUser) { + $share->setSharedBy($this->destinationUser); + } + + $this->shareManager->updateShare($share); + } + $progress->advance(); + } + $progress->finish(); + $output->writeln(''); + } +} diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php new file mode 100644 index 00000000000..072498c7b5f --- /dev/null +++ b/apps/files/lib/Controller/ApiController.php @@ -0,0 +1,239 @@ + + * @author Christoph Wurst + * @author Lukas Reschke + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Tobias Kaminsky + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Controller; + +use OCP\AppFramework\Http; +use OCP\AppFramework\Controller; +use OCP\IConfig; +use OCP\IRequest; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\DataDisplayResponse; +use OCP\AppFramework\Http\Response; +use OCA\Files\Service\TagService; +use OCP\IPreview; +use OCP\Share\IManager; +use OCP\Files\Node; +use OCP\IUserSession; + +/** + * Class ApiController + * + * @package OCA\Files\Controller + */ +class ApiController extends Controller { + /** @var TagService */ + private $tagService; + /** @var IManager **/ + private $shareManager; + /** @var IPreview */ + private $previewManager; + /** IUserSession */ + private $userSession; + /** IConfig */ + private $config; + + /** + * @param string $appName + * @param IRequest $request + * @param TagService $tagService + * @param IPreview $previewManager + */ + public function __construct($appName, + IRequest $request, + IUserSession $userSession, + TagService $tagService, + IPreview $previewManager, + IManager $shareManager, + IConfig $config) { + parent::__construct($appName, $request); + $this->userSession = $userSession; + $this->tagService = $tagService; + $this->previewManager = $previewManager; + $this->shareManager = $shareManager; + $this->config = $config; + } + + /** + * Gets a thumbnail of the specified file + * + * @since API version 1.0 + * + * @NoAdminRequired + * @NoCSRFRequired + * + * @param int $x + * @param int $y + * @param string $file URL-encoded filename + * @return DataResponse|DataDisplayResponse + */ + public function getThumbnail($x, $y, $file) { + if($x < 1 || $y < 1) { + return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); + } + + $preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true); + if ($preview->valid()) { + return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']); + } else { + return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); + } + } + + /** + * Updates the info of the specified file path + * The passed tags are absolute, which means they will + * replace the actual tag selection. + * + * @NoAdminRequired + * + * @param string $path path + * @param array|string $tags array of tags + * @return DataResponse + */ + public function updateFileTags($path, $tags = null) { + $result = []; + // if tags specified or empty array, update tags + if (!is_null($tags)) { + try { + $this->tagService->updateFileTags($path, $tags); + } catch (\OCP\Files\NotFoundException $e) { + return new DataResponse([ + 'message' => $e->getMessage() + ], Http::STATUS_NOT_FOUND); + } catch (\OCP\Files\StorageNotAvailableException $e) { + return new DataResponse([ + 'message' => $e->getMessage() + ], Http::STATUS_SERVICE_UNAVAILABLE); + } catch (\Exception $e) { + return new DataResponse([ + 'message' => $e->getMessage() + ], Http::STATUS_NOT_FOUND); + } + $result['tags'] = $tags; + } + return new DataResponse($result); + } + + /** + * Returns a list of all files tagged with the given tag. + * + * @NoAdminRequired + * + * @param string $tagName tag name to filter by + * @return DataResponse + */ + public function getFilesByTag($tagName) { + $files = array(); + $nodes = $this->tagService->getFilesByTag($tagName); + foreach ($nodes as &$node) { + $shareTypes = $this->getShareTypes($node); + $fileInfo = $node->getFileInfo(); + $file = \OCA\Files\Helper::formatFileInfo($fileInfo); + $parts = explode('/', dirname($fileInfo->getPath()), 4); + if(isset($parts[3])) { + $file['path'] = '/' . $parts[3]; + } else { + $file['path'] = '/'; + } + $file['tags'] = [$tagName]; + if (!empty($shareTypes)) { + $file['shareTypes'] = $shareTypes; + } + $files[] = $file; + } + return new DataResponse(['files' => $files]); + } + + /** + * Return a list of share types for outgoing shares + * + * @param Node $node file node + * + * @return int[] array of share types + */ + private function getShareTypes(Node $node) { + $userId = $this->userSession->getUser()->getUID(); + $shareTypes = []; + $requestedShareTypes = [ + \OCP\Share::SHARE_TYPE_USER, + \OCP\Share::SHARE_TYPE_GROUP, + \OCP\Share::SHARE_TYPE_LINK, + \OCP\Share::SHARE_TYPE_REMOTE + ]; + foreach ($requestedShareTypes as $requestedShareType) { + // one of each type is enough to find out about the types + $shares = $this->shareManager->getSharesBy( + $userId, + $requestedShareType, + $node, + false, + 1 + ); + if (!empty($shares)) { + $shareTypes[] = $requestedShareType; + } + } + return $shareTypes; + } + + /** + * Change the default sort mode + * + * @NoAdminRequired + * + * @param string $mode + * @param string $direction + * @return Response + */ + public function updateFileSorting($mode, $direction) { + $allowedMode = ['name', 'size', 'mtime']; + $allowedDirection = ['asc', 'desc']; + if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { + $response = new Response(); + $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); + return $response; + } + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); + return new Response(); + } + + /** + * Toggle default for showing/hiding hidden files + * + * @NoAdminRequired + * + * @param bool $show + */ + public function showHiddenFiles($show) { + $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int) $show); + return new Response(); + } + +} diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php new file mode 100644 index 00000000000..7539a3ab649 --- /dev/null +++ b/apps/files/lib/Controller/ViewController.php @@ -0,0 +1,304 @@ + + * @author Lukas Reschke + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Controller; + +use OC\AppFramework\Http\Request; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\INavigationManager; +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\IUserSession; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\AppFramework\Http\NotFoundResponse; +use OCP\Files\Folder; +use OCP\App\IAppManager; + +/** + * Class ViewController + * + * @package OCA\Files\Controller + */ +class ViewController extends Controller { + /** @var string */ + protected $appName; + /** @var IRequest */ + protected $request; + /** @var IURLGenerator */ + protected $urlGenerator; + /** @var INavigationManager */ + protected $navigationManager; + /** @var IL10N */ + protected $l10n; + /** @var IConfig */ + protected $config; + /** @var EventDispatcherInterface */ + protected $eventDispatcher; + /** @var IUserSession */ + protected $userSession; + /** @var IAppManager */ + protected $appManager; + /** @var \OCP\Files\Folder */ + protected $rootFolder; + + /** + * @param string $appName + * @param IRequest $request + * @param IURLGenerator $urlGenerator + * @param INavigationManager $navigationManager + * @param IL10N $l10n + * @param IConfig $config + * @param EventDispatcherInterface $eventDispatcherInterface + * @param IUserSession $userSession + * @param IAppManager $appManager + * @param Folder $rootFolder + */ + public function __construct($appName, + IRequest $request, + IURLGenerator $urlGenerator, + INavigationManager $navigationManager, + IL10N $l10n, + IConfig $config, + EventDispatcherInterface $eventDispatcherInterface, + IUserSession $userSession, + IAppManager $appManager, + Folder $rootFolder + ) { + parent::__construct($appName, $request); + $this->appName = $appName; + $this->request = $request; + $this->urlGenerator = $urlGenerator; + $this->navigationManager = $navigationManager; + $this->l10n = $l10n; + $this->config = $config; + $this->eventDispatcher = $eventDispatcherInterface; + $this->userSession = $userSession; + $this->appManager = $appManager; + $this->rootFolder = $rootFolder; + } + + /** + * @param string $appName + * @param string $scriptName + * @return string + */ + protected function renderScript($appName, $scriptName) { + $content = ''; + $appPath = \OC_App::getAppPath($appName); + $scriptPath = $appPath . '/' . $scriptName; + if (file_exists($scriptPath)) { + // TODO: sanitize path / script name ? + ob_start(); + include $scriptPath; + $content = ob_get_contents(); + @ob_end_clean(); + } + return $content; + } + + /** + * FIXME: Replace with non static code + * + * @return array + * @throws \OCP\Files\NotFoundException + */ + protected function getStorageInfo() { + $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); + return \OC_Helper::getStorageInfo('/', $dirInfo); + } + + /** + * @NoCSRFRequired + * @NoAdminRequired + * + * @param string $dir + * @param string $view + * @param string $fileid + * @return TemplateResponse + * @throws \OCP\Files\NotFoundException + */ + public function index($dir = '', $view = '', $fileid = null) { + if ($fileid !== null) { + return $this->showFile($fileid); + } + + $nav = new \OCP\Template('files', 'appnavigation', ''); + + // Load the files we need + \OCP\Util::addStyle('files', 'files'); + \OCP\Util::addStyle('files', 'upload'); + \OCP\Util::addStyle('files', 'mobile'); + \OCP\Util::addscript('files', 'app'); + \OCP\Util::addscript('files', 'file-upload'); + \OCP\Util::addscript('files', 'newfilemenu'); + \OCP\Util::addscript('files', 'jquery.fileupload'); + \OCP\Util::addscript('files', 'jquery-visibility'); + \OCP\Util::addscript('files', 'fileinfomodel'); + \OCP\Util::addscript('files', 'filesummary'); + \OCP\Util::addscript('files', 'breadcrumb'); + \OCP\Util::addscript('files', 'filelist'); + \OCP\Util::addscript('files', 'search'); + + \OCP\Util::addScript('files', 'favoritesfilelist'); + \OCP\Util::addScript('files', 'tagsplugin'); + \OCP\Util::addScript('files', 'favoritesplugin'); + + \OCP\Util::addScript('files', 'detailfileinfoview'); + \OCP\Util::addScript('files', 'detailtabview'); + \OCP\Util::addScript('files', 'mainfileinfodetailview'); + \OCP\Util::addScript('files', 'detailsview'); + \OCP\Util::addStyle('files', 'detailsView'); + + \OC_Util::addVendorScript('core', 'handlebars/handlebars'); + + \OCP\Util::addscript('files', 'fileactions'); + \OCP\Util::addscript('files', 'fileactionsmenu'); + \OCP\Util::addscript('files', 'files'); + \OCP\Util::addscript('files', 'keyboardshortcuts'); + \OCP\Util::addscript('files', 'navigation'); + + // if IE8 and "?dir=path&view=someview" was specified, reformat the URL to use a hash like "#?dir=path&view=someview" + $isIE8 = $this->request->isUserAgent([Request::USER_AGENT_IE_8]); + if ($isIE8 && ($dir !== '' || $view !== '')) { + $dir = !empty($dir) ? $dir : '/'; + $view = !empty($view) ? $view : 'files'; + $hash = '#?dir=' . \OCP\Util::encodePath($dir); + if ($view !== 'files') { + $hash .= '&view=' . urlencode($view); + } + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index') . $hash); + } + + // mostly for the home storage's free space + // FIXME: Make non static + $storageInfo = $this->getStorageInfo(); + + \OCA\Files\App::getNavigationManager()->add( + [ + 'id' => 'favorites', + 'appname' => 'files', + 'script' => 'simplelist.php', + 'order' => 5, + 'name' => $this->l10n->t('Favorites') + ] + ); + + $navItems = \OCA\Files\App::getNavigationManager()->getAll(); + usort($navItems, function($item1, $item2) { + return $item1['order'] - $item2['order']; + }); + $nav->assign('navigationItems', $navItems); + + $contentItems = []; + + // render the container content for every navigation item + foreach ($navItems as $item) { + $content = ''; + if (isset($item['script'])) { + $content = $this->renderScript($item['appname'], $item['script']); + } + $contentItem = []; + $contentItem['id'] = $item['id']; + $contentItem['content'] = $content; + $contentItems[] = $contentItem; + } + + $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts'); + + $params = []; + $params['usedSpacePercent'] = (int)$storageInfo['relative']; + $params['owner'] = $storageInfo['owner']; + $params['ownerDisplayName'] = $storageInfo['ownerDisplayName']; + $params['isPublic'] = false; + $params['mailNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no'); + $params['mailPublicNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no'); + $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'); + $user = $this->userSession->getUser()->getUID(); + $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); + $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); + $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); + $params['showHiddenFiles'] = $showHidden ? 1 : 0; + $params['appNavigation'] = $nav; + $params['appContents'] = $contentItems; + $this->navigationManager->setActiveEntry('files_index'); + + $response = new TemplateResponse( + $this->appName, + 'index', + $params + ); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedFrameDomain('\'self\''); + $response->setContentSecurityPolicy($policy); + + return $response; + } + + /** + * Redirects to the file list and highlight the given file id + * + * @param string $fileId file id to show + * @return Response redirect response or not found response + * + * @NoCSRFRequired + * @NoAdminRequired + */ + public function showFile($fileId) { + try { + $uid = $this->userSession->getUser()->getUID(); + $baseFolder = $this->rootFolder->get($uid . '/files/'); + $files = $baseFolder->getById($fileId); + $params = []; + + if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { + $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); + $files = $baseFolder->getById($fileId); + $params['view'] = 'trashbin'; + } + + if (!empty($files)) { + $file = current($files); + if ($file instanceof Folder) { + // set the full path to enter the folder + $params['dir'] = $baseFolder->getRelativePath($file->getPath()); + } else { + // set parent path as dir + $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); + // and scroll to the entry + $params['scrollto'] = $file->getName(); + } + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); + } + } catch (\OCP\Files\NotFoundException $e) { + return new NotFoundResponse(); + } + return new NotFoundResponse(); + } +} diff --git a/apps/files/lib/Helper.php b/apps/files/lib/Helper.php new file mode 100644 index 00000000000..d21a65afcee --- /dev/null +++ b/apps/files/lib/Helper.php @@ -0,0 +1,248 @@ + + * @author brumsel + * @author Jörn Friedrich Dreyer + * @author Lukas Reschke + * @author Morris Jobke + * @author Robin Appelman + * @author Robin McCorkell + * @author Roeland Jago Douma + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files; + +use OCP\Files\FileInfo; + +/** + * Helper class for manipulating file information + */ +class Helper { + /** + * @param string $dir + * @return array + * @throws \OCP\Files\NotFoundException + */ + public static function buildFileStorageStatistics($dir) { + // information about storage capacities + $storageInfo = \OC_Helper::getStorageInfo($dir); + $l = new \OC_L10N('files'); + $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']); + $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize); + $maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize)); + + return [ + 'uploadMaxFilesize' => $maxUploadFileSize, + 'maxHumanFilesize' => $maxHumanFileSize, + 'freeSpace' => $storageInfo['free'], + 'usedSpacePercent' => (int)$storageInfo['relative'], + 'owner' => $storageInfo['owner'], + 'ownerDisplayName' => $storageInfo['ownerDisplayName'], + ]; + } + + /** + * Determine icon for a given file + * + * @param \OCP\Files\FileInfo $file file info + * @return string icon URL + */ + public static function determineIcon($file) { + if($file['type'] === 'dir') { + $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir'); + // TODO: move this part to the client side, using mountType + if ($file->isShared()) { + $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared'); + } elseif ($file->isMounted()) { + $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external'); + } + }else{ + $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype()); + } + + return substr($icon, 0, -3) . 'svg'; + } + + /** + * Comparator function to sort files alphabetically and have + * the directories appear first + * + * @param \OCP\Files\FileInfo $a file + * @param \OCP\Files\FileInfo $b file + * @return int -1 if $a must come before $b, 1 otherwise + */ + public static function compareFileNames(FileInfo $a, FileInfo $b) { + $aType = $a->getType(); + $bType = $b->getType(); + if ($aType === 'dir' and $bType !== 'dir') { + return -1; + } elseif ($aType !== 'dir' and $bType === 'dir') { + return 1; + } else { + return \OCP\Util::naturalSortCompare($a->getName(), $b->getName()); + } + } + + /** + * Comparator function to sort files by date + * + * @param \OCP\Files\FileInfo $a file + * @param \OCP\Files\FileInfo $b file + * @return int -1 if $a must come before $b, 1 otherwise + */ + public static function compareTimestamp(FileInfo $a, FileInfo $b) { + $aTime = $a->getMTime(); + $bTime = $b->getMTime(); + return ($aTime < $bTime) ? -1 : 1; + } + + /** + * Comparator function to sort files by size + * + * @param \OCP\Files\FileInfo $a file + * @param \OCP\Files\FileInfo $b file + * @return int -1 if $a must come before $b, 1 otherwise + */ + public static function compareSize(FileInfo $a, FileInfo $b) { + $aSize = $a->getSize(); + $bSize = $b->getSize(); + return ($aSize < $bSize) ? -1 : 1; + } + + /** + * Formats the file info to be returned as JSON to the client. + * + * @param \OCP\Files\FileInfo $i + * @return array formatted file info + */ + public static function formatFileInfo(FileInfo $i) { + $entry = array(); + + $entry['id'] = $i['fileid']; + $entry['parentId'] = $i['parent']; + $entry['mtime'] = $i['mtime'] * 1000; + // only pick out the needed attributes + $entry['name'] = $i->getName(); + $entry['permissions'] = $i['permissions']; + $entry['mimetype'] = $i['mimetype']; + $entry['size'] = $i['size']; + $entry['type'] = $i['type']; + $entry['etag'] = $i['etag']; + if (isset($i['tags'])) { + $entry['tags'] = $i['tags']; + } + if (isset($i['displayname_owner'])) { + $entry['shareOwner'] = $i['displayname_owner']; + } + if (isset($i['is_share_mount_point'])) { + $entry['isShareMountPoint'] = $i['is_share_mount_point']; + } + $mountType = null; + if ($i->isShared()) { + $mountType = 'shared'; + } else if ($i->isMounted()) { + $mountType = 'external'; + } + if ($mountType !== null) { + if ($i->getInternalPath() === '') { + $mountType .= '-root'; + } + $entry['mountType'] = $mountType; + } + if (isset($i['extraData'])) { + $entry['extraData'] = $i['extraData']; + } + return $entry; + } + + /** + * Format file info for JSON + * @param \OCP\Files\FileInfo[] $fileInfos file infos + * @return array + */ + public static function formatFileInfos($fileInfos) { + $files = array(); + foreach ($fileInfos as $i) { + $files[] = self::formatFileInfo($i); + } + + return $files; + } + + /** + * Retrieves the contents of the given directory and + * returns it as a sorted array of FileInfo. + * + * @param string $dir path to the directory + * @param string $sortAttribute attribute to sort on + * @param bool $sortDescending true for descending sort, false otherwise + * @param string $mimetypeFilter limit returned content to this mimetype or mimepart + * @return \OCP\Files\FileInfo[] files + */ + public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') { + $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter); + + return self::sortFiles($content, $sortAttribute, $sortDescending); + } + + /** + * Populate the result set with file tags + * + * @param array $fileList + * @return array file list populated with tags + */ + public static function populateTags(array $fileList) { + $filesById = array(); + foreach ($fileList as $fileData) { + $filesById[$fileData['fileid']] = $fileData; + } + $tagger = \OC::$server->getTagManager()->load('files'); + $tags = $tagger->getTagsForObjects(array_keys($filesById)); + if ($tags) { + foreach ($tags as $fileId => $fileTags) { + $filesById[$fileId]['tags'] = $fileTags; + } + } + return $fileList; + } + + /** + * Sort the given file info array + * + * @param \OCP\Files\FileInfo[] $files files to sort + * @param string $sortAttribute attribute to sort on + * @param bool $sortDescending true for descending sort, false otherwise + * @return \OCP\Files\FileInfo[] sorted files + */ + public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) { + $sortFunc = 'compareFileNames'; + if ($sortAttribute === 'mtime') { + $sortFunc = 'compareTimestamp'; + } else if ($sortAttribute === 'size') { + $sortFunc = 'compareSize'; + } + usort($files, array('\OCA\Files\Helper', $sortFunc)); + if ($sortDescending) { + $files = array_reverse($files); + } + return $files; + } +} diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php new file mode 100644 index 00000000000..57cad43a539 --- /dev/null +++ b/apps/files/lib/Service/TagService.php @@ -0,0 +1,114 @@ + + * @author Morris Jobke + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Service; + +use OC\Files\FileInfo; +use OCP\Files\Node; + +/** + * Service class to manage tags on files. + */ +class TagService { + + /** + * @var \OCP\IUserSession + */ + private $userSession; + + /** + * @var \OCP\ITags + */ + private $tagger; + + /** + * @var \OCP\Files\Folder + */ + private $homeFolder; + + public function __construct( + \OCP\IUserSession $userSession, + \OCP\ITags $tagger, + \OCP\Files\Folder $homeFolder + ) { + $this->userSession = $userSession; + $this->tagger = $tagger; + $this->homeFolder = $homeFolder; + } + + /** + * Updates the tags of the specified file path. + * The passed tags are absolute, which means they will + * replace the actual tag selection. + * + * @param string $path path + * @param array $tags array of tags + * @return array list of tags + * @throws \OCP\Files\NotFoundException if the file does not exist + */ + public function updateFileTags($path, $tags) { + $fileId = $this->homeFolder->get($path)->getId(); + + $currentTags = $this->tagger->getTagsForObjects(array($fileId)); + + if (!empty($currentTags)) { + $currentTags = current($currentTags); + } + + $newTags = array_diff($tags, $currentTags); + foreach ($newTags as $tag) { + $this->tagger->tagAs($fileId, $tag); + } + $deletedTags = array_diff($currentTags, $tags); + foreach ($deletedTags as $tag) { + $this->tagger->unTag($fileId, $tag); + } + + // TODO: re-read from tagger to make sure the + // list is up to date, in case of concurrent changes ? + return $tags; + } + + /** + * Get all files for the given tag + * + * @param string $tagName tag name to filter by + * @return Node[] list of matching files + * @throws \Exception if the tag does not exist + */ + public function getFilesByTag($tagName) { + try { + $fileIds = $this->tagger->getIdsForTag($tagName); + } catch (\Exception $e) { + return []; + } + + $allNodes = []; + foreach ($fileIds as $fileId) { + $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); + } + return $allNodes; + } +} + diff --git a/apps/files/lib/activity.php b/apps/files/lib/activity.php deleted file mode 100644 index 1cbd6c3b973..00000000000 --- a/apps/files/lib/activity.php +++ /dev/null @@ -1,424 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files; - -use OCP\IDBConnection; -use OCP\L10N\IFactory; -use OCP\Activity\IExtension; -use OCP\Activity\IManager; -use OCP\IConfig; -use OCP\IL10N; -use OCP\IURLGenerator; - -class Activity implements IExtension { - const APP_FILES = 'files'; - const FILTER_FILES = 'files'; - const FILTER_FAVORITES = 'files_favorites'; - - const TYPE_SHARE_CREATED = 'file_created'; - const TYPE_SHARE_CHANGED = 'file_changed'; - const TYPE_SHARE_DELETED = 'file_deleted'; - const TYPE_SHARE_RESTORED = 'file_restored'; - const TYPE_FAVORITES = 'files_favorites'; - - /** @var IL10N */ - protected $l; - - /** @var IFactory */ - protected $languageFactory; - - /** @var IURLGenerator */ - protected $URLGenerator; - - /** @var \OCP\Activity\IManager */ - protected $activityManager; - - /** @var \OCP\IDBConnection */ - protected $connection; - - /** @var \OCP\IConfig */ - protected $config; - - /** @var \OCA\Files\ActivityHelper */ - protected $helper; - - /** - * @param IFactory $languageFactory - * @param IURLGenerator $URLGenerator - * @param IManager $activityManager - * @param ActivityHelper $helper - * @param IDBConnection $connection - * @param IConfig $config - */ - public function __construct(IFactory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ActivityHelper $helper, IDBConnection $connection, IConfig $config) { - $this->languageFactory = $languageFactory; - $this->URLGenerator = $URLGenerator; - $this->l = $this->getL10N(); - $this->activityManager = $activityManager; - $this->helper = $helper; - $this->connection = $connection; - $this->config = $config; - } - - /** - * @param string|null $languageCode - * @return IL10N - */ - protected function getL10N($languageCode = null) { - return $this->languageFactory->get(self::APP_FILES, $languageCode); - } - - /** - * The extension can return an array of additional notification types. - * If no additional types are to be added false is to be returned - * - * @param string $languageCode - * @return array|false Array "stringID of the type" => "translated string description for the setting" - * or Array "stringID of the type" => [ - * 'desc' => "translated string description for the setting" - * 'methods' => [self::METHOD_*], - * ] - */ - public function getNotificationTypes($languageCode) { - $l = $this->getL10N($languageCode); - return [ - self::TYPE_SHARE_CREATED => (string) $l->t('A new file or folder has been created'), - self::TYPE_SHARE_CHANGED => (string) $l->t('A file or folder has been changed'), - self::TYPE_FAVORITES => [ - 'desc' => (string) $l->t('Limit notifications about creation and changes to your favorite files (Stream only)'), - 'methods' => [self::METHOD_STREAM], - ], - self::TYPE_SHARE_DELETED => (string) $l->t('A file or folder has been deleted'), - self::TYPE_SHARE_RESTORED => (string) $l->t('A file or folder has been restored'), - ]; - } - - /** - * For a given method additional types to be displayed in the settings can be returned. - * In case no additional types are to be added false is to be returned. - * - * @param string $method - * @return array|false - */ - public function getDefaultTypes($method) { - if ($method === self::METHOD_STREAM) { - $settings = array(); - $settings[] = self::TYPE_SHARE_CREATED; - $settings[] = self::TYPE_SHARE_CHANGED; - $settings[] = self::TYPE_SHARE_DELETED; - $settings[] = self::TYPE_SHARE_RESTORED; - return $settings; - } - - return false; - } - - /** - * The extension can translate a given message to the requested languages. - * If no translation is available false is to be returned. - * - * @param string $app - * @param string $text - * @param array $params - * @param boolean $stripPath - * @param boolean $highlightParams - * @param string $languageCode - * @return string|false - */ - public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { - if ($app !== self::APP_FILES) { - return false; - } - - $l = $this->getL10N($languageCode); - - if ($this->activityManager->isFormattingFilteredObject()) { - $translation = $this->translateShort($text, $l, $params); - if ($translation !== false) { - return $translation; - } - } - - return $this->translateLong($text, $l, $params); - } - - /** - * @param string $text - * @param IL10N $l - * @param array $params - * @return string|false - */ - protected function translateLong($text, IL10N $l, array $params) { - switch ($text) { - case 'created_self': - return (string) $l->t('You created %1$s', $params); - case 'created_by': - return (string) $l->t('%2$s created %1$s', $params); - case 'created_public': - return (string) $l->t('%1$s was created in a public folder', $params); - case 'changed_self': - return (string) $l->t('You changed %1$s', $params); - case 'changed_by': - return (string) $l->t('%2$s changed %1$s', $params); - case 'deleted_self': - return (string) $l->t('You deleted %1$s', $params); - case 'deleted_by': - return (string) $l->t('%2$s deleted %1$s', $params); - case 'restored_self': - return (string) $l->t('You restored %1$s', $params); - case 'restored_by': - return (string) $l->t('%2$s restored %1$s', $params); - - default: - return false; - } - } - - /** - * @param string $text - * @param IL10N $l - * @param array $params - * @return string|false - */ - protected function translateShort($text, IL10N $l, array $params) { - switch ($text) { - case 'changed_by': - return (string) $l->t('Changed by %2$s', $params); - case 'deleted_by': - return (string) $l->t('Deleted by %2$s', $params); - case 'restored_by': - return (string) $l->t('Restored by %2$s', $params); - - default: - return false; - } - } - - /** - * The extension can define the type of parameters for translation - * - * Currently known types are: - * * file => will strip away the path of the file and add a tooltip with it - * * username => will add the avatar of the user - * - * @param string $app - * @param string $text - * @return array|false - */ - function getSpecialParameterList($app, $text) { - if ($app === self::APP_FILES) { - switch ($text) { - case 'created_self': - case 'created_by': - case 'created_public': - case 'changed_self': - case 'changed_by': - case 'deleted_self': - case 'deleted_by': - case 'restored_self': - case 'restored_by': - return [ - 0 => 'file', - 1 => 'username', - ]; - } - } - - return false; - } - - /** - * A string naming the css class for the icon to be used can be returned. - * If no icon is known for the given type false is to be returned. - * - * @param string $type - * @return string|false - */ - public function getTypeIcon($type) { - switch ($type) { - case self::TYPE_SHARE_CHANGED: - return 'icon-change'; - case self::TYPE_SHARE_CREATED: - return 'icon-add-color'; - case self::TYPE_SHARE_DELETED: - return 'icon-delete-color'; - - default: - return false; - } - } - - /** - * The extension can define the parameter grouping by returning the index as integer. - * In case no grouping is required false is to be returned. - * - * @param array $activity - * @return integer|false - */ - public function getGroupParameter($activity) { - if ($activity['app'] === self::APP_FILES) { - switch ($activity['subject']) { - case 'created_self': - case 'created_by': - case 'changed_self': - case 'changed_by': - case 'deleted_self': - case 'deleted_by': - case 'restored_self': - case 'restored_by': - return 0; - } - } - - return false; - } - - /** - * The extension can define additional navigation entries. The array returned has to contain two keys 'top' - * and 'apps' which hold arrays with the relevant entries. - * If no further entries are to be added false is no be returned. - * - * @return array|false - */ - public function getNavigation() { - return [ - 'top' => [ - self::FILTER_FAVORITES => [ - 'id' => self::FILTER_FAVORITES, - 'name' => (string) $this->l->t('Favorites'), - 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FAVORITES]), - ], - ], - 'apps' => [ - self::FILTER_FILES => [ - 'id' => self::FILTER_FILES, - 'name' => (string) $this->l->t('Files'), - 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FILES]), - ], - ], - ]; - } - - /** - * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not. - * - * @param string $filterValue - * @return boolean - */ - public function isFilterValid($filterValue) { - return $filterValue === self::FILTER_FILES || $filterValue === self::FILTER_FAVORITES; - } - - /** - * The extension can filter the types based on the filter if required. - * In case no filter is to be applied false is to be returned unchanged. - * - * @param array $types - * @param string $filter - * @return array|false - */ - public function filterNotificationTypes($types, $filter) { - if ($filter === self::FILTER_FILES || $filter === self::FILTER_FAVORITES) { - return array_intersect([ - self::TYPE_SHARE_CREATED, - self::TYPE_SHARE_CHANGED, - self::TYPE_SHARE_DELETED, - self::TYPE_SHARE_RESTORED, - ], $types); - } - return false; - } - - /** - * For a given filter the extension can specify the sql query conditions including parameters for that query. - * In case the extension does not know the filter false is to be returned. - * The query condition and the parameters are to be returned as array with two elements. - * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%')); - * - * @param string $filter - * @return array|false - */ - public function getQueryForFilter($filter) { - $user = $this->activityManager->getCurrentUserId(); - // Display actions from all files - if ($filter === self::FILTER_FILES) { - return ['`app` = ?', [self::APP_FILES]]; - } - - if (!$user) { - // Remaining filters only work with a user/token - return false; - } - - // Display actions from favorites only - if ($filter === self::FILTER_FAVORITES || in_array($filter, ['all', 'by', 'self']) && $this->userSettingFavoritesOnly($user)) { - try { - $favorites = $this->helper->getFavoriteFilePaths($user); - } catch (\RuntimeException $e) { - // Too many favorites, can not put them into one query anymore... - return ['`app` = ?', [self::APP_FILES]]; - } - - /* - * Display activities only, when they are not `type` create/change - * or `file` is a favorite or in a favorite folder - */ - $parameters = $fileQueryList = []; - $parameters[] = self::APP_FILES; - $parameters[] = self::APP_FILES; - - $fileQueryList[] = '(`type` <> ? AND `type` <> ?)'; - $parameters[] = self::TYPE_SHARE_CREATED; - $parameters[] = self::TYPE_SHARE_CHANGED; - - foreach ($favorites['items'] as $favorite) { - $fileQueryList[] = '`file` = ?'; - $parameters[] = $favorite; - } - foreach ($favorites['folders'] as $favorite) { - $fileQueryList[] = '`file` LIKE ?'; - $parameters[] = $this->connection->escapeLikeParameter($favorite) . '/%'; - } - - return [ - ' CASE ' - . 'WHEN `app` <> ? THEN 1 ' - . 'WHEN `app` = ? AND (' . implode(' OR ', $fileQueryList) . ') THEN 1 ' - . 'ELSE 0 ' - . 'END = 1 ', - $parameters, - ]; - } - return false; - } - - /** - * Is the file actions favorite limitation enabled? - * - * @param string $user - * @return bool - */ - protected function userSettingFavoritesOnly($user) { - return (bool) $this->config->getUserValue($user, 'activity', 'notify_' . self::METHOD_STREAM . '_' . self::TYPE_FAVORITES, false); - } -} diff --git a/apps/files/lib/activityhelper.php b/apps/files/lib/activityhelper.php deleted file mode 100644 index 046dd59bc76..00000000000 --- a/apps/files/lib/activityhelper.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files; - -use OCP\Files\Folder; -use OCP\ITagManager; - -class ActivityHelper { - /** If a user has a lot of favorites the query might get too slow and long */ - const FAVORITE_LIMIT = 50; - - /** @var \OCP\ITagManager */ - protected $tagManager; - - /** - * @param ITagManager $tagManager - */ - public function __construct(ITagManager $tagManager) { - $this->tagManager = $tagManager; - } - - /** - * Returns an array with the favorites - * - * @param string $user - * @return array - * @throws \RuntimeException when too many or no favorites where found - */ - public function getFavoriteFilePaths($user) { - $tags = $this->tagManager->load('files', [], false, $user); - $favorites = $tags->getFavorites(); - - if (empty($favorites)) { - throw new \RuntimeException('No favorites', 1); - } else if (isset($favorites[self::FAVORITE_LIMIT])) { - throw new \RuntimeException('Too many favorites', 2); - } - - // Can not DI because the user is not known on instantiation - $rootFolder = \OC::$server->getUserFolder($user); - $folders = $items = []; - foreach ($favorites as $favorite) { - $nodes = $rootFolder->getById($favorite); - if (!empty($nodes)) { - /** @var \OCP\Files\Node $node */ - $node = array_shift($nodes); - $path = substr($node->getPath(), strlen($user . '/files/')); - - $items[] = $path; - if ($node instanceof Folder) { - $folders[] = $path; - } - } - } - - if (empty($items)) { - throw new \RuntimeException('No favorites', 1); - } - - return [ - 'items' => $items, - 'folders' => $folders, - ]; - } -} diff --git a/apps/files/lib/app.php b/apps/files/lib/app.php deleted file mode 100644 index 981c41ff413..00000000000 --- a/apps/files/lib/app.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @author Morris Jobke - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - - -namespace OCA\Files; - -class App { - /** - * @var \OCP\INavigationManager - */ - private static $navigationManager; - - /** - * Returns the app's navigation manager - * - * @return \OCP\INavigationManager - */ - public static function getNavigationManager() { - // TODO: move this into a service in the Application class - if (self::$navigationManager === null) { - self::$navigationManager = new \OC\NavigationManager(); - } - return self::$navigationManager; - } - -} diff --git a/apps/files/lib/backgroundjob/cleanupfilelocks.php b/apps/files/lib/backgroundjob/cleanupfilelocks.php deleted file mode 100644 index b5cf8e94551..00000000000 --- a/apps/files/lib/backgroundjob/cleanupfilelocks.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\BackgroundJob; - -use OC\BackgroundJob\TimedJob; -use OC\Lock\DBLockingProvider; - -/** - * Clean up all file locks that are expired for the DB file locking provider - */ -class CleanupFileLocks extends TimedJob { - - /** - * Default interval in minutes - * - * @var int $defaultIntervalMin - **/ - protected $defaultIntervalMin = 5; - - /** - * sets the correct interval for this timed job - */ - public function __construct() { - $this->interval = $this->defaultIntervalMin * 60; - } - - /** - * Makes the background job do its work - * - * @param array $argument unused argument - */ - public function run($argument) { - $lockingProvider = \OC::$server->getLockingProvider(); - if($lockingProvider instanceof DBLockingProvider) { - $lockingProvider->cleanExpiredLocks(); - } - } -} diff --git a/apps/files/lib/backgroundjob/deleteorphaneditems.php b/apps/files/lib/backgroundjob/deleteorphaneditems.php deleted file mode 100644 index 1eef9c24e0c..00000000000 --- a/apps/files/lib/backgroundjob/deleteorphaneditems.php +++ /dev/null @@ -1,153 +0,0 @@ - - * @author Joas Schilling - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\BackgroundJob; - -use OC\BackgroundJob\TimedJob; -use OCP\DB\QueryBuilder\IQueryBuilder; - -/** - * Delete all share entries that have no matching entries in the file cache table. - */ -class DeleteOrphanedItems extends TimedJob { - - const CHUNK_SIZE = 200; - - /** @var \OCP\IDBConnection */ - protected $connection; - - /** @var \OCP\ILogger */ - protected $logger; - - /** - * Default interval in minutes - * - * @var int $defaultIntervalMin - **/ - protected $defaultIntervalMin = 60; - - /** - * sets the correct interval for this timed job - */ - public function __construct() { - $this->interval = $this->defaultIntervalMin * 60; - $this->connection = \OC::$server->getDatabaseConnection(); - $this->logger = \OC::$server->getLogger(); - } - - /** - * Makes the background job do its work - * - * @param array $argument unused argument - */ - public function run($argument) { - $this->cleanSystemTags(); - $this->cleanUserTags(); - $this->cleanComments(); - $this->cleanCommentMarkers(); - } - - /** - * Deleting orphaned system tag mappings - * - * @param string $table - * @param string $idCol - * @param string $typeCol - * @return int Number of deleted entries - */ - protected function cleanUp($table, $idCol, $typeCol) { - $deletedEntries = 0; - - $query = $this->connection->getQueryBuilder(); - $query->select('t1.' . $idCol) - ->from($table, 't1') - ->where($query->expr()->eq($typeCol, $query->expr()->literal('files'))) - ->andWhere($query->expr()->isNull('t2.fileid')) - ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid')) - ->groupBy('t1.' . $idCol) - ->setMaxResults(self::CHUNK_SIZE); - - $deleteQuery = $this->connection->getQueryBuilder(); - $deleteQuery->delete($table) - ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid'))); - - $deletedInLastChunk = self::CHUNK_SIZE; - while ($deletedInLastChunk === self::CHUNK_SIZE) { - $result = $query->execute(); - $deletedInLastChunk = 0; - while ($row = $result->fetch()) { - $deletedInLastChunk++; - $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row[$idCol]) - ->execute(); - } - $result->closeCursor(); - } - - return $deletedEntries; - } - - /** - * Deleting orphaned system tag mappings - * - * @return int Number of deleted entries - */ - protected function cleanSystemTags() { - $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype'); - $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']); - return $deletedEntries; - } - - /** - * Deleting orphaned user tag mappings - * - * @return int Number of deleted entries - */ - protected function cleanUserTags() { - $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type'); - $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']); - return $deletedEntries; - } - - /** - * Deleting orphaned comments - * - * @return int Number of deleted entries - */ - protected function cleanComments() { - $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type'); - $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']); - return $deletedEntries; - } - - /** - * Deleting orphaned comment read markers - * - * @return int Number of deleted entries - */ - protected function cleanCommentMarkers() { - $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type'); - $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']); - return $deletedEntries; - } - -} diff --git a/apps/files/lib/backgroundjob/scanfiles.php b/apps/files/lib/backgroundjob/scanfiles.php deleted file mode 100644 index dcc180bcfbe..00000000000 --- a/apps/files/lib/backgroundjob/scanfiles.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\BackgroundJob; - -use OC\Files\Utils\Scanner; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\ILogger; -use OCP\IUser; -use OCP\IUserManager; - -/** - * Class ScanFiles is a background job used to run the file scanner over the user - * accounts to ensure integrity of the file cache. - * - * @package OCA\Files\BackgroundJob - */ -class ScanFiles extends \OC\BackgroundJob\TimedJob { - /** @var IConfig */ - private $config; - /** @var IUserManager */ - private $userManager; - /** @var IDBConnection */ - private $dbConnection; - /** @var ILogger */ - private $logger; - /** Amount of users that should get scanned per execution */ - const USERS_PER_SESSION = 500; - - /** - * @param IConfig|null $config - * @param IUserManager|null $userManager - * @param IDBConnection|null $dbConnection - * @param ILogger|null $logger - */ - public function __construct(IConfig $config = null, - IUserManager $userManager = null, - IDBConnection $dbConnection = null, - ILogger $logger = null) { - // Run once per 10 minutes - $this->setInterval(60 * 10); - - if (is_null($userManager) || is_null($config)) { - $this->fixDIForJobs(); - } else { - $this->config = $config; - $this->userManager = $userManager; - $this->logger = $logger; - } - } - - protected function fixDIForJobs() { - $this->config = \OC::$server->getConfig(); - $this->userManager = \OC::$server->getUserManager(); - $this->logger = \OC::$server->getLogger(); - } - - /** - * @param IUser $user - */ - protected function runScanner(IUser $user) { - try { - $scanner = new Scanner( - $user->getUID(), - $this->dbConnection, - $this->logger - ); - $scanner->backgroundScan(''); - } catch (\Exception $e) { - $this->logger->logException($e, ['app' => 'files']); - } - \OC_Util::tearDownFS(); - } - - /** - * @param $argument - * @throws \Exception - */ - protected function run($argument) { - $offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0); - $users = $this->userManager->search('', self::USERS_PER_SESSION, $offset); - if (!count($users)) { - // No users found, reset offset and retry - $offset = 0; - $users = $this->userManager->search('', self::USERS_PER_SESSION); - } - - $offset += self::USERS_PER_SESSION; - $this->config->setAppValue('files', 'cronjob_scan_files', $offset); - - foreach ($users as $user) { - $this->runScanner($user); - } - } -} diff --git a/apps/files/lib/capabilities.php b/apps/files/lib/capabilities.php deleted file mode 100644 index dc49ca174b3..00000000000 --- a/apps/files/lib/capabilities.php +++ /dev/null @@ -1,60 +0,0 @@ - - * @author Roeland Jago Douma - * @author Tom Needham - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files; - -use OCP\Capabilities\ICapability; -use OCP\IConfig; - -/** - * Class Capabilities - * - * @package OCA\Files - */ -class Capabilities implements ICapability { - /** @var IConfig */ - protected $config; - - /** - * Capabilities constructor. - * - * @param IConfig $config - */ - public function __construct(IConfig $config) { - $this->config = $config; - } - - /** - * Return this classes capabilities - * - * @return array - */ - public function getCapabilities() { - return [ - 'files' => [ - 'bigfilechunking' => true, - 'blacklisted_files' => $this->config->getSystemValue('blacklisted_files', ['.htaccess']), - ], - ]; - } -} diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php deleted file mode 100644 index d21a65afcee..00000000000 --- a/apps/files/lib/helper.php +++ /dev/null @@ -1,248 +0,0 @@ - - * @author brumsel - * @author Jörn Friedrich Dreyer - * @author Lukas Reschke - * @author Morris Jobke - * @author Robin Appelman - * @author Robin McCorkell - * @author Roeland Jago Douma - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files; - -use OCP\Files\FileInfo; - -/** - * Helper class for manipulating file information - */ -class Helper { - /** - * @param string $dir - * @return array - * @throws \OCP\Files\NotFoundException - */ - public static function buildFileStorageStatistics($dir) { - // information about storage capacities - $storageInfo = \OC_Helper::getStorageInfo($dir); - $l = new \OC_L10N('files'); - $maxUploadFileSize = \OCP\Util::maxUploadFilesize($dir, $storageInfo['free']); - $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize); - $maxHumanFileSize = $l->t('Upload (max. %s)', array($maxHumanFileSize)); - - return [ - 'uploadMaxFilesize' => $maxUploadFileSize, - 'maxHumanFilesize' => $maxHumanFileSize, - 'freeSpace' => $storageInfo['free'], - 'usedSpacePercent' => (int)$storageInfo['relative'], - 'owner' => $storageInfo['owner'], - 'ownerDisplayName' => $storageInfo['ownerDisplayName'], - ]; - } - - /** - * Determine icon for a given file - * - * @param \OCP\Files\FileInfo $file file info - * @return string icon URL - */ - public static function determineIcon($file) { - if($file['type'] === 'dir') { - $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir'); - // TODO: move this part to the client side, using mountType - if ($file->isShared()) { - $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-shared'); - } elseif ($file->isMounted()) { - $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon('dir-external'); - } - }else{ - $icon = \OC::$server->getMimeTypeDetector()->mimeTypeIcon($file->getMimetype()); - } - - return substr($icon, 0, -3) . 'svg'; - } - - /** - * Comparator function to sort files alphabetically and have - * the directories appear first - * - * @param \OCP\Files\FileInfo $a file - * @param \OCP\Files\FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareFileNames(FileInfo $a, FileInfo $b) { - $aType = $a->getType(); - $bType = $b->getType(); - if ($aType === 'dir' and $bType !== 'dir') { - return -1; - } elseif ($aType !== 'dir' and $bType === 'dir') { - return 1; - } else { - return \OCP\Util::naturalSortCompare($a->getName(), $b->getName()); - } - } - - /** - * Comparator function to sort files by date - * - * @param \OCP\Files\FileInfo $a file - * @param \OCP\Files\FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareTimestamp(FileInfo $a, FileInfo $b) { - $aTime = $a->getMTime(); - $bTime = $b->getMTime(); - return ($aTime < $bTime) ? -1 : 1; - } - - /** - * Comparator function to sort files by size - * - * @param \OCP\Files\FileInfo $a file - * @param \OCP\Files\FileInfo $b file - * @return int -1 if $a must come before $b, 1 otherwise - */ - public static function compareSize(FileInfo $a, FileInfo $b) { - $aSize = $a->getSize(); - $bSize = $b->getSize(); - return ($aSize < $bSize) ? -1 : 1; - } - - /** - * Formats the file info to be returned as JSON to the client. - * - * @param \OCP\Files\FileInfo $i - * @return array formatted file info - */ - public static function formatFileInfo(FileInfo $i) { - $entry = array(); - - $entry['id'] = $i['fileid']; - $entry['parentId'] = $i['parent']; - $entry['mtime'] = $i['mtime'] * 1000; - // only pick out the needed attributes - $entry['name'] = $i->getName(); - $entry['permissions'] = $i['permissions']; - $entry['mimetype'] = $i['mimetype']; - $entry['size'] = $i['size']; - $entry['type'] = $i['type']; - $entry['etag'] = $i['etag']; - if (isset($i['tags'])) { - $entry['tags'] = $i['tags']; - } - if (isset($i['displayname_owner'])) { - $entry['shareOwner'] = $i['displayname_owner']; - } - if (isset($i['is_share_mount_point'])) { - $entry['isShareMountPoint'] = $i['is_share_mount_point']; - } - $mountType = null; - if ($i->isShared()) { - $mountType = 'shared'; - } else if ($i->isMounted()) { - $mountType = 'external'; - } - if ($mountType !== null) { - if ($i->getInternalPath() === '') { - $mountType .= '-root'; - } - $entry['mountType'] = $mountType; - } - if (isset($i['extraData'])) { - $entry['extraData'] = $i['extraData']; - } - return $entry; - } - - /** - * Format file info for JSON - * @param \OCP\Files\FileInfo[] $fileInfos file infos - * @return array - */ - public static function formatFileInfos($fileInfos) { - $files = array(); - foreach ($fileInfos as $i) { - $files[] = self::formatFileInfo($i); - } - - return $files; - } - - /** - * Retrieves the contents of the given directory and - * returns it as a sorted array of FileInfo. - * - * @param string $dir path to the directory - * @param string $sortAttribute attribute to sort on - * @param bool $sortDescending true for descending sort, false otherwise - * @param string $mimetypeFilter limit returned content to this mimetype or mimepart - * @return \OCP\Files\FileInfo[] files - */ - public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false, $mimetypeFilter = '') { - $content = \OC\Files\Filesystem::getDirectoryContent($dir, $mimetypeFilter); - - return self::sortFiles($content, $sortAttribute, $sortDescending); - } - - /** - * Populate the result set with file tags - * - * @param array $fileList - * @return array file list populated with tags - */ - public static function populateTags(array $fileList) { - $filesById = array(); - foreach ($fileList as $fileData) { - $filesById[$fileData['fileid']] = $fileData; - } - $tagger = \OC::$server->getTagManager()->load('files'); - $tags = $tagger->getTagsForObjects(array_keys($filesById)); - if ($tags) { - foreach ($tags as $fileId => $fileTags) { - $filesById[$fileId]['tags'] = $fileTags; - } - } - return $fileList; - } - - /** - * Sort the given file info array - * - * @param \OCP\Files\FileInfo[] $files files to sort - * @param string $sortAttribute attribute to sort on - * @param bool $sortDescending true for descending sort, false otherwise - * @return \OCP\Files\FileInfo[] sorted files - */ - public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) { - $sortFunc = 'compareFileNames'; - if ($sortAttribute === 'mtime') { - $sortFunc = 'compareTimestamp'; - } else if ($sortAttribute === 'size') { - $sortFunc = 'compareSize'; - } - usort($files, array('\OCA\Files\Helper', $sortFunc)); - if ($sortDescending) { - $files = array_reverse($files); - } - return $files; - } -} diff --git a/apps/files/service/tagservice.php b/apps/files/service/tagservice.php deleted file mode 100644 index 57cad43a539..00000000000 --- a/apps/files/service/tagservice.php +++ /dev/null @@ -1,114 +0,0 @@ - - * @author Morris Jobke - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Service; - -use OC\Files\FileInfo; -use OCP\Files\Node; - -/** - * Service class to manage tags on files. - */ -class TagService { - - /** - * @var \OCP\IUserSession - */ - private $userSession; - - /** - * @var \OCP\ITags - */ - private $tagger; - - /** - * @var \OCP\Files\Folder - */ - private $homeFolder; - - public function __construct( - \OCP\IUserSession $userSession, - \OCP\ITags $tagger, - \OCP\Files\Folder $homeFolder - ) { - $this->userSession = $userSession; - $this->tagger = $tagger; - $this->homeFolder = $homeFolder; - } - - /** - * Updates the tags of the specified file path. - * The passed tags are absolute, which means they will - * replace the actual tag selection. - * - * @param string $path path - * @param array $tags array of tags - * @return array list of tags - * @throws \OCP\Files\NotFoundException if the file does not exist - */ - public function updateFileTags($path, $tags) { - $fileId = $this->homeFolder->get($path)->getId(); - - $currentTags = $this->tagger->getTagsForObjects(array($fileId)); - - if (!empty($currentTags)) { - $currentTags = current($currentTags); - } - - $newTags = array_diff($tags, $currentTags); - foreach ($newTags as $tag) { - $this->tagger->tagAs($fileId, $tag); - } - $deletedTags = array_diff($currentTags, $tags); - foreach ($deletedTags as $tag) { - $this->tagger->unTag($fileId, $tag); - } - - // TODO: re-read from tagger to make sure the - // list is up to date, in case of concurrent changes ? - return $tags; - } - - /** - * Get all files for the given tag - * - * @param string $tagName tag name to filter by - * @return Node[] list of matching files - * @throws \Exception if the tag does not exist - */ - public function getFilesByTag($tagName) { - try { - $fileIds = $this->tagger->getIdsForTag($tagName); - } catch (\Exception $e) { - return []; - } - - $allNodes = []; - foreach ($fileIds as $fileId) { - $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); - } - return $allNodes; - } -} - diff --git a/apps/files/tests/ActivityTest.php b/apps/files/tests/ActivityTest.php new file mode 100644 index 00000000000..bc062dd0542 --- /dev/null +++ b/apps/files/tests/ActivityTest.php @@ -0,0 +1,375 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Tests; + +use OCA\Files\Activity; +use Test\TestCase; + +/** + * Class ActivityTest + * + * @group DB + * @package OCA\Files\Tests + */ +class ActivityTest extends TestCase { + + /** @var \OCP\Activity\IManager */ + private $activityManager; + + /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + protected $request; + + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $session; + + /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \OCA\Files\ActivityHelper|\PHPUnit_Framework_MockObject_MockObject */ + protected $activityHelper; + + /** @var \OCP\L10N\IFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $l10nFactory; + + /** @var \OCA\Files\Activity */ + protected $activityExtension; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->activityHelper = $this->getMockBuilder('OCA\Files\ActivityHelper') + ->disableOriginalConstructor() + ->getMock(); + + $this->activityManager = new \OC\Activity\Manager( + $this->request, + $this->session, + $this->config + ); + + $this->l10nFactory = $this->getMockBuilder('OCP\L10N\IFactory') + ->disableOriginalConstructor() + ->getMock(); + $deL10n = $this->getMockBuilder('OC_L10N') + ->disableOriginalConstructor() + ->getMock(); + $deL10n->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($argument) { + return 'translate(' . $argument . ')'; + }); + + $this->l10nFactory->expects($this->any()) + ->method('get') + ->willReturnMap([ + ['files', null, new \OC_L10N('files', 'en')], + ['files', 'en', new \OC_L10N('files', 'en')], + ['files', 'de', $deL10n], + ]); + + $this->activityExtension = $activityExtension = new Activity( + $this->l10nFactory, + $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), + $this->activityManager, + $this->activityHelper, + \OC::$server->getDatabaseConnection(), + $this->config + ); + + $this->activityManager->registerExtension(function() use ($activityExtension) { + return $activityExtension; + }); + } + + public function testNotificationTypes() { + $result = $this->activityExtension->getNotificationTypes('en'); + $this->assertTrue(is_array($result), 'Asserting getNotificationTypes() returns an array'); + $this->assertCount(5, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); + $this->assertArrayHasKey(Activity::TYPE_FAVORITES, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); + } + + public function testDefaultTypes() { + $result = $this->activityExtension->getDefaultTypes('stream'); + $this->assertTrue(is_array($result), 'Asserting getDefaultTypes(stream) returns an array'); + $this->assertCount(4, $result); + $result = array_flip($result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); + $this->assertArrayNotHasKey(Activity::TYPE_FAVORITES, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); + + $result = $this->activityExtension->getDefaultTypes('email'); + $this->assertFalse($result, 'Asserting getDefaultTypes(email) returns false'); + } + + public function testTranslate() { + $this->assertFalse( + $this->activityExtension->translate('files_sharing', '', [], false, false, 'en'), + 'Asserting that no translations are set for files_sharing' + ); + + // Test english + $this->assertNotFalse( + $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en'), + 'Asserting that translations are set for files.deleted_self' + ); + $this->assertStringStartsWith( + 'You deleted ', + $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en') + ); + + // Test translation + $this->assertNotFalse( + $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de'), + 'Asserting that translations are set for files.deleted_self' + ); + $this->assertStringStartsWith( + 'translate(You deleted ', + $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de') + ); + } + + public function testGetSpecialParameterList() { + $this->assertFalse( + $this->activityExtension->getSpecialParameterList('files_sharing', ''), + 'Asserting that no special parameters are set for files_sharing' + ); + } + + public function typeIconData() { + return [ + [Activity::TYPE_SHARE_CHANGED, 'icon-change'], + [Activity::TYPE_SHARE_CREATED, 'icon-add-color'], + [Activity::TYPE_SHARE_DELETED, 'icon-delete-color'], + [Activity::TYPE_SHARE_RESTORED, false], + [Activity::TYPE_FAVORITES, false], + ['unknown type', false], + ]; + } + + /** + * @dataProvider typeIconData + * + * @param string $type + * @param mixed $expected + */ + public function testTypeIcon($type, $expected) { + $this->assertSame($expected, $this->activityExtension->getTypeIcon($type)); + } + + public function testGroupParameter() { + $this->assertFalse( + $this->activityExtension->getGroupParameter(['app' => 'files_sharing']), + 'Asserting that no group parameters are set for files_sharing' + ); + } + + public function testNavigation() { + $result = $this->activityExtension->getNavigation(); + $this->assertCount(1, $result['top']); + $this->assertArrayHasKey(Activity::FILTER_FAVORITES, $result['top']); + + $this->assertCount(1, $result['apps']); + $this->assertArrayHasKey(Activity::FILTER_FILES, $result['apps']); + } + + public function testIsFilterValid() { + $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FAVORITES)); + $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FILES)); + $this->assertFalse($this->activityExtension->isFilterValid('unknown filter')); + } + + public function filterNotificationTypesData() { + return [ + [ + Activity::FILTER_FILES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], [ + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + ], + ], + [ + Activity::FILTER_FILES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_FAVORITES, + ], + [ + Activity::TYPE_SHARE_CREATED, + ], + ], + [ + Activity::FILTER_FAVORITES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], [ + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + ], + ], + [ + 'unknown filter', + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], + false, + ], + ]; + } + + /** + * @dataProvider filterNotificationTypesData + * + * @param string $filter + * @param array $types + * @param mixed $expected + */ + public function testFilterNotificationTypes($filter, $types, $expected) { + $result = $this->activityExtension->filterNotificationTypes($types, $filter); + $this->assertEquals($expected, $result); + } + + public function queryForFilterData() { + return [ + [ + new \RuntimeException(), + '`app` = ?', + ['files'] + ], + [ + [ + 'items' => [], + 'folders' => [], + ], + ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?)) THEN 1 ELSE 0 END = 1 ', + ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED] + ], + [ + [ + 'items' => ['file.txt', 'folder'], + 'folders' => ['folder'], + ], + ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?) OR `file` = ? OR `file` = ? OR `file` LIKE ?) THEN 1 ELSE 0 END = 1 ', + ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'file.txt', 'folder', 'folder/%'] + ], + ]; + } + + /** + * @dataProvider queryForFilterData + * + * @param mixed $will + * @param string $query + * @param array $parameters + */ + public function testQueryForFilter($will, $query, $parameters) { + $this->mockUserSession('test'); + + $this->config->expects($this->any()) + ->method('getUserValue') + ->willReturnMap([ + ['test', 'activity', 'notify_stream_' . Activity::TYPE_FAVORITES, false, true], + ]); + if (is_array($will)) { + $this->activityHelper->expects($this->any()) + ->method('getFavoriteFilePaths') + ->with('test') + ->willReturn($will); + } else { + $this->activityHelper->expects($this->any()) + ->method('getFavoriteFilePaths') + ->with('test') + ->willThrowException($will); + } + + $result = $this->activityExtension->getQueryForFilter('all'); + $this->assertEquals([$query, $parameters], $result); + + $this->executeQueryForFilter($result); + } + + public function executeQueryForFilter(array $result) { + list($resultQuery, $resultParameters) = $result; + $resultQuery = str_replace('`file`', '`user`', $resultQuery); + $resultQuery = str_replace('`type`', '`key`', $resultQuery); + + $connection = \OC::$server->getDatabaseConnection(); + // Test the query on the privatedata table, because the activity table + // does not exist in core + $result = $connection->executeQuery('SELECT * FROM `*PREFIX*privatedata` WHERE ' . $resultQuery, $resultParameters); + $rows = $result->fetchAll(); + $result->closeCursor(); + } + + protected function mockUserSession($user) { + $mockUser = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $mockUser->expects($this->any()) + ->method('getUID') + ->willReturn($user); + + $this->session->expects($this->any()) + ->method('isLoggedIn') + ->willReturn(true); + $this->session->expects($this->any()) + ->method('getUser') + ->willReturn($mockUser); + } +} diff --git a/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php new file mode 100644 index 00000000000..e802a248a9b --- /dev/null +++ b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php @@ -0,0 +1,259 @@ + + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Tests\BackgroundJob; + +use OCA\Files\BackgroundJob\DeleteOrphanedItems; +use OCP\DB\QueryBuilder\IQueryBuilder; + +/** + * Class DeleteOrphanedItemsJobTest + * + * @group DB + * + * @package Test\BackgroundJob + */ +class DeleteOrphanedItemsJobTest extends \Test\TestCase { + + /** @var \OCP\IDBConnection */ + protected $connection; + + protected function setup() { + parent::setUp(); + $this->connection = \OC::$server->getDatabaseConnection(); + } + + protected function cleanMapping($table) { + $query = $this->connection->getQueryBuilder(); + $query->delete($table)->execute(); + } + + protected function getMappings($table) { + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from($table); + $result = $query->execute(); + $mapping = $result->fetchAll(); + $result->closeCursor(); + + return $mapping; + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearSystemTagMappings() { + $this->cleanMapping('systemtag_object_mapping'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('systemtag_object_mapping') + ->values([ + 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'objecttype' => $query->createNamedParameter('files'), + 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('systemtag_object_mapping') + ->values([ + 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'objecttype' => $query->createNamedParameter('files'), + 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('systemtag_object_mapping'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanSystemTags'); + + $mapping = $this->getMappings('systemtag_object_mapping'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('systemtag_object_mapping'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearUserTagMappings() { + $this->cleanMapping('vcategory_to_object'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('vcategory_to_object') + ->values([ + 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'type' => $query->createNamedParameter('files'), + 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('vcategory_to_object') + ->values([ + 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'type' => $query->createNamedParameter('files'), + 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('vcategory_to_object'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanUserTags'); + + $mapping = $this->getMappings('vcategory_to_object'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('vcategory_to_object'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearComments() { + $this->cleanMapping('comments'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + $mapping = $this->getMappings('comments'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanComments'); + + $mapping = $this->getMappings('comments'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearCommentReadMarks() { + $this->cleanMapping('comments_read_markers'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanCommentMarkers'); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments_read_markers'); + } + +} diff --git a/apps/files/tests/BackgroundJob/ScanFilesTest.php b/apps/files/tests/BackgroundJob/ScanFilesTest.php new file mode 100644 index 00000000000..eab28071b70 --- /dev/null +++ b/apps/files/tests/BackgroundJob/ScanFilesTest.php @@ -0,0 +1,133 @@ + + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ +namespace OCA\Files\Tests\BackgroundJob; + +use Test\TestCase; +use OCP\IConfig; +use OCP\IUserManager; +use OCA\Files\BackgroundJob\ScanFiles; + +/** + * Class ScanFilesTest + * + * @package OCA\Files\Tests\BackgroundJob + */ +class ScanFilesTest extends TestCase { + /** @var IConfig */ + private $config; + /** @var IUserManager */ + private $userManager; + /** @var ScanFiles */ + private $scanFiles; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMock('\OCP\IConfig'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + + $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles') + ->setConstructorArgs([ + $this->config, + $this->userManager, + ]) + ->setMethods(['runScanner']) + ->getMock(); + } + + public function testRunWithoutUsers() { + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([])); + $this->userManager + ->expects($this->at(1)) + ->method('search') + ->with('', 500) + ->will($this->returnValue([])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 500); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + + public function testRunWithUsers() { + $fakeUser = $this->getMock('\OCP\IUser'); + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([ + $fakeUser + ])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 550); + $this->scanFiles + ->expects($this->once()) + ->method('runScanner') + ->with($fakeUser); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + + public function testRunWithUsersAndOffsetAtEndOfUserList() { + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([])); + $this->userManager + ->expects($this->at(1)) + ->method('search') + ->with('', 500) + ->will($this->returnValue([])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 500); + $this->scanFiles + ->expects($this->never()) + ->method('runScanner'); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + +} diff --git a/apps/files/tests/Command/DeleteOrphanedFilesTest.php b/apps/files/tests/Command/DeleteOrphanedFilesTest.php new file mode 100644 index 00000000000..ff29942bc4a --- /dev/null +++ b/apps/files/tests/Command/DeleteOrphanedFilesTest.php @@ -0,0 +1,130 @@ + + * @author Robin Appelman + * @author Thomas Müller + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Tests\Command; + +use OCA\Files\Command\DeleteOrphanedFiles; +use OCP\Files\StorageNotAvailableException; + +/** + * Class DeleteOrphanedFilesTest + * + * @group DB + * + * @package OCA\Files\Tests\Command + */ +class DeleteOrphanedFilesTest extends \Test\TestCase { + + /** + * @var DeleteOrphanedFiles + */ + private $command; + + /** + * @var \OCP\IDBConnection + */ + private $connection; + + /** + * @var string + */ + private $user1; + + protected function setup() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + + $this->user1 = $this->getUniqueID('user1_'); + + $userManager = \OC::$server->getUserManager(); + $userManager->createUser($this->user1, 'pass'); + + $this->command = new DeleteOrphanedFiles($this->connection); + } + + protected function tearDown() { + $userManager = \OC::$server->getUserManager(); + $user1 = $userManager->get($this->user1); + if($user1) { + $user1->delete(); + } + + $this->logout(); + + parent::tearDown(); + } + + protected function getFile($fileId) { + $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*filecache` WHERE `fileid` = ?', [$fileId]); + return $stmt->fetchAll(); + } + + /** + * Test clearing orphaned files + */ + public function testClearFiles() { + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface') + ->disableOriginalConstructor() + ->getMock(); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->loginAsUser($this->user1); + + $view = new \OC\Files\View('/' . $this->user1 . '/'); + $view->mkdir('files/test'); + + $fileInfo = $view->getFileInfo('files/test'); + + $storageId = $fileInfo->getStorage()->getId(); + + $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available'); + + $this->command->execute($input, $output); + + $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available'); + + $deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]); + $this->assertNotNull($deletedRows, 'Asserts that storage got deleted'); + $this->assertSame(1, $deletedRows, 'Asserts that storage got deleted'); + + // parent folder, `files`, ´test` and `welcome.txt` => 4 elements + $output + ->expects($this->once()) + ->method('writeln') + ->with('4 orphaned file cache entries deleted'); + + $this->command->execute($input, $output); + + $this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up'); + + // since we deleted the storage it might throw a (valid) StorageNotAvailableException + try { + $view->unlink('files/test'); + } catch (StorageNotAvailableException $e) { + } + } +} + diff --git a/apps/files/tests/Controller/ApiControllerTest.php b/apps/files/tests/Controller/ApiControllerTest.php new file mode 100644 index 00000000000..2eba7d62feb --- /dev/null +++ b/apps/files/tests/Controller/ApiControllerTest.php @@ -0,0 +1,399 @@ + + * @author Lukas Reschke + * @author Morris Jobke + * @author Roeland Jago Douma + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Controller; + +use OC\Files\FileInfo; +use OCP\AppFramework\Http; +use OCP\Files\NotFoundException; +use OCP\Files\StorageNotAvailableException; +use Test\TestCase; +use OCP\IRequest; +use OCA\Files\Service\TagService; +use OCP\AppFramework\Http\DataResponse; +use OCP\IPreview; +use OCP\Image; + +/** + * Class ApiController + * + * @package OCA\Files\Controller + */ +class ApiControllerTest extends TestCase { + /** @var string */ + private $appName = 'files'; + /** @var \OCP\IUser */ + private $user; + /** @var IRequest */ + private $request; + /** @var TagService */ + private $tagService; + /** @var IPreview */ + private $preview; + /** @var ApiController */ + private $apiController; + /** @var \OCP\Share\IManager */ + private $shareManager; + /** @var \OCP\IConfig */ + private $config; + + public function setUp() { + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->user = $this->getMock('\OCP\IUser'); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('user1')); + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService') + ->disableOriginalConstructor() + ->getMock(); + $this->shareManager = $this->getMockBuilder('\OCP\Share\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->preview = $this->getMockBuilder('\OCP\IPreview') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMock('\OCP\IConfig'); + + $this->apiController = new ApiController( + $this->appName, + $this->request, + $userSession, + $this->tagService, + $this->preview, + $this->shareManager, + $this->config + ); + } + + public function testGetFilesByTagEmpty() { + $tagName = 'MyTagName'; + $this->tagService->expects($this->once()) + ->method('getFilesByTag') + ->with($this->equalTo([$tagName])) + ->will($this->returnValue([])); + + $expected = new DataResponse(['files' => []]); + $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); + } + + public function testGetFilesByTagSingle() { + $tagName = 'MyTagName'; + $fileInfo = new FileInfo( + '/root.txt', + $this->getMockBuilder('\OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(), + '/var/www/root.txt', + [ + 'mtime' => 55, + 'mimetype' => 'application/pdf', + 'permissions' => 31, + 'size' => 1234, + 'etag' => 'MyEtag', + ], + $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') + ->disableOriginalConstructor() + ->getMock() + ); + $node = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo)); + $this->tagService->expects($this->once()) + ->method('getFilesByTag') + ->with($this->equalTo([$tagName])) + ->will($this->returnValue([$node])); + + $this->shareManager->expects($this->any()) + ->method('getSharesBy') + ->with( + $this->equalTo('user1'), + $this->anything(), + $node, + $this->equalTo(false), + $this->equalTo(1) + ) + ->will($this->returnCallback(function($userId, $shareType) { + if ($shareType === \OCP\Share::SHARE_TYPE_USER || $shareType === \OCP\Share::SHARE_TYPE_LINK) { + return ['dummy_share']; + } + return []; + })); + + $expected = new DataResponse([ + 'files' => [ + [ + 'id' => null, + 'parentId' => null, + 'mtime' => 55000, + 'name' => 'root.txt', + 'permissions' => 31, + 'mimetype' => 'application/pdf', + 'size' => 1234, + 'type' => 'file', + 'etag' => 'MyEtag', + 'path' => '/', + 'tags' => [ + [ + 'MyTagName' + ] + ], + 'shareTypes' => [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK] + ], + ], + ]); + $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); + } + + public function testGetFilesByTagMultiple() { + $tagName = 'MyTagName'; + $fileInfo1 = new FileInfo( + '/root.txt', + $this->getMockBuilder('\OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(), + '/var/www/root.txt', + [ + 'mtime' => 55, + 'mimetype' => 'application/pdf', + 'permissions' => 31, + 'size' => 1234, + 'etag' => 'MyEtag', + ], + $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') + ->disableOriginalConstructor() + ->getMock() + ); + $fileInfo2 = new FileInfo( + '/root.txt', + $this->getMockBuilder('\OC\Files\Storage\Storage') + ->disableOriginalConstructor() + ->getMock(), + '/var/www/some/sub.txt', + [ + 'mtime' => 999, + 'mimetype' => 'application/binary', + 'permissions' => 31, + 'size' => 9876, + 'etag' => 'SubEtag', + ], + $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') + ->disableOriginalConstructor() + ->getMock() + ); + $node1 = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node1->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo1)); + $node2 = $this->getMockBuilder('\OC\Files\Node\File') + ->disableOriginalConstructor() + ->getMock(); + $node2->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue($fileInfo2)); + $this->tagService->expects($this->once()) + ->method('getFilesByTag') + ->with($this->equalTo([$tagName])) + ->will($this->returnValue([$node1, $node2])); + + $expected = new DataResponse([ + 'files' => [ + [ + 'id' => null, + 'parentId' => null, + 'mtime' => 55000, + 'name' => 'root.txt', + 'permissions' => 31, + 'mimetype' => 'application/pdf', + 'size' => 1234, + 'type' => 'file', + 'etag' => 'MyEtag', + 'path' => '/', + 'tags' => [ + [ + 'MyTagName' + ] + ], + ], + [ + 'id' => null, + 'parentId' => null, + 'mtime' => 999000, + 'name' => 'root.txt', + 'permissions' => 31, + 'mimetype' => 'application/binary', + 'size' => 9876, + 'type' => 'file', + 'etag' => 'SubEtag', + 'path' => '/', + 'tags' => [ + [ + 'MyTagName' + ] + ], + ] + ], + ]); + $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); + } + + public function testUpdateFileTagsEmpty() { + $expected = new DataResponse([]); + $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt')); + } + + public function testUpdateFileTagsWorking() { + $this->tagService->expects($this->once()) + ->method('updateFileTags') + ->with('/path.txt', ['Tag1', 'Tag2']); + + $expected = new DataResponse([ + 'tags' => [ + 'Tag1', + 'Tag2' + ], + ]); + $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); + } + + public function testUpdateFileTagsNotFoundException() { + $this->tagService->expects($this->once()) + ->method('updateFileTags') + ->with('/path.txt', ['Tag1', 'Tag2']) + ->will($this->throwException(new NotFoundException('My error message'))); + + $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); + $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); + } + + public function testUpdateFileTagsStorageNotAvailableException() { + $this->tagService->expects($this->once()) + ->method('updateFileTags') + ->with('/path.txt', ['Tag1', 'Tag2']) + ->will($this->throwException(new StorageNotAvailableException('My error message'))); + + $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE); + $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); + } + + public function testUpdateFileTagsStorageGenericException() { + $this->tagService->expects($this->once()) + ->method('updateFileTags') + ->with('/path.txt', ['Tag1', 'Tag2']) + ->will($this->throwException(new \Exception('My error message'))); + + $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); + $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); + } + + public function testGetThumbnailInvalidSize() { + $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); + $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, '')); + } + + public function testGetThumbnailInvaidImage() { + $this->preview->expects($this->once()) + ->method('createPreview') + ->with('files/unknown.jpg', 10, 10, true) + ->willReturn(new Image); + $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); + $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg')); + } + + public function testGetThumbnail() { + $this->preview->expects($this->once()) + ->method('createPreview') + ->with('files/known.jpg', 10, 10, true) + ->willReturn(new Image(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); + + $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg'); + + $this->assertEquals(Http::STATUS_OK, $ret->getStatus()); + } + + public function testUpdateFileSorting() { + $mode = 'mtime'; + $direction = 'desc'; + + $this->config->expects($this->at(0)) + ->method('setUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting', $mode); + $this->config->expects($this->at(1)) + ->method('setUserValue') + ->with($this->user->getUID(), 'files', 'file_sorting_direction', $direction); + + $expected = new HTTP\Response(); + $actual = $this->apiController->updateFileSorting($mode, $direction); + $this->assertEquals($expected, $actual); + } + + public function invalidSortingModeData() { + return [ + ['color', 'asc'], + ['name', 'size'], + ['foo', 'bar'] + ]; + } + + /** + * @dataProvider invalidSortingModeData + */ + public function testUpdateInvalidFileSorting($mode, $direction) { + $this->config->expects($this->never()) + ->method('setUserValue'); + + $expected = new Http\Response(null); + $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); + + $result = $this->apiController->updateFileSorting($mode, $direction); + + $this->assertEquals($expected, $result); + } + + public function testShowHiddenFiles() { + $show = false; + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with($this->user->getUID(), 'files', 'show_hidden', $show); + + $expected = new Http\Response(); + $actual = $this->apiController->showHiddenFiles($show); + + $this->assertEquals($expected, $actual); + } + +} diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php new file mode 100644 index 00000000000..affcdb53a4b --- /dev/null +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -0,0 +1,482 @@ + + * @author Joas Schilling + * @author Lukas Reschke + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files\Tests\Controller; + +use OCA\Files\Controller\ViewController; +use OCP\AppFramework\Http; +use OCP\IUser; +use OCP\Template; +use Test\TestCase; +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\AppFramework\Http\RedirectResponse; +use OCP\INavigationManager; +use OCP\IL10N; +use OCP\IConfig; +use OCP\IUserSession; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\Files\Folder; +use OCP\App\IAppManager; + +/** + * Class ViewControllerTest + * + * @package OCA\Files\Tests\Controller + */ +class ViewControllerTest extends TestCase { + /** @var IRequest */ + private $request; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var INavigationManager */ + private $navigationManager; + /** @var IL10N */ + private $l10n; + /** @var IConfig */ + private $config; + /** @var EventDispatcherInterface */ + private $eventDispatcher; + /** @var ViewController */ + private $viewController; + /** @var IUser */ + private $user; + /** @var IUserSession */ + private $userSession; + /** @var IAppManager */ + private $appManager; + /** @var Folder */ + private $rootFolder; + + public function setUp() { + parent::setUp(); + $this->request = $this->getMock('\OCP\IRequest'); + $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); + $this->navigationManager = $this->getMock('\OCP\INavigationManager'); + $this->l10n = $this->getMock('\OCP\IL10N'); + $this->config = $this->getMock('\OCP\IConfig'); + $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->appManager = $this->getMock('\OCP\App\IAppManager'); + $this->user = $this->getMock('\OCP\IUser'); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('testuser1')); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $this->rootFolder = $this->getMock('\OCP\Files\Folder'); + $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController') + ->setConstructorArgs([ + 'files', + $this->request, + $this->urlGenerator, + $this->navigationManager, + $this->l10n, + $this->config, + $this->eventDispatcher, + $this->userSession, + $this->appManager, + $this->rootFolder + ]) + ->setMethods([ + 'getStorageInfo', + 'renderScript' + ]) + ->getMock(); + } + + public function testIndexWithIE8RedirectAndDirDefined() { + $this->request + ->expects($this->once()) + ->method('isUserAgent') + ->with(['/MSIE 8.0/']) + ->will($this->returnValue(true)); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('/apps/files/')); + + $expected = new Http\RedirectResponse('/apps/files/#?dir=MyDir'); + $this->assertEquals($expected, $this->viewController->index('MyDir')); + } + + public function testIndexWithIE8RedirectAndViewDefined() { + $this->request + ->expects($this->once()) + ->method('isUserAgent') + ->with(['/MSIE 8.0/']) + ->will($this->returnValue(true)); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('/apps/files/')); + + $expected = new Http\RedirectResponse('/apps/files/#?dir=/&view=MyView'); + $this->assertEquals($expected, $this->viewController->index('', 'MyView')); + } + + public function testIndexWithIE8RedirectAndViewAndDirDefined() { + $this->request + ->expects($this->once()) + ->method('isUserAgent') + ->with(['/MSIE 8.0/']) + ->will($this->returnValue(true)); + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('/apps/files/')); + + $expected = new RedirectResponse('/apps/files/#?dir=MyDir&view=MyView'); + $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); + } + + public function testIndexWithRegularBrowser() { + $this->request + ->expects($this->once()) + ->method('isUserAgent') + ->with(['/MSIE 8.0/']) + ->will($this->returnValue(false)); + $this->viewController + ->expects($this->once()) + ->method('getStorageInfo') + ->will($this->returnValue([ + 'relative' => 123, + 'owner' => 'MyName', + 'ownerDisplayName' => 'MyDisplayName', + ])); + $this->config->expects($this->exactly(3)) + ->method('getUserValue') + ->will($this->returnValueMap([ + [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'], + [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'], + [$this->user->getUID(), 'files', 'show_hidden', false, false], + ])); + + $this->config + ->expects($this->any()) + ->method('getAppValue') + ->will($this->returnArgument(2)); + + $nav = new Template('files', 'appnavigation'); + $nav->assign('navigationItems', [ + [ + 'id' => 'files', + 'appname' => 'files', + 'script' => 'list.php', + 'order' => 0, + 'name' => new \OC_L10N_String(new \OC_L10N('files'), 'All files', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'favorites', + 'appname' => 'files', + 'script' => 'simplelist.php', + 'order' => 5, + 'name' => null, + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'sharingin', + 'appname' => 'files_sharing', + 'script' => 'list.php', + 'order' => 10, + 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with you', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'sharingout', + 'appname' => 'files_sharing', + 'script' => 'list.php', + 'order' => 15, + 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with others', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'sharinglinks', + 'appname' => 'files_sharing', + 'script' => 'list.php', + 'order' => 20, + 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared by link', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'systemtagsfilter', + 'appname' => 'systemtags', + 'script' => 'list.php', + 'order' => 25, + 'name' => new \OC_L10N_String(new \OC_L10N('systemtags'), 'Tags', []), + 'active' => false, + 'icon' => '', + ], + [ + 'id' => 'trashbin', + 'appname' => 'files_trashbin', + 'script' => 'list.php', + 'order' => 50, + 'name' => new \OC_L10N_String(new \OC_L10N('files_trashbin'), 'Deleted files', []), + 'active' => false, + 'icon' => '', + ], + ]); + + $expected = new Http\TemplateResponse( + 'files', + 'index', + [ + 'usedSpacePercent' => 123, + 'owner' => 'MyName', + 'ownerDisplayName' => 'MyDisplayName', + 'isPublic' => false, + 'defaultFileSorting' => 'name', + 'defaultFileSortingDirection' => 'asc', + 'showHiddenFiles' => false, + 'mailNotificationEnabled' => 'no', + 'mailPublicNotificationEnabled' => 'no', + 'allowShareWithLink' => 'yes', + 'appNavigation' => $nav, + 'appContents' => [ + [ + 'id' => 'files', + 'content' => null, + ], + [ + 'id' => 'favorites', + 'content' => null, + ], + [ + 'id' => 'sharingin', + 'content' => null, + ], + [ + 'id' => 'sharingout', + 'content' => null, + ], + [ + 'id' => 'sharinglinks', + 'content' => null, + ], + [ + 'id' => 'systemtagsfilter', + 'content' => null, + ], + [ + 'id' => 'trashbin', + 'content' => null, + ], + ], + ] + ); + $policy = new Http\ContentSecurityPolicy(); + $policy->addAllowedFrameDomain('\'self\''); + $expected->setContentSecurityPolicy($policy); + $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); + } + + public function showFileMethodProvider() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider showFileMethodProvider + */ + public function testShowFileRouteWithFolder($useShowFile) { + $node = $this->getMock('\OCP\Files\Folder'); + $node->expects($this->once()) + ->method('getPath') + ->will($this->returnValue('/testuser1/files/test/sub')); + + $baseFolder = $this->getMock('\OCP\Files\Folder'); + + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('testuser1/files/') + ->will($this->returnValue($baseFolder)); + + $baseFolder->expects($this->at(0)) + ->method('getById') + ->with(123) + ->will($this->returnValue([$node])); + $baseFolder->expects($this->at(1)) + ->method('getRelativePath') + ->with('/testuser1/files/test/sub') + ->will($this->returnValue('/test/sub')); + + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index', ['dir' => '/test/sub']) + ->will($this->returnValue('/apps/files/?dir=/test/sub')); + + $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub'); + if ($useShowFile) { + $this->assertEquals($expected, $this->viewController->showFile(123)); + } else { + $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); + } + } + + /** + * @dataProvider showFileMethodProvider + */ + public function testShowFileRouteWithFile($useShowFile) { + $parentNode = $this->getMock('\OCP\Files\Folder'); + $parentNode->expects($this->once()) + ->method('getPath') + ->will($this->returnValue('testuser1/files/test')); + + $baseFolder = $this->getMock('\OCP\Files\Folder'); + + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('testuser1/files/') + ->will($this->returnValue($baseFolder)); + + $node = $this->getMock('\OCP\Files\File'); + $node->expects($this->once()) + ->method('getParent') + ->will($this->returnValue($parentNode)); + $node->expects($this->once()) + ->method('getName') + ->will($this->returnValue('somefile.txt')); + + $baseFolder->expects($this->at(0)) + ->method('getById') + ->with(123) + ->will($this->returnValue([$node])); + $baseFolder->expects($this->at(1)) + ->method('getRelativePath') + ->with('testuser1/files/test') + ->will($this->returnValue('/test')); + + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt']) + ->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt')); + + $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt'); + if ($useShowFile) { + $this->assertEquals($expected, $this->viewController->showFile(123)); + } else { + $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); + } + } + + /** + * @dataProvider showFileMethodProvider + */ + public function testShowFileRouteWithInvalidFileId($useShowFile) { + $baseFolder = $this->getMock('\OCP\Files\Folder'); + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('testuser1/files/') + ->will($this->returnValue($baseFolder)); + + $baseFolder->expects($this->at(0)) + ->method('getById') + ->with(123) + ->will($this->returnValue([])); + + $expected = new Http\NotFoundResponse(); + if ($useShowFile) { + $this->assertEquals($expected, $this->viewController->showFile(123)); + } else { + $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); + } + } + + /** + * @dataProvider showFileMethodProvider + */ + public function testShowFileRouteWithTrashedFile($useShowFile) { + $this->appManager->expects($this->once()) + ->method('isEnabledForUser') + ->with('files_trashbin') + ->will($this->returnValue(true)); + + $parentNode = $this->getMock('\OCP\Files\Folder'); + $parentNode->expects($this->once()) + ->method('getPath') + ->will($this->returnValue('testuser1/files_trashbin/files/test.d1462861890/sub')); + + $baseFolderFiles = $this->getMock('\OCP\Files\Folder'); + $baseFolderTrash = $this->getMock('\OCP\Files\Folder'); + + $this->rootFolder->expects($this->at(0)) + ->method('get') + ->with('testuser1/files/') + ->will($this->returnValue($baseFolderFiles)); + $this->rootFolder->expects($this->at(1)) + ->method('get') + ->with('testuser1/files_trashbin/files/') + ->will($this->returnValue($baseFolderTrash)); + + $baseFolderFiles->expects($this->once()) + ->method('getById') + ->with(123) + ->will($this->returnValue([])); + + $node = $this->getMock('\OCP\Files\File'); + $node->expects($this->once()) + ->method('getParent') + ->will($this->returnValue($parentNode)); + $node->expects($this->once()) + ->method('getName') + ->will($this->returnValue('somefile.txt')); + + $baseFolderTrash->expects($this->at(0)) + ->method('getById') + ->with(123) + ->will($this->returnValue([$node])); + $baseFolderTrash->expects($this->at(1)) + ->method('getRelativePath') + ->with('testuser1/files_trashbin/files/test.d1462861890/sub') + ->will($this->returnValue('/test.d1462861890/sub')); + + $this->urlGenerator + ->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt']) + ->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt')); + + $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'); + if ($useShowFile) { + $this->assertEquals($expected, $this->viewController->showFile(123)); + } else { + $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); + } + } +} diff --git a/apps/files/tests/HelperTest.php b/apps/files/tests/HelperTest.php new file mode 100644 index 00000000000..d2d52b4f8a3 --- /dev/null +++ b/apps/files/tests/HelperTest.php @@ -0,0 +1,114 @@ + + * @author Joas Schilling + * @author Morris Jobke + * @author Robin Appelman + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ + +use OCA\Files\Tests; + +/** + * Class Helper + */ +class HelperTest extends \Test\TestCase { + + private function makeFileInfo($name, $size, $mtime, $isDir = false) { + return new \OC\Files\FileInfo( + '/' . $name, + null, + '/', + array( + 'name' => $name, + 'size' => $size, + 'mtime' => $mtime, + 'type' => $isDir ? 'dir' : 'file', + 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream' + ), + null + ); + } + + /** + * Returns a file list for testing + */ + private function getTestFileList() { + return array( + self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)), + self::makeFileInfo('q.txt', 5, 150), + self::makeFileInfo('subdir2', 87, 128, true), + self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800), + self::makeFileInfo('o.txt', 12, 100), + self::makeFileInfo('subdir', 88, 125, true), + ); + } + + function sortDataProvider() { + return array( + array( + 'name', + false, + array('subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'), + ), + array( + 'name', + true, + array('q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'), + ), + array( + 'size', + false, + array('a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'), + ), + array( + 'size', + true, + array('b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'), + ), + array( + 'mtime', + false, + array('o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'), + ), + array( + 'mtime', + true, + array('a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'), + ), + ); + } + + /** + * @dataProvider sortDataProvider + */ + public function testSortByName($sort, $sortDescending, $expectedOrder) { + $files = self::getTestFileList(); + $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending); + $fileNames = array(); + foreach ($files as $fileInfo) { + $fileNames[] = $fileInfo->getName(); + } + $this->assertEquals( + $expectedOrder, + $fileNames + ); + } + +} diff --git a/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php new file mode 100644 index 00000000000..3f210b500ff --- /dev/null +++ b/apps/files/tests/Service/TagServiceTest.php @@ -0,0 +1,127 @@ + + * @author Thomas Müller + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ +namespace OCA\Files\Tests\Service; + +use OCA\Files\Service\TagService; + +/** + * Class TagServiceTest + * + * @group DB + * + * @package OCA\Files + */ +class TagServiceTest extends \Test\TestCase { + + /** + * @var string + */ + private $user; + + /** + * @var \OCP\Files\Folder + */ + private $root; + + /** + * @var \OCA\Files\Service\TagService + */ + private $tagService; + + /** + * @var \OCP\ITags + */ + private $tagger; + + protected function setUp() { + parent::setUp(); + $this->user = $this->getUniqueID('user'); + \OC::$server->getUserManager()->createUser($this->user, 'test'); + \OC_User::setUserId($this->user); + \OC_Util::setupFS($this->user); + /** @var \OCP\IUser */ + $user = new \OC\User\User($this->user, null); + /** + * @var \OCP\IUserSession + */ + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession->expects($this->any()) + ->method('getUser') + ->withAnyParameters() + ->will($this->returnValue($user)); + + $this->root = \OC::$server->getUserFolder(); + + $this->tagger = \OC::$server->getTagManager()->load('files'); + $this->tagService = new TagService( + $userSession, + $this->tagger, + $this->root + ); + } + + protected function tearDown() { + \OC_User::setUserId(''); + $user = \OC::$server->getUserManager()->get($this->user); + if ($user !== null) { $user->delete(); } + } + + public function testUpdateFileTags() { + $tag1 = 'tag1'; + $tag2 = 'tag2'; + + $subdir = $this->root->newFolder('subdir'); + $testFile = $subdir->newFile('test.txt'); + $testFile->putContent('test contents'); + + $fileId = $testFile->getId(); + + // set tags + $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2)); + + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); + + // remove tag + $this->tagService->updateFileTags('subdir/test.txt', array($tag2)); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); + + // clear tags + $this->tagService->updateFileTags('subdir/test.txt', array()); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2)); + + // non-existing file + $caught = false; + try { + $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1)); + } catch (\OCP\Files\NotFoundException $e) { + $caught = true; + } + $this->assertTrue($caught); + + $subdir->delete(); + } +} + diff --git a/apps/files/tests/activitytest.php b/apps/files/tests/activitytest.php deleted file mode 100644 index bc062dd0542..00000000000 --- a/apps/files/tests/activitytest.php +++ /dev/null @@ -1,375 +0,0 @@ - - * @author Morris Jobke - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Tests; - -use OCA\Files\Activity; -use Test\TestCase; - -/** - * Class ActivityTest - * - * @group DB - * @package OCA\Files\Tests - */ -class ActivityTest extends TestCase { - - /** @var \OCP\Activity\IManager */ - private $activityManager; - - /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ - protected $request; - - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ - protected $session; - - /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ - protected $config; - - /** @var \OCA\Files\ActivityHelper|\PHPUnit_Framework_MockObject_MockObject */ - protected $activityHelper; - - /** @var \OCP\L10N\IFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $l10nFactory; - - /** @var \OCA\Files\Activity */ - protected $activityExtension; - - protected function setUp() { - parent::setUp(); - - $this->request = $this->getMockBuilder('OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->session = $this->getMockBuilder('OCP\IUserSession') - ->disableOriginalConstructor() - ->getMock(); - $this->config = $this->getMockBuilder('OCP\IConfig') - ->disableOriginalConstructor() - ->getMock(); - $this->activityHelper = $this->getMockBuilder('OCA\Files\ActivityHelper') - ->disableOriginalConstructor() - ->getMock(); - - $this->activityManager = new \OC\Activity\Manager( - $this->request, - $this->session, - $this->config - ); - - $this->l10nFactory = $this->getMockBuilder('OCP\L10N\IFactory') - ->disableOriginalConstructor() - ->getMock(); - $deL10n = $this->getMockBuilder('OC_L10N') - ->disableOriginalConstructor() - ->getMock(); - $deL10n->expects($this->any()) - ->method('t') - ->willReturnCallback(function ($argument) { - return 'translate(' . $argument . ')'; - }); - - $this->l10nFactory->expects($this->any()) - ->method('get') - ->willReturnMap([ - ['files', null, new \OC_L10N('files', 'en')], - ['files', 'en', new \OC_L10N('files', 'en')], - ['files', 'de', $deL10n], - ]); - - $this->activityExtension = $activityExtension = new Activity( - $this->l10nFactory, - $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), - $this->activityManager, - $this->activityHelper, - \OC::$server->getDatabaseConnection(), - $this->config - ); - - $this->activityManager->registerExtension(function() use ($activityExtension) { - return $activityExtension; - }); - } - - public function testNotificationTypes() { - $result = $this->activityExtension->getNotificationTypes('en'); - $this->assertTrue(is_array($result), 'Asserting getNotificationTypes() returns an array'); - $this->assertCount(5, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); - $this->assertArrayHasKey(Activity::TYPE_FAVORITES, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); - } - - public function testDefaultTypes() { - $result = $this->activityExtension->getDefaultTypes('stream'); - $this->assertTrue(is_array($result), 'Asserting getDefaultTypes(stream) returns an array'); - $this->assertCount(4, $result); - $result = array_flip($result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); - $this->assertArrayNotHasKey(Activity::TYPE_FAVORITES, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); - $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); - - $result = $this->activityExtension->getDefaultTypes('email'); - $this->assertFalse($result, 'Asserting getDefaultTypes(email) returns false'); - } - - public function testTranslate() { - $this->assertFalse( - $this->activityExtension->translate('files_sharing', '', [], false, false, 'en'), - 'Asserting that no translations are set for files_sharing' - ); - - // Test english - $this->assertNotFalse( - $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en'), - 'Asserting that translations are set for files.deleted_self' - ); - $this->assertStringStartsWith( - 'You deleted ', - $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en') - ); - - // Test translation - $this->assertNotFalse( - $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de'), - 'Asserting that translations are set for files.deleted_self' - ); - $this->assertStringStartsWith( - 'translate(You deleted ', - $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de') - ); - } - - public function testGetSpecialParameterList() { - $this->assertFalse( - $this->activityExtension->getSpecialParameterList('files_sharing', ''), - 'Asserting that no special parameters are set for files_sharing' - ); - } - - public function typeIconData() { - return [ - [Activity::TYPE_SHARE_CHANGED, 'icon-change'], - [Activity::TYPE_SHARE_CREATED, 'icon-add-color'], - [Activity::TYPE_SHARE_DELETED, 'icon-delete-color'], - [Activity::TYPE_SHARE_RESTORED, false], - [Activity::TYPE_FAVORITES, false], - ['unknown type', false], - ]; - } - - /** - * @dataProvider typeIconData - * - * @param string $type - * @param mixed $expected - */ - public function testTypeIcon($type, $expected) { - $this->assertSame($expected, $this->activityExtension->getTypeIcon($type)); - } - - public function testGroupParameter() { - $this->assertFalse( - $this->activityExtension->getGroupParameter(['app' => 'files_sharing']), - 'Asserting that no group parameters are set for files_sharing' - ); - } - - public function testNavigation() { - $result = $this->activityExtension->getNavigation(); - $this->assertCount(1, $result['top']); - $this->assertArrayHasKey(Activity::FILTER_FAVORITES, $result['top']); - - $this->assertCount(1, $result['apps']); - $this->assertArrayHasKey(Activity::FILTER_FILES, $result['apps']); - } - - public function testIsFilterValid() { - $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FAVORITES)); - $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FILES)); - $this->assertFalse($this->activityExtension->isFilterValid('unknown filter')); - } - - public function filterNotificationTypesData() { - return [ - [ - Activity::FILTER_FILES, - [ - 'NT0', - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_SHARE_CHANGED, - Activity::TYPE_SHARE_DELETED, - Activity::TYPE_SHARE_RESTORED, - Activity::TYPE_FAVORITES, - ], [ - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_SHARE_CHANGED, - Activity::TYPE_SHARE_DELETED, - Activity::TYPE_SHARE_RESTORED, - ], - ], - [ - Activity::FILTER_FILES, - [ - 'NT0', - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_FAVORITES, - ], - [ - Activity::TYPE_SHARE_CREATED, - ], - ], - [ - Activity::FILTER_FAVORITES, - [ - 'NT0', - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_SHARE_CHANGED, - Activity::TYPE_SHARE_DELETED, - Activity::TYPE_SHARE_RESTORED, - Activity::TYPE_FAVORITES, - ], [ - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_SHARE_CHANGED, - Activity::TYPE_SHARE_DELETED, - Activity::TYPE_SHARE_RESTORED, - ], - ], - [ - 'unknown filter', - [ - 'NT0', - Activity::TYPE_SHARE_CREATED, - Activity::TYPE_SHARE_CHANGED, - Activity::TYPE_SHARE_DELETED, - Activity::TYPE_SHARE_RESTORED, - Activity::TYPE_FAVORITES, - ], - false, - ], - ]; - } - - /** - * @dataProvider filterNotificationTypesData - * - * @param string $filter - * @param array $types - * @param mixed $expected - */ - public function testFilterNotificationTypes($filter, $types, $expected) { - $result = $this->activityExtension->filterNotificationTypes($types, $filter); - $this->assertEquals($expected, $result); - } - - public function queryForFilterData() { - return [ - [ - new \RuntimeException(), - '`app` = ?', - ['files'] - ], - [ - [ - 'items' => [], - 'folders' => [], - ], - ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?)) THEN 1 ELSE 0 END = 1 ', - ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED] - ], - [ - [ - 'items' => ['file.txt', 'folder'], - 'folders' => ['folder'], - ], - ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?) OR `file` = ? OR `file` = ? OR `file` LIKE ?) THEN 1 ELSE 0 END = 1 ', - ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'file.txt', 'folder', 'folder/%'] - ], - ]; - } - - /** - * @dataProvider queryForFilterData - * - * @param mixed $will - * @param string $query - * @param array $parameters - */ - public function testQueryForFilter($will, $query, $parameters) { - $this->mockUserSession('test'); - - $this->config->expects($this->any()) - ->method('getUserValue') - ->willReturnMap([ - ['test', 'activity', 'notify_stream_' . Activity::TYPE_FAVORITES, false, true], - ]); - if (is_array($will)) { - $this->activityHelper->expects($this->any()) - ->method('getFavoriteFilePaths') - ->with('test') - ->willReturn($will); - } else { - $this->activityHelper->expects($this->any()) - ->method('getFavoriteFilePaths') - ->with('test') - ->willThrowException($will); - } - - $result = $this->activityExtension->getQueryForFilter('all'); - $this->assertEquals([$query, $parameters], $result); - - $this->executeQueryForFilter($result); - } - - public function executeQueryForFilter(array $result) { - list($resultQuery, $resultParameters) = $result; - $resultQuery = str_replace('`file`', '`user`', $resultQuery); - $resultQuery = str_replace('`type`', '`key`', $resultQuery); - - $connection = \OC::$server->getDatabaseConnection(); - // Test the query on the privatedata table, because the activity table - // does not exist in core - $result = $connection->executeQuery('SELECT * FROM `*PREFIX*privatedata` WHERE ' . $resultQuery, $resultParameters); - $rows = $result->fetchAll(); - $result->closeCursor(); - } - - protected function mockUserSession($user) { - $mockUser = $this->getMockBuilder('\OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - $mockUser->expects($this->any()) - ->method('getUID') - ->willReturn($user); - - $this->session->expects($this->any()) - ->method('isLoggedIn') - ->willReturn(true); - $this->session->expects($this->any()) - ->method('getUser') - ->willReturn($mockUser); - } -} diff --git a/apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php b/apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php deleted file mode 100644 index e802a248a9b..00000000000 --- a/apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php +++ /dev/null @@ -1,259 +0,0 @@ - - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Tests\BackgroundJob; - -use OCA\Files\BackgroundJob\DeleteOrphanedItems; -use OCP\DB\QueryBuilder\IQueryBuilder; - -/** - * Class DeleteOrphanedItemsJobTest - * - * @group DB - * - * @package Test\BackgroundJob - */ -class DeleteOrphanedItemsJobTest extends \Test\TestCase { - - /** @var \OCP\IDBConnection */ - protected $connection; - - protected function setup() { - parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); - } - - protected function cleanMapping($table) { - $query = $this->connection->getQueryBuilder(); - $query->delete($table)->execute(); - } - - protected function getMappings($table) { - $query = $this->connection->getQueryBuilder(); - $query->select('*') - ->from($table); - $result = $query->execute(); - $mapping = $result->fetchAll(); - $result->closeCursor(); - - return $mapping; - } - - /** - * Test clearing orphaned system tag mappings - */ - public function testClearSystemTagMappings() { - $this->cleanMapping('systemtag_object_mapping'); - - $query = $this->connection->getQueryBuilder(); - $query->insert('filecache') - ->values([ - 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), - 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); - $fileId = $query->getLastInsertId(); - - // Existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('systemtag_object_mapping') - ->values([ - 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'objecttype' => $query->createNamedParameter('files'), - 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); - - // Non-existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('systemtag_object_mapping') - ->values([ - 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), - 'objecttype' => $query->createNamedParameter('files'), - 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); - - $mapping = $this->getMappings('systemtag_object_mapping'); - $this->assertCount(2, $mapping); - - $job = new DeleteOrphanedItems(); - $this->invokePrivate($job, 'cleanSystemTags'); - - $mapping = $this->getMappings('systemtag_object_mapping'); - $this->assertCount(1, $mapping); - - $query = $this->connection->getQueryBuilder(); - $query->delete('filecache') - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); - $this->cleanMapping('systemtag_object_mapping'); - } - - /** - * Test clearing orphaned system tag mappings - */ - public function testClearUserTagMappings() { - $this->cleanMapping('vcategory_to_object'); - - $query = $this->connection->getQueryBuilder(); - $query->insert('filecache') - ->values([ - 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), - 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); - $fileId = $query->getLastInsertId(); - - // Existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('vcategory_to_object') - ->values([ - 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'type' => $query->createNamedParameter('files'), - 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); - - // Non-existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('vcategory_to_object') - ->values([ - 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), - 'type' => $query->createNamedParameter('files'), - 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - ])->execute(); - - $mapping = $this->getMappings('vcategory_to_object'); - $this->assertCount(2, $mapping); - - $job = new DeleteOrphanedItems(); - $this->invokePrivate($job, 'cleanUserTags'); - - $mapping = $this->getMappings('vcategory_to_object'); - $this->assertCount(1, $mapping); - - $query = $this->connection->getQueryBuilder(); - $query->delete('filecache') - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); - $this->cleanMapping('vcategory_to_object'); - } - - /** - * Test clearing orphaned system tag mappings - */ - public function testClearComments() { - $this->cleanMapping('comments'); - - $query = $this->connection->getQueryBuilder(); - $query->insert('filecache') - ->values([ - 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), - 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); - $fileId = $query->getLastInsertId(); - - // Existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('comments') - ->values([ - 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'object_type' => $query->createNamedParameter('files'), - 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - 'actor_type' => $query->createNamedParameter('users'), - ])->execute(); - - // Non-existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('comments') - ->values([ - 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), - 'object_type' => $query->createNamedParameter('files'), - 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - 'actor_type' => $query->createNamedParameter('users'), - ])->execute(); - - $mapping = $this->getMappings('comments'); - $this->assertCount(2, $mapping); - - $job = new DeleteOrphanedItems(); - $this->invokePrivate($job, 'cleanComments'); - - $mapping = $this->getMappings('comments'); - $this->assertCount(1, $mapping); - - $query = $this->connection->getQueryBuilder(); - $query->delete('filecache') - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); - $this->cleanMapping('comments'); - } - - /** - * Test clearing orphaned system tag mappings - */ - public function testClearCommentReadMarks() { - $this->cleanMapping('comments_read_markers'); - - $query = $this->connection->getQueryBuilder(); - $query->insert('filecache') - ->values([ - 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), - 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), - 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), - ])->execute(); - $fileId = $query->getLastInsertId(); - - // Existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('comments_read_markers') - ->values([ - 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'object_type' => $query->createNamedParameter('files'), - 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - ])->execute(); - - // Non-existing file - $query = $this->connection->getQueryBuilder(); - $query->insert('comments_read_markers') - ->values([ - 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), - 'object_type' => $query->createNamedParameter('files'), - 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), - ])->execute(); - - $mapping = $this->getMappings('comments_read_markers'); - $this->assertCount(2, $mapping); - - $job = new DeleteOrphanedItems(); - $this->invokePrivate($job, 'cleanCommentMarkers'); - - $mapping = $this->getMappings('comments_read_markers'); - $this->assertCount(1, $mapping); - - $query = $this->connection->getQueryBuilder(); - $query->delete('filecache') - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->execute(); - $this->cleanMapping('comments_read_markers'); - } - -} diff --git a/apps/files/tests/backgroundjob/ScanFilesTest.php b/apps/files/tests/backgroundjob/ScanFilesTest.php deleted file mode 100644 index eab28071b70..00000000000 --- a/apps/files/tests/backgroundjob/ScanFilesTest.php +++ /dev/null @@ -1,133 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ -namespace OCA\Files\Tests\BackgroundJob; - -use Test\TestCase; -use OCP\IConfig; -use OCP\IUserManager; -use OCA\Files\BackgroundJob\ScanFiles; - -/** - * Class ScanFilesTest - * - * @package OCA\Files\Tests\BackgroundJob - */ -class ScanFilesTest extends TestCase { - /** @var IConfig */ - private $config; - /** @var IUserManager */ - private $userManager; - /** @var ScanFiles */ - private $scanFiles; - - public function setUp() { - parent::setUp(); - - $this->config = $this->getMock('\OCP\IConfig'); - $this->userManager = $this->getMock('\OCP\IUserManager'); - - $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles') - ->setConstructorArgs([ - $this->config, - $this->userManager, - ]) - ->setMethods(['runScanner']) - ->getMock(); - } - - public function testRunWithoutUsers() { - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->will($this->returnValue(50)); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->will($this->returnValue([])); - $this->userManager - ->expects($this->at(1)) - ->method('search') - ->with('', 500) - ->will($this->returnValue([])); - $this->config - ->expects($this->at(1)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 500); - - $this->invokePrivate($this->scanFiles, 'run', [[]]); - } - - public function testRunWithUsers() { - $fakeUser = $this->getMock('\OCP\IUser'); - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->will($this->returnValue(50)); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->will($this->returnValue([ - $fakeUser - ])); - $this->config - ->expects($this->at(1)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 550); - $this->scanFiles - ->expects($this->once()) - ->method('runScanner') - ->with($fakeUser); - - $this->invokePrivate($this->scanFiles, 'run', [[]]); - } - - public function testRunWithUsersAndOffsetAtEndOfUserList() { - $this->config - ->expects($this->at(0)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->will($this->returnValue(50)); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->will($this->returnValue([])); - $this->userManager - ->expects($this->at(1)) - ->method('search') - ->with('', 500) - ->will($this->returnValue([])); - $this->config - ->expects($this->at(1)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 500); - $this->scanFiles - ->expects($this->never()) - ->method('runScanner'); - - $this->invokePrivate($this->scanFiles, 'run', [[]]); - } - -} diff --git a/apps/files/tests/command/deleteorphanedfilestest.php b/apps/files/tests/command/deleteorphanedfilestest.php deleted file mode 100644 index ff29942bc4a..00000000000 --- a/apps/files/tests/command/deleteorphanedfilestest.php +++ /dev/null @@ -1,130 +0,0 @@ - - * @author Robin Appelman - * @author Thomas Müller - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Tests\Command; - -use OCA\Files\Command\DeleteOrphanedFiles; -use OCP\Files\StorageNotAvailableException; - -/** - * Class DeleteOrphanedFilesTest - * - * @group DB - * - * @package OCA\Files\Tests\Command - */ -class DeleteOrphanedFilesTest extends \Test\TestCase { - - /** - * @var DeleteOrphanedFiles - */ - private $command; - - /** - * @var \OCP\IDBConnection - */ - private $connection; - - /** - * @var string - */ - private $user1; - - protected function setup() { - parent::setUp(); - - $this->connection = \OC::$server->getDatabaseConnection(); - - $this->user1 = $this->getUniqueID('user1_'); - - $userManager = \OC::$server->getUserManager(); - $userManager->createUser($this->user1, 'pass'); - - $this->command = new DeleteOrphanedFiles($this->connection); - } - - protected function tearDown() { - $userManager = \OC::$server->getUserManager(); - $user1 = $userManager->get($this->user1); - if($user1) { - $user1->delete(); - } - - $this->logout(); - - parent::tearDown(); - } - - protected function getFile($fileId) { - $stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*filecache` WHERE `fileid` = ?', [$fileId]); - return $stmt->fetchAll(); - } - - /** - * Test clearing orphaned files - */ - public function testClearFiles() { - $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface') - ->disableOriginalConstructor() - ->getMock(); - $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface') - ->disableOriginalConstructor() - ->getMock(); - - $this->loginAsUser($this->user1); - - $view = new \OC\Files\View('/' . $this->user1 . '/'); - $view->mkdir('files/test'); - - $fileInfo = $view->getFileInfo('files/test'); - - $storageId = $fileInfo->getStorage()->getId(); - - $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available'); - - $this->command->execute($input, $output); - - $this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available'); - - $deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]); - $this->assertNotNull($deletedRows, 'Asserts that storage got deleted'); - $this->assertSame(1, $deletedRows, 'Asserts that storage got deleted'); - - // parent folder, `files`, ´test` and `welcome.txt` => 4 elements - $output - ->expects($this->once()) - ->method('writeln') - ->with('4 orphaned file cache entries deleted'); - - $this->command->execute($input, $output); - - $this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up'); - - // since we deleted the storage it might throw a (valid) StorageNotAvailableException - try { - $view->unlink('files/test'); - } catch (StorageNotAvailableException $e) { - } - } -} - diff --git a/apps/files/tests/controller/ViewControllerTest.php b/apps/files/tests/controller/ViewControllerTest.php deleted file mode 100644 index 4e4e88043e4..00000000000 --- a/apps/files/tests/controller/ViewControllerTest.php +++ /dev/null @@ -1,481 +0,0 @@ - - * @author Joas Schilling - * @author Lukas Reschke - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Tests\Controller; - -use OCA\Files\Controller\ViewController; -use OCP\AppFramework\Http; -use OCP\Template; -use Test\TestCase; -use OCP\IRequest; -use OCP\IURLGenerator; -use OCP\AppFramework\Http\RedirectResponse; -use OCP\INavigationManager; -use OCP\IL10N; -use OCP\IConfig; -use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use OCP\Files\Folder; -use OCP\App\IAppManager; - -/** - * Class ViewControllerTest - * - * @package OCA\Files\Tests\Controller - */ -class ViewControllerTest extends TestCase { - /** @var IRequest */ - private $request; - /** @var IURLGenerator */ - private $urlGenerator; - /** @var INavigationManager */ - private $navigationManager; - /** @var IL10N */ - private $l10n; - /** @var IConfig */ - private $config; - /** @var EventDispatcherInterface */ - private $eventDispatcher; - /** @var ViewController */ - private $viewController; - /** @var IUser */ - private $user; - /** @var IUserSession */ - private $userSession; - /** @var IAppManager */ - private $appManager; - /** @var Folder */ - private $rootFolder; - - public function setUp() { - parent::setUp(); - $this->request = $this->getMock('\OCP\IRequest'); - $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); - $this->navigationManager = $this->getMock('\OCP\INavigationManager'); - $this->l10n = $this->getMock('\OCP\IL10N'); - $this->config = $this->getMock('\OCP\IConfig'); - $this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $this->userSession = $this->getMock('\OCP\IUserSession'); - $this->appManager = $this->getMock('\OCP\App\IAppManager'); - $this->user = $this->getMock('\OCP\IUser'); - $this->user->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('testuser1')); - $this->userSession->expects($this->any()) - ->method('getUser') - ->will($this->returnValue($this->user)); - $this->rootFolder = $this->getMock('\OCP\Files\Folder'); - $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController') - ->setConstructorArgs([ - 'files', - $this->request, - $this->urlGenerator, - $this->navigationManager, - $this->l10n, - $this->config, - $this->eventDispatcher, - $this->userSession, - $this->appManager, - $this->rootFolder - ]) - ->setMethods([ - 'getStorageInfo', - 'renderScript' - ]) - ->getMock(); - } - - public function testIndexWithIE8RedirectAndDirDefined() { - $this->request - ->expects($this->once()) - ->method('isUserAgent') - ->with(['/MSIE 8.0/']) - ->will($this->returnValue(true)); - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); - - $expected = new Http\RedirectResponse('/apps/files/#?dir=MyDir'); - $this->assertEquals($expected, $this->viewController->index('MyDir')); - } - - public function testIndexWithIE8RedirectAndViewDefined() { - $this->request - ->expects($this->once()) - ->method('isUserAgent') - ->with(['/MSIE 8.0/']) - ->will($this->returnValue(true)); - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); - - $expected = new Http\RedirectResponse('/apps/files/#?dir=/&view=MyView'); - $this->assertEquals($expected, $this->viewController->index('', 'MyView')); - } - - public function testIndexWithIE8RedirectAndViewAndDirDefined() { - $this->request - ->expects($this->once()) - ->method('isUserAgent') - ->with(['/MSIE 8.0/']) - ->will($this->returnValue(true)); - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index') - ->will($this->returnValue('/apps/files/')); - - $expected = new RedirectResponse('/apps/files/#?dir=MyDir&view=MyView'); - $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); - } - - public function testIndexWithRegularBrowser() { - $this->request - ->expects($this->once()) - ->method('isUserAgent') - ->with(['/MSIE 8.0/']) - ->will($this->returnValue(false)); - $this->viewController - ->expects($this->once()) - ->method('getStorageInfo') - ->will($this->returnValue([ - 'relative' => 123, - 'owner' => 'MyName', - 'ownerDisplayName' => 'MyDisplayName', - ])); - $this->config->expects($this->exactly(3)) - ->method('getUserValue') - ->will($this->returnValueMap([ - [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'], - [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'], - [$this->user->getUID(), 'files', 'show_hidden', false, false], - ])); - - $this->config - ->expects($this->any()) - ->method('getAppValue') - ->will($this->returnArgument(2)); - - $nav = new Template('files', 'appnavigation'); - $nav->assign('navigationItems', [ - [ - 'id' => 'files', - 'appname' => 'files', - 'script' => 'list.php', - 'order' => 0, - 'name' => new \OC_L10N_String(new \OC_L10N('files'), 'All files', []), - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'favorites', - 'appname' => 'files', - 'script' => 'simplelist.php', - 'order' => 5, - 'name' => null, - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'sharingin', - 'appname' => 'files_sharing', - 'script' => 'list.php', - 'order' => 10, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with you', []), - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'sharingout', - 'appname' => 'files_sharing', - 'script' => 'list.php', - 'order' => 15, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared with others', []), - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'sharinglinks', - 'appname' => 'files_sharing', - 'script' => 'list.php', - 'order' => 20, - 'name' => new \OC_L10N_String(new \OC_L10N('files_sharing'), 'Shared by link', []), - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'systemtagsfilter', - 'appname' => 'systemtags', - 'script' => 'list.php', - 'order' => 25, - 'name' => new \OC_L10N_String(new \OC_L10N('systemtags'), 'Tags', []), - 'active' => false, - 'icon' => '', - ], - [ - 'id' => 'trashbin', - 'appname' => 'files_trashbin', - 'script' => 'list.php', - 'order' => 50, - 'name' => new \OC_L10N_String(new \OC_L10N('files_trashbin'), 'Deleted files', []), - 'active' => false, - 'icon' => '', - ], - ]); - - $expected = new Http\TemplateResponse( - 'files', - 'index', - [ - 'usedSpacePercent' => 123, - 'owner' => 'MyName', - 'ownerDisplayName' => 'MyDisplayName', - 'isPublic' => false, - 'defaultFileSorting' => 'name', - 'defaultFileSortingDirection' => 'asc', - 'showHiddenFiles' => false, - 'mailNotificationEnabled' => 'no', - 'mailPublicNotificationEnabled' => 'no', - 'allowShareWithLink' => 'yes', - 'appNavigation' => $nav, - 'appContents' => [ - [ - 'id' => 'files', - 'content' => null, - ], - [ - 'id' => 'favorites', - 'content' => null, - ], - [ - 'id' => 'sharingin', - 'content' => null, - ], - [ - 'id' => 'sharingout', - 'content' => null, - ], - [ - 'id' => 'sharinglinks', - 'content' => null, - ], - [ - 'id' => 'systemtagsfilter', - 'content' => null, - ], - [ - 'id' => 'trashbin', - 'content' => null, - ], - ], - ] - ); - $policy = new Http\ContentSecurityPolicy(); - $policy->addAllowedFrameDomain('\'self\''); - $expected->setContentSecurityPolicy($policy); - $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView')); - } - - public function showFileMethodProvider() { - return [ - [true], - [false], - ]; - } - - /** - * @dataProvider showFileMethodProvider - */ - public function testShowFileRouteWithFolder($useShowFile) { - $node = $this->getMock('\OCP\Files\Folder'); - $node->expects($this->once()) - ->method('getPath') - ->will($this->returnValue('/testuser1/files/test/sub')); - - $baseFolder = $this->getMock('\OCP\Files\Folder'); - - $this->rootFolder->expects($this->once()) - ->method('get') - ->with('testuser1/files/') - ->will($this->returnValue($baseFolder)); - - $baseFolder->expects($this->at(0)) - ->method('getById') - ->with(123) - ->will($this->returnValue([$node])); - $baseFolder->expects($this->at(1)) - ->method('getRelativePath') - ->with('/testuser1/files/test/sub') - ->will($this->returnValue('/test/sub')); - - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index', ['dir' => '/test/sub']) - ->will($this->returnValue('/apps/files/?dir=/test/sub')); - - $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub'); - if ($useShowFile) { - $this->assertEquals($expected, $this->viewController->showFile(123)); - } else { - $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); - } - } - - /** - * @dataProvider showFileMethodProvider - */ - public function testShowFileRouteWithFile($useShowFile) { - $parentNode = $this->getMock('\OCP\Files\Folder'); - $parentNode->expects($this->once()) - ->method('getPath') - ->will($this->returnValue('testuser1/files/test')); - - $baseFolder = $this->getMock('\OCP\Files\Folder'); - - $this->rootFolder->expects($this->once()) - ->method('get') - ->with('testuser1/files/') - ->will($this->returnValue($baseFolder)); - - $node = $this->getMock('\OCP\Files\File'); - $node->expects($this->once()) - ->method('getParent') - ->will($this->returnValue($parentNode)); - $node->expects($this->once()) - ->method('getName') - ->will($this->returnValue('somefile.txt')); - - $baseFolder->expects($this->at(0)) - ->method('getById') - ->with(123) - ->will($this->returnValue([$node])); - $baseFolder->expects($this->at(1)) - ->method('getRelativePath') - ->with('testuser1/files/test') - ->will($this->returnValue('/test')); - - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt']) - ->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt')); - - $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt'); - if ($useShowFile) { - $this->assertEquals($expected, $this->viewController->showFile(123)); - } else { - $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); - } - } - - /** - * @dataProvider showFileMethodProvider - */ - public function testShowFileRouteWithInvalidFileId($useShowFile) { - $baseFolder = $this->getMock('\OCP\Files\Folder'); - $this->rootFolder->expects($this->once()) - ->method('get') - ->with('testuser1/files/') - ->will($this->returnValue($baseFolder)); - - $baseFolder->expects($this->at(0)) - ->method('getById') - ->with(123) - ->will($this->returnValue([])); - - $expected = new Http\NotFoundResponse(); - if ($useShowFile) { - $this->assertEquals($expected, $this->viewController->showFile(123)); - } else { - $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); - } - } - - /** - * @dataProvider showFileMethodProvider - */ - public function testShowFileRouteWithTrashedFile($useShowFile) { - $this->appManager->expects($this->once()) - ->method('isEnabledForUser') - ->with('files_trashbin') - ->will($this->returnValue(true)); - - $parentNode = $this->getMock('\OCP\Files\Folder'); - $parentNode->expects($this->once()) - ->method('getPath') - ->will($this->returnValue('testuser1/files_trashbin/files/test.d1462861890/sub')); - - $baseFolderFiles = $this->getMock('\OCP\Files\Folder'); - $baseFolderTrash = $this->getMock('\OCP\Files\Folder'); - - $this->rootFolder->expects($this->at(0)) - ->method('get') - ->with('testuser1/files/') - ->will($this->returnValue($baseFolderFiles)); - $this->rootFolder->expects($this->at(1)) - ->method('get') - ->with('testuser1/files_trashbin/files/') - ->will($this->returnValue($baseFolderTrash)); - - $baseFolderFiles->expects($this->once()) - ->method('getById') - ->with(123) - ->will($this->returnValue([])); - - $node = $this->getMock('\OCP\Files\File'); - $node->expects($this->once()) - ->method('getParent') - ->will($this->returnValue($parentNode)); - $node->expects($this->once()) - ->method('getName') - ->will($this->returnValue('somefile.txt')); - - $baseFolderTrash->expects($this->at(0)) - ->method('getById') - ->with(123) - ->will($this->returnValue([$node])); - $baseFolderTrash->expects($this->at(1)) - ->method('getRelativePath') - ->with('testuser1/files_trashbin/files/test.d1462861890/sub') - ->will($this->returnValue('/test.d1462861890/sub')); - - $this->urlGenerator - ->expects($this->once()) - ->method('linkToRoute') - ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt']) - ->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt')); - - $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'); - if ($useShowFile) { - $this->assertEquals($expected, $this->viewController->showFile(123)); - } else { - $this->assertEquals($expected, $this->viewController->index('/whatever', '', '123')); - } - } -} diff --git a/apps/files/tests/controller/apicontrollertest.php b/apps/files/tests/controller/apicontrollertest.php deleted file mode 100644 index 2eba7d62feb..00000000000 --- a/apps/files/tests/controller/apicontrollertest.php +++ /dev/null @@ -1,399 +0,0 @@ - - * @author Lukas Reschke - * @author Morris Jobke - * @author Roeland Jago Douma - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -namespace OCA\Files\Controller; - -use OC\Files\FileInfo; -use OCP\AppFramework\Http; -use OCP\Files\NotFoundException; -use OCP\Files\StorageNotAvailableException; -use Test\TestCase; -use OCP\IRequest; -use OCA\Files\Service\TagService; -use OCP\AppFramework\Http\DataResponse; -use OCP\IPreview; -use OCP\Image; - -/** - * Class ApiController - * - * @package OCA\Files\Controller - */ -class ApiControllerTest extends TestCase { - /** @var string */ - private $appName = 'files'; - /** @var \OCP\IUser */ - private $user; - /** @var IRequest */ - private $request; - /** @var TagService */ - private $tagService; - /** @var IPreview */ - private $preview; - /** @var ApiController */ - private $apiController; - /** @var \OCP\Share\IManager */ - private $shareManager; - /** @var \OCP\IConfig */ - private $config; - - public function setUp() { - $this->request = $this->getMockBuilder('\OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->user = $this->getMock('\OCP\IUser'); - $this->user->expects($this->any()) - ->method('getUID') - ->will($this->returnValue('user1')); - $userSession = $this->getMock('\OCP\IUserSession'); - $userSession->expects($this->any()) - ->method('getUser') - ->will($this->returnValue($this->user)); - $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService') - ->disableOriginalConstructor() - ->getMock(); - $this->shareManager = $this->getMockBuilder('\OCP\Share\IManager') - ->disableOriginalConstructor() - ->getMock(); - $this->preview = $this->getMockBuilder('\OCP\IPreview') - ->disableOriginalConstructor() - ->getMock(); - $this->config = $this->getMock('\OCP\IConfig'); - - $this->apiController = new ApiController( - $this->appName, - $this->request, - $userSession, - $this->tagService, - $this->preview, - $this->shareManager, - $this->config - ); - } - - public function testGetFilesByTagEmpty() { - $tagName = 'MyTagName'; - $this->tagService->expects($this->once()) - ->method('getFilesByTag') - ->with($this->equalTo([$tagName])) - ->will($this->returnValue([])); - - $expected = new DataResponse(['files' => []]); - $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); - } - - public function testGetFilesByTagSingle() { - $tagName = 'MyTagName'; - $fileInfo = new FileInfo( - '/root.txt', - $this->getMockBuilder('\OC\Files\Storage\Storage') - ->disableOriginalConstructor() - ->getMock(), - '/var/www/root.txt', - [ - 'mtime' => 55, - 'mimetype' => 'application/pdf', - 'permissions' => 31, - 'size' => 1234, - 'etag' => 'MyEtag', - ], - $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') - ->disableOriginalConstructor() - ->getMock() - ); - $node = $this->getMockBuilder('\OC\Files\Node\File') - ->disableOriginalConstructor() - ->getMock(); - $node->expects($this->once()) - ->method('getFileInfo') - ->will($this->returnValue($fileInfo)); - $this->tagService->expects($this->once()) - ->method('getFilesByTag') - ->with($this->equalTo([$tagName])) - ->will($this->returnValue([$node])); - - $this->shareManager->expects($this->any()) - ->method('getSharesBy') - ->with( - $this->equalTo('user1'), - $this->anything(), - $node, - $this->equalTo(false), - $this->equalTo(1) - ) - ->will($this->returnCallback(function($userId, $shareType) { - if ($shareType === \OCP\Share::SHARE_TYPE_USER || $shareType === \OCP\Share::SHARE_TYPE_LINK) { - return ['dummy_share']; - } - return []; - })); - - $expected = new DataResponse([ - 'files' => [ - [ - 'id' => null, - 'parentId' => null, - 'mtime' => 55000, - 'name' => 'root.txt', - 'permissions' => 31, - 'mimetype' => 'application/pdf', - 'size' => 1234, - 'type' => 'file', - 'etag' => 'MyEtag', - 'path' => '/', - 'tags' => [ - [ - 'MyTagName' - ] - ], - 'shareTypes' => [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK] - ], - ], - ]); - $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); - } - - public function testGetFilesByTagMultiple() { - $tagName = 'MyTagName'; - $fileInfo1 = new FileInfo( - '/root.txt', - $this->getMockBuilder('\OC\Files\Storage\Storage') - ->disableOriginalConstructor() - ->getMock(), - '/var/www/root.txt', - [ - 'mtime' => 55, - 'mimetype' => 'application/pdf', - 'permissions' => 31, - 'size' => 1234, - 'etag' => 'MyEtag', - ], - $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') - ->disableOriginalConstructor() - ->getMock() - ); - $fileInfo2 = new FileInfo( - '/root.txt', - $this->getMockBuilder('\OC\Files\Storage\Storage') - ->disableOriginalConstructor() - ->getMock(), - '/var/www/some/sub.txt', - [ - 'mtime' => 999, - 'mimetype' => 'application/binary', - 'permissions' => 31, - 'size' => 9876, - 'etag' => 'SubEtag', - ], - $this->getMockBuilder('\OCP\Files\Mount\IMountPoint') - ->disableOriginalConstructor() - ->getMock() - ); - $node1 = $this->getMockBuilder('\OC\Files\Node\File') - ->disableOriginalConstructor() - ->getMock(); - $node1->expects($this->once()) - ->method('getFileInfo') - ->will($this->returnValue($fileInfo1)); - $node2 = $this->getMockBuilder('\OC\Files\Node\File') - ->disableOriginalConstructor() - ->getMock(); - $node2->expects($this->once()) - ->method('getFileInfo') - ->will($this->returnValue($fileInfo2)); - $this->tagService->expects($this->once()) - ->method('getFilesByTag') - ->with($this->equalTo([$tagName])) - ->will($this->returnValue([$node1, $node2])); - - $expected = new DataResponse([ - 'files' => [ - [ - 'id' => null, - 'parentId' => null, - 'mtime' => 55000, - 'name' => 'root.txt', - 'permissions' => 31, - 'mimetype' => 'application/pdf', - 'size' => 1234, - 'type' => 'file', - 'etag' => 'MyEtag', - 'path' => '/', - 'tags' => [ - [ - 'MyTagName' - ] - ], - ], - [ - 'id' => null, - 'parentId' => null, - 'mtime' => 999000, - 'name' => 'root.txt', - 'permissions' => 31, - 'mimetype' => 'application/binary', - 'size' => 9876, - 'type' => 'file', - 'etag' => 'SubEtag', - 'path' => '/', - 'tags' => [ - [ - 'MyTagName' - ] - ], - ] - ], - ]); - $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); - } - - public function testUpdateFileTagsEmpty() { - $expected = new DataResponse([]); - $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt')); - } - - public function testUpdateFileTagsWorking() { - $this->tagService->expects($this->once()) - ->method('updateFileTags') - ->with('/path.txt', ['Tag1', 'Tag2']); - - $expected = new DataResponse([ - 'tags' => [ - 'Tag1', - 'Tag2' - ], - ]); - $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); - } - - public function testUpdateFileTagsNotFoundException() { - $this->tagService->expects($this->once()) - ->method('updateFileTags') - ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new NotFoundException('My error message'))); - - $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); - $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); - } - - public function testUpdateFileTagsStorageNotAvailableException() { - $this->tagService->expects($this->once()) - ->method('updateFileTags') - ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new StorageNotAvailableException('My error message'))); - - $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE); - $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); - } - - public function testUpdateFileTagsStorageGenericException() { - $this->tagService->expects($this->once()) - ->method('updateFileTags') - ->with('/path.txt', ['Tag1', 'Tag2']) - ->will($this->throwException(new \Exception('My error message'))); - - $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND); - $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2'])); - } - - public function testGetThumbnailInvalidSize() { - $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST); - $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, '')); - } - - public function testGetThumbnailInvaidImage() { - $this->preview->expects($this->once()) - ->method('createPreview') - ->with('files/unknown.jpg', 10, 10, true) - ->willReturn(new Image); - $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND); - $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg')); - } - - public function testGetThumbnail() { - $this->preview->expects($this->once()) - ->method('createPreview') - ->with('files/known.jpg', 10, 10, true) - ->willReturn(new Image(\OC::$SERVERROOT.'/tests/data/testimage.jpg')); - - $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg'); - - $this->assertEquals(Http::STATUS_OK, $ret->getStatus()); - } - - public function testUpdateFileSorting() { - $mode = 'mtime'; - $direction = 'desc'; - - $this->config->expects($this->at(0)) - ->method('setUserValue') - ->with($this->user->getUID(), 'files', 'file_sorting', $mode); - $this->config->expects($this->at(1)) - ->method('setUserValue') - ->with($this->user->getUID(), 'files', 'file_sorting_direction', $direction); - - $expected = new HTTP\Response(); - $actual = $this->apiController->updateFileSorting($mode, $direction); - $this->assertEquals($expected, $actual); - } - - public function invalidSortingModeData() { - return [ - ['color', 'asc'], - ['name', 'size'], - ['foo', 'bar'] - ]; - } - - /** - * @dataProvider invalidSortingModeData - */ - public function testUpdateInvalidFileSorting($mode, $direction) { - $this->config->expects($this->never()) - ->method('setUserValue'); - - $expected = new Http\Response(null); - $expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); - - $result = $this->apiController->updateFileSorting($mode, $direction); - - $this->assertEquals($expected, $result); - } - - public function testShowHiddenFiles() { - $show = false; - - $this->config->expects($this->once()) - ->method('setUserValue') - ->with($this->user->getUID(), 'files', 'show_hidden', $show); - - $expected = new Http\Response(); - $actual = $this->apiController->showHiddenFiles($show); - - $this->assertEquals($expected, $actual); - } - -} diff --git a/apps/files/tests/helper.php b/apps/files/tests/helper.php deleted file mode 100644 index 654ec8332ed..00000000000 --- a/apps/files/tests/helper.php +++ /dev/null @@ -1,114 +0,0 @@ - - * @author Joas Schilling - * @author Morris Jobke - * @author Robin Appelman - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -use OCA\Files; - -/** - * Class Test_Files_Helper - */ -class Test_Files_Helper extends \Test\TestCase { - - private function makeFileInfo($name, $size, $mtime, $isDir = false) { - return new \OC\Files\FileInfo( - '/' . $name, - null, - '/', - array( - 'name' => $name, - 'size' => $size, - 'mtime' => $mtime, - 'type' => $isDir ? 'dir' : 'file', - 'mimetype' => $isDir ? 'httpd/unix-directory' : 'application/octet-stream' - ), - null - ); - } - - /** - * Returns a file list for testing - */ - private function getTestFileList() { - return array( - self::makeFileInfo('a.txt', 4, 2.3 * pow(10, 9)), - self::makeFileInfo('q.txt', 5, 150), - self::makeFileInfo('subdir2', 87, 128, true), - self::makeFileInfo('b.txt', 2.2 * pow(10, 9), 800), - self::makeFileInfo('o.txt', 12, 100), - self::makeFileInfo('subdir', 88, 125, true), - ); - } - - function sortDataProvider() { - return array( - array( - 'name', - false, - array('subdir', 'subdir2', 'a.txt', 'b.txt', 'o.txt', 'q.txt'), - ), - array( - 'name', - true, - array('q.txt', 'o.txt', 'b.txt', 'a.txt', 'subdir2', 'subdir'), - ), - array( - 'size', - false, - array('a.txt', 'q.txt', 'o.txt', 'subdir2', 'subdir', 'b.txt'), - ), - array( - 'size', - true, - array('b.txt', 'subdir', 'subdir2', 'o.txt', 'q.txt', 'a.txt'), - ), - array( - 'mtime', - false, - array('o.txt', 'subdir', 'subdir2', 'q.txt', 'b.txt', 'a.txt'), - ), - array( - 'mtime', - true, - array('a.txt', 'b.txt', 'q.txt', 'subdir2', 'subdir', 'o.txt'), - ), - ); - } - - /** - * @dataProvider sortDataProvider - */ - public function testSortByName($sort, $sortDescending, $expectedOrder) { - $files = self::getTestFileList(); - $files = \OCA\Files\Helper::sortFiles($files, $sort, $sortDescending); - $fileNames = array(); - foreach ($files as $fileInfo) { - $fileNames[] = $fileInfo->getName(); - } - $this->assertEquals( - $expectedOrder, - $fileNames - ); - } - -} diff --git a/apps/files/tests/service/tagservice.php b/apps/files/tests/service/tagservice.php deleted file mode 100644 index 5fcf64b1352..00000000000 --- a/apps/files/tests/service/tagservice.php +++ /dev/null @@ -1,129 +0,0 @@ - - * @author Thomas Müller - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ -namespace OCA\Files; - -use \OCA\Files\Service\TagService; - -/** - * Class TagServiceTest - * - * @group DB - * - * @package OCA\Files - */ -class TagServiceTest extends \Test\TestCase { - - /** - * @var string - */ - private $user; - - /** - * @var \OCP\Files\Folder - */ - private $root; - - /** - * @var \OCA\Files\Service\TagService - */ - private $tagService; - - /** - * @var \OCP\ITags - */ - private $tagger; - - protected function setUp() { - parent::setUp(); - $this->user = $this->getUniqueId('user'); - \OC::$server->getUserManager()->createUser($this->user, 'test'); - \OC_User::setUserId($this->user); - \OC_Util::setupFS($this->user); - /** - * @var \OCP\IUser - */ - $user = new \OC\User\User($this->user, null); - /** - * @var \OCP\IUserSession - */ - $userSession = $this->getMock('\OCP\IUserSession'); - $userSession->expects($this->any()) - ->method('getUser') - ->withAnyParameters() - ->will($this->returnValue($user)); - - $this->root = \OC::$server->getUserFolder(); - - $this->tagger = \OC::$server->getTagManager()->load('files'); - $this->tagService = new TagService( - $userSession, - $this->tagger, - $this->root - ); - } - - protected function tearDown() { - \OC_User::setUserId(''); - $user = \OC::$server->getUserManager()->get($this->user); - if ($user !== null) { $user->delete(); } - } - - public function testUpdateFileTags() { - $tag1 = 'tag1'; - $tag2 = 'tag2'; - - $subdir = $this->root->newFolder('subdir'); - $testFile = $subdir->newFile('test.txt'); - $testFile->putContent('test contents'); - - $fileId = $testFile->getId(); - - // set tags - $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2)); - - $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1)); - $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); - - // remove tag - $result = $this->tagService->updateFileTags('subdir/test.txt', array($tag2)); - $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); - $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); - - // clear tags - $result = $this->tagService->updateFileTags('subdir/test.txt', array()); - $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); - $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2)); - - // non-existing file - $caught = false; - try { - $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1)); - } catch (\OCP\Files\NotFoundException $e) { - $caught = true; - } - $this->assertTrue($caught); - - $subdir->delete(); - } -} - -- cgit v1.2.3