diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index 92f54b9f8f28..1ff7208de581 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -75,15 +75,11 @@ public function __construct(array $urlParams= []) { /** * 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 + $c->query('ServerContainer')->getTagManager(), + $c->query('ServerContainer')->getLazyRootFolder() ); }); diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php index 76f5fac5c7e5..3ed8f9a1d579 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -23,6 +23,8 @@ */ namespace OCA\Files\Service; +use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException; +use OCP\Files\IRootFolder; /** * Service class to manage tags on files. @@ -35,23 +37,23 @@ class TagService { private $userSession; /** - * @var \OCP\ITags + * @var \OCP\ITagManager */ - private $tagger; + private $tagManager; /** - * @var \OCP\Files\Folder + * @var IRootFolder */ - private $homeFolder; + private $rootFolder; public function __construct( \OCP\IUserSession $userSession, - \OCP\ITags $tagger, - \OCP\Files\Folder $homeFolder + \OCP\ITagManager $tagManager, + IRootFolder $rootFolder ) { $this->userSession = $userSession; - $this->tagger = $tagger; - $this->homeFolder = $homeFolder; + $this->tagManager = $tagManager; + $this->rootFolder = $rootFolder; } /** @@ -60,14 +62,20 @@ public function __construct( * replace the actual tag selection. * * @param string $path path - * @param array $tags array of tags + * @param array $tags array of tags * @return array list of tags * @throws \OCP\Files\NotFoundException if the file does not exist + * @throws NotLoggedInException */ public function updateFileTags($path, $tags) { - $fileId = $this->homeFolder->get($path)->getId(); + $user = $this->userSession->getUser(); + if ($user === null) { + throw new NotLoggedInException(); + } + $fileId = $this->rootFolder->getUserFolder($user->getUID())->get($path)->getId(); + $tagger = $this->tagManager->load('files'); - $currentTags = $this->tagger->getTagsForObjects([$fileId]); + $currentTags = $tagger->getTagsForObjects([$fileId]); if (!empty($currentTags)) { $currentTags = current($currentTags); @@ -75,11 +83,11 @@ public function updateFileTags($path, $tags) { $newTags = array_diff($tags, $currentTags); foreach ($newTags as $tag) { - $this->tagger->tagAs($fileId, $tag); + $tagger->tagAs($fileId, $tag); } $deletedTags = array_diff($currentTags, $tags); foreach ($deletedTags as $tag) { - $this->tagger->unTag($fileId, $tag); + $tagger->unTag($fileId, $tag); } // TODO: re-read from tagger to make sure the diff --git a/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php index 89f3d14fde87..734618521f92 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -24,6 +24,7 @@ namespace OCA\Files\Tests\Service; use OCA\Files\Service\TagService; +use OCP\Files\IRootFolder; use OCP\IUserSession; use Test\Traits\UserTrait; @@ -44,7 +45,7 @@ class TagServiceTest extends \Test\TestCase { private $user; /** - * @var \OCP\Files\Folder + * @var IRootFolder */ private $root; @@ -54,9 +55,9 @@ class TagServiceTest extends \Test\TestCase { private $tagService; /** - * @var \OCP\ITags + * @var \OCP\ITagManager */ - private $tagger; + private $tagManager; protected function setUp() { parent::setUp(); @@ -71,12 +72,12 @@ protected function setUp() { ->withAnyParameters() ->will($this->returnValue($user)); - $this->root = \OC::$server->getUserFolder(); + $this->root = \OC::$server->getLazyRootFolder(); - $this->tagger = \OC::$server->getTagManager()->load('files'); + $this->tagManager = \OC::$server->getTagManager(); $this->tagService = new TagService( $userSession, - $this->tagger, + $this->tagManager, $this->root ); } @@ -90,7 +91,7 @@ public function testUpdateFileTags() { $tag1 = 'tag1'; $tag2 = 'tag2'; - $subdir = $this->root->newFolder('subdir'); + $subdir = $this->root->getUserFolder($this->user)->newFolder('subdir'); $testFile = $subdir->newFile('test.txt'); $testFile->putContent('test contents'); @@ -98,19 +99,20 @@ public function testUpdateFileTags() { // set tags $this->tagService->updateFileTags('subdir/test.txt', [$tag1, $tag2]); + $tags = $this->tagManager->load('files'); - $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag1)); - $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2)); + $this->assertEquals([$fileId], $tags->getIdsForTag($tag1)); + $this->assertEquals([$fileId], $tags->getIdsForTag($tag2)); // remove tag $this->tagService->updateFileTags('subdir/test.txt', [$tag2]); - $this->assertEquals([], $this->tagger->getIdsForTag($tag1)); - $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2)); + $this->assertEquals([], $tags->getIdsForTag($tag1)); + $this->assertEquals([$fileId], $tags->getIdsForTag($tag2)); // clear tags $this->tagService->updateFileTags('subdir/test.txt', []); - $this->assertEquals([], $this->tagger->getIdsForTag($tag1)); - $this->assertEquals([], $this->tagger->getIdsForTag($tag2)); + $this->assertEquals([], $tags->getIdsForTag($tag1)); + $this->assertEquals([], $tags->getIdsForTag($tag2)); // non-existing file $caught = false; diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index f753e35bc86e..7cdea6b668ba 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -26,11 +26,16 @@ */ namespace OC; +use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException; +use OCP\Files\IRootFolder; +use OCP\IConfig; +use OCP\IImage; use OCP\IPreview; +use OCP\IUserSession; use OCP\Preview\IProvider; class PreviewManager implements IPreview { - /** @var \OCP\IConfig */ + /** @var IConfig */ protected $config; /** @var bool */ @@ -48,13 +53,23 @@ class PreviewManager implements IPreview { /** @var array */ protected $defaultProviders; + /** @var IRootFolder */ + private $rootFolder; + + /** @var IUserSession */ + private $userSession; + /** * Constructor * - * @param \OCP\IConfig $config + * @param IConfig $config + * @param IRootFolder $rootFolder + * @param IUserSession $userSession */ - public function __construct(\OCP\IConfig $config) { + public function __construct(IConfig $config, IRootFolder $rootFolder, IUserSession $userSession) { $this->config = $config; + $this->rootFolder = $rootFolder; + $this->userSession = $userSession; } /** @@ -114,10 +129,18 @@ public function hasProviders() { * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly - * @return \OCP\IImage + * @return IImage + * @throws NotLoggedInException + * @throws \OCP\Files\NotFoundException + * @throws \Exception */ public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { - $preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp); + $user = $this->userSession->getUser(); + if ($user === null) { + throw new NotLoggedInException(); + } + $file = $this->rootFolder->getUserFolder($user->getUID())->get($file); + $preview = new Preview('', '/', $file, $maxX, $maxY, $scaleUp); return $preview->getPreview(); } diff --git a/lib/private/Server.php b/lib/private/Server.php index d16ed466cdd7..e850589f54a3 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -159,7 +159,9 @@ public function __construct($webRoot, \OC\Config $config) { }); $this->registerService('PreviewManager', function (Server $c) { - return new PreviewManager($c->getConfig()); + return new PreviewManager($c->getConfig(), + $c->getLazyRootFolder(), + $c->getUserSession()); }); $this->registerService('EncryptionManager', function (Server $c) { diff --git a/tests/lib/PreviewTest.php b/tests/lib/PreviewTest.php index a620ae1fb298..2c49823370f3 100644 --- a/tests/lib/PreviewTest.php +++ b/tests/lib/PreviewTest.php @@ -27,6 +27,8 @@ use OC\Files\Storage\Temporary; use OC\Files\View; use OC\Preview; +use OC\PreviewManager; +use OC\Server; use Test\Traits\MountProviderTrait; use Test\Traits\UserTrait; @@ -107,6 +109,14 @@ protected function setUp() { \OC::$server->getConfig() ->setSystemValue('enabledPreviewProviders', $providers); + //re-initialize the preview manager due to config change above + unset(\OC::$server['PreviewManager']); + \OC::$server->registerService('PreviewManager', function ($c) { + /** @var Server $c */ + return new PreviewManager($c->getConfig(), $c->getLazyRootFolder(), $c->getUserSession()); + }); + + // Sample is 1680x1050 JPEG $this->prepareSample('testimage.jpg', 1680, 1050); // Sample is 2400x1707 EPS