Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Fix ApiContoller initialization #31104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
});

Expand Down
34 changes: 21 additions & 13 deletions apps/files/lib/Service/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -60,26 +62,32 @@ 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);
}

$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
Expand Down
28 changes: 15 additions & 13 deletions apps/files/tests/Service/TagServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,7 +45,7 @@ class TagServiceTest extends \Test\TestCase {
private $user;

/**
* @var \OCP\Files\Folder
* @var IRootFolder
*/
private $root;

Expand All @@ -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();
Expand All @@ -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
);
}
Expand All @@ -90,27 +91,28 @@ 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');

$fileId = $testFile->getId();

// 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;
Expand Down
33 changes: 28 additions & 5 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/PreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down