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(); }
/** * Get all files for the given tag * * @param array $tagName tag name to filter by * @return FileInfo[] 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 []; } $fileInfos = []; foreach ($fileIds as $fileId) { $nodes = $this->homeFolder->getById((int) $fileId); foreach ($nodes as $node) { /** @var \OC\Files\Node\Node $node */ $fileInfos[] = $node->getFileInfo(); } } return $fileInfos; }
/** * 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; }