/** * @param IEvent $event * @param string $parameter The parameter to be formatted * @return string The formatted parameter */ public function format(IEvent $event, $parameter) { $param = $this->fixLegacyFilename($parameter); // If the activity is about the very same file, we use the current path // for the link generation instead of the one that was saved. $fileId = ''; if (is_array($param)) { $fileId = key($param); $param = $param[$fileId]; $info = $this->infoCache->getInfoById($this->user, $fileId, $param); } elseif ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { $fileId = $event->getObjectId(); $info = $this->infoCache->getInfoById($this->user, $fileId, $param); } else { $info = $this->infoCache->getInfoByPath($this->user, $param); } if ($info['is_dir']) { $linkData = ['dir' => $info['path']]; } else { $parentDir = substr_count($info['path'], '/') === 1 ? '/' : dirname($info['path']); $fileName = basename($info['path']); $linkData = ['dir' => $parentDir, 'scrollto' => $fileName]; } if ($info['view'] !== '') { $linkData['view'] = $info['view']; } $param = trim($param, '/'); $fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData); return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>'; }
/** * @dataProvider dataGetPreview * * @param string $author * @param int $fileId * @param string $path * @param string $returnedPath * @param bool $isDir * @param bool $isMimeSup * @param string $source * @param bool $isMimeTypeIcon */ public function testGetPreview($author, $fileId, $path, $returnedPath, $isDir, $isMimeSup, $source, $isMimeTypeIcon) { $controller = $this->getController(['getPreviewLink', 'getPreviewPathFromMimeType']); $this->infoCache->expects($this->once())->method('getInfoById')->with($author, $fileId, $path)->willReturn(['path' => $returnedPath, 'exists' => true, 'is_dir' => $isDir, 'view' => '']); $controller->expects($this->once())->method('getPreviewLink')->with($returnedPath, $isDir)->willReturnCallback(function ($path) { return '/preview' . $path; }); if ($isDir) { $controller->expects($this->once())->method('getPreviewPathFromMimeType')->with('dir')->willReturn('/preview/dir'); } else { $fileInfo = $this->getMockBuilder('OCP\\Files\\FileInfo')->disableOriginalConstructor()->getMock(); $this->view->expects($this->once())->method('chroot')->with('/' . $author . '/files'); $this->view->expects($this->once())->method('getFileInfo')->with($returnedPath)->willReturn($fileInfo); $this->preview->expects($this->once())->method('isAvailable')->with($fileInfo)->willReturn($isMimeSup); if (!$isMimeSup) { $fileInfo->expects($this->once())->method('getMimetype')->willReturn('audio/mp3'); $controller->expects($this->once())->method('getPreviewPathFromMimeType')->with('audio/mp3')->willReturn('/preview/mpeg'); } else { $this->urlGenerator->expects($this->once())->method('linkToRoute')->with('core_ajax_preview', $this->anything())->willReturnCallback(function () use($returnedPath) { return '/preview' . $returnedPath; }); } } $this->assertSame(['link' => '/preview' . $returnedPath, 'source' => $source, 'isMimeTypeIcon' => $isMimeTypeIcon], $this->invokePrivate($controller, 'getPreview', [$author, $fileId, $path])); }
/** * @dataProvider dataFormat * * @param string $user * @param string $parameter * @param bool $isDir * @param array $info * @param bool $allowHtml * @param bool $verbose * @param string $expected */ public function testFormat($user, $parameter, $isDir, array $info, $allowHtml, $verbose, $expected) { /** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */ $event = $this->getMockBuilder('OCP\\Activity\\IEvent')->disableOriginalConstructor()->getMock(); if (!empty($info)) { $event->expects($this->once())->method('getObjectType')->willReturn('files'); $event->expects($this->once())->method('getObjectName')->willReturn($parameter); $event->expects($this->once())->method('getObjectId')->willReturn(42); } $this->urlGenerator->expects($allowHtml ? $this->once() : $this->never())->method('linkTo')->with('files', 'index.php', $this->anything())->willReturnCallback(function ($app, $file, $parameters) { $paramList = []; foreach ($parameters as $key => $value) { $paramList[] = $key . '=' . urlencode($value); } return $app . '/' . $file . '?' . implode('&', $paramList); }); $formatter = $this->getFormatter(['fixLegacyFilename'], $user); $formatter->expects($this->once())->method('fixLegacyFilename')->willReturnArgument(0); if (!empty($info)) { $this->infoCache->expects($this->once())->method('getInfoById')->with($user, 42, $parameter)->willReturn($info); } else { $this->infoCache->expects($this->once())->method('getInfoByPath')->with($user, $parameter)->willReturn(['path' => $parameter, 'is_dir' => $isDir, 'exists' => true, 'view' => '']); } $this->assertSame($expected, $formatter->format($event, $parameter, $allowHtml, $verbose)); }
/** * @param string $owner * @param int $fileId * @param string $filePath * @return array */ protected function getPreview($owner, $fileId, $filePath) { $info = $this->infoCache->getInfoById($owner, $fileId, $filePath); if (!$info['exists'] || $info['view'] !== '') { return $this->getPreviewFromPath($filePath, $info); } $preview = ['link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']), 'source' => '', 'isMimeTypeIcon' => true]; // show a preview image if the file still exists if ($info['is_dir']) { $preview['source'] = $this->getPreviewPathFromMimeType('dir'); } else { $this->view->chroot('/' . $owner . '/files'); $fileInfo = $this->view->getFileInfo($info['path']); if (!$fileInfo instanceof FileInfo) { $pathPreview = $this->getPreviewFromPath($filePath, $info); $preview['source'] = $pathPreview['source']; } else { if ($this->preview->isAvailable($fileInfo)) { $preview['isMimeTypeIcon'] = false; $preview['source'] = $this->urlGenerator->linkToRoute('core_ajax_preview', ['file' => $info['path'], 'c' => $this->view->getETag($info['path']), 'x' => 150, 'y' => 150]); } else { $preview['source'] = $this->getPreviewPathFromMimeType($fileInfo->getMimetype()); } } } return $preview; }
/** * @param IEvent $event * @param string $parameter The parameter to be formatted * @param bool $allowHtml Should HTML be used to format the parameter? * @param bool $verbose Should paths, names, etc be shortened or full length * @return string The formatted parameter */ public function format(IEvent $event, $parameter, $allowHtml, $verbose = false) { $param = $this->fixLegacyFilename($parameter); // If the activity is about the very same file, we use the current path // for the link generation instead of the one that was saved. $fileId = ''; if ($event->getObjectType() === 'files' && $event->getObjectName() === $param) { $fileId = $event->getObjectId(); $info = $this->infoCache->getInfoById($this->user, $fileId, $param); } else { $info = $this->infoCache->getInfoByPath($this->user, $param); } if ($info['is_dir']) { $linkData = ['dir' => $info['path']]; } else { $parentDir = substr_count($info['path'], '/') === 1 ? '/' : dirname($info['path']); $fileName = basename($info['path']); $linkData = ['dir' => $parentDir, 'scrollto' => $fileName]; } if ($info['view'] !== '') { $linkData['view'] = $info['view']; } $param = trim($param, '/'); list($path, $name) = $this->splitPathFromFilename($param); $fileLink = $this->urlGenerator->linkTo('files', 'index.php', $linkData); if ($allowHtml === null) { return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>'; } if ($verbose || $path === '') { if (!$allowHtml) { return $param; } return '<a class="filename" href="' . $fileLink . '">' . Util::sanitizeHTML($param) . '</a>'; } if (!$allowHtml) { return $name; } $title = ' title="' . $this->l->t('in %s', array(Util::sanitizeHTML($path))) . '"'; return '<a class="filename has-tooltip" href="' . $fileLink . '"' . $title . '>' . Util::sanitizeHTML($name) . '</a>'; }