Skip to content
45 changes: 4 additions & 41 deletions lib/Controller/APIv2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace OCA\Activity\Controller;

use OC\Files\View;
use OCA\Activity\Data;
use OCA\Activity\Exception\InvalidFilterException;
use OCA\Activity\GroupHelper;
Expand Down Expand Up @@ -93,28 +92,9 @@ class APIv2Controller extends OCSController {
/** @var IMimeTypeDetector */
protected $mimeTypeDetector;

/** @var View */
protected $view;

/** @var ViewInfoCache */
protected $infoCache;

/**
* OCSEndPoint constructor.
*
* @param string $appName
* @param IRequest $request
* @param IManager $activityManager
* @param Data $data
* @param GroupHelper $helper
* @param UserSettings $settings
* @param IURLGenerator $urlGenerator
* @param IUserSession $userSession
* @param IPreview $preview
* @param IMimeTypeDetector $mimeTypeDetector
* @param View $view
* @param ViewInfoCache $infoCache
*/
public function __construct($appName,
IRequest $request,
IManager $activityManager,
Expand All @@ -125,7 +105,6 @@ public function __construct($appName,
IUserSession $userSession,
IPreview $preview,
IMimeTypeDetector $mimeTypeDetector,
View $view,
ViewInfoCache $infoCache) {
parent::__construct($appName, $request);
$this->activityManager = $activityManager;
Expand All @@ -136,7 +115,6 @@ public function __construct($appName,
$this->userSession = $userSession;
$this->preview = $preview;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->view = $view;
$this->infoCache = $infoCache;
}

Expand Down Expand Up @@ -357,7 +335,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
}

$preview = [
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']),
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]),
'source' => '',
'mimeType' => 'application/octet-stream',
'isMimeTypeIcon' => true,
Expand All @@ -371,8 +349,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
$preview['source'] = $this->getPreviewPathFromMimeType('dir');
$preview['mimeType'] = 'dir';
} else {
$this->view->chroot('/' . $owner . '/files');
$fileInfo = $this->view->getFileInfo($info['path']);
$fileInfo = $info['node'] ?? null;
if (!($fileInfo instanceof FileInfo)) {
$preview = $this->getPreviewFromPath($fileId, $filePath, $info);
} elseif ($this->preview->isAvailable($fileInfo)) {
Expand All @@ -382,7 +359,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
'x' => 250,
'y' => 250,
'fileId' => $fileId,
'c' => $fileInfo->getEtag()
'c' => $fileInfo->getEtag(),
];

$preview['source'] = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', $params);
Expand All @@ -400,7 +377,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
protected function getPreviewFromPath(int $fileId, string $filePath, array $info): array {
$mimeType = $info['is_dir'] ? 'dir' : $this->mimeTypeDetector->detectPath($filePath);
return [
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']),
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]),
'source' => $this->getPreviewPathFromMimeType($mimeType),
'mimeType' => $mimeType,
'isMimeTypeIcon' => true,
Expand All @@ -418,18 +395,4 @@ protected function getPreviewPathFromMimeType(string $mimeType): string {

return $this->urlGenerator->getAbsoluteURL($mimeTypeIcon);
}

protected function getPreviewLink(string $path, bool $isDir, string $view): string {
$params = [
'dir' => $path,
];
if (!$isDir) {
$params['dir'] = (substr_count($path, '/') === 1) ? '/' : \dirname($path);
$params['scrollto'] = basename($path);
}
if ($view !== '') {
$params['view'] = $view;
}
return $this->urlGenerator->linkToRouteAbsolute('files.view.index', $params);
}
}
83 changes: 30 additions & 53 deletions lib/ViewInfoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

namespace OCA\Activity;

use OC\Files\View;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;

class ViewInfoCache {
Expand All @@ -32,27 +34,11 @@ class ViewInfoCache {
/** @var array */
protected $cacheId;

/** @var \OC\Files\View */
protected $view;
/** @var IRootFolder */
protected $rootFolder;

/**
* @param View $view
*/
public function __construct(View $view) {
$this->view = $view;
}

/**
* @param string $user
* @param string $path
* @return array
*/
public function getInfoByPath($user, $path) {
if (isset($this->cachePath[$user][$path])) {
return $this->cachePath[$user][$path];
}

return $this->findInfoByPath($user, $path);
public function __construct(IRootFolder $rootFolder) {
$this->rootFolder = $rootFolder;
}

/**
Expand All @@ -73,35 +59,13 @@ public function getInfoById($user, $fileId, $path) {
return $this->findInfoById($user, $fileId, $path);
}

/**
* @param string $user
* @param string $path
* @return array
*/
protected function findInfoByPath($user, $path) {
$this->view->chroot('/' . $user . '/files');

$exists = $this->view->file_exists($path);

$this->cachePath[$user][$path] = [
'path' => $path,
'exists' => $exists,
'is_dir' => $exists ? (bool)$this->view->is_dir($path) : false,
'view' => '',
];

return $this->cachePath[$user][$path];
}

/**
* @param string $user
* @param int $fileId
* @param string $filePath
* @return array
*/
protected function findInfoById($user, $fileId, $filePath) {
$this->view->chroot('/' . $user . '/files');

$cache = [
'path' => $filePath,
'exists' => false,
Expand All @@ -111,24 +75,37 @@ protected function findInfoById($user, $fileId, $filePath) {

$notFound = false;
try {
$path = $this->view->getPath($fileId);
$userFolder = $this->rootFolder->getUserFolder($user);
$entries = $userFolder->getById($fileId);
if (empty($entries)) {
throw new NotFoundException('No entries returned');
}
/** @var Node $entry */
$entry = array_shift($entries);

$cache['path'] = $path;
$cache['is_dir'] = $this->view->is_dir($path);
$cache['path'] = substr($entry->getPath(), strlen($userFolder->getPath()));
$cache['is_dir'] = $entry instanceof Folder;
$cache['exists'] = true;
$cache['node'] = $entry;
} catch (NotFoundException $e) {
// The file was not found in the normal view, maybe it is in
// the trashbin?
$this->view->chroot('/' . $user . '/files_trashbin');

// The file was not found in the normal view,
// maybe it is in the trashbin?
try {
$path = $this->view->getPath($fileId);
$userTrashBin = $this->rootFolder->get('/' . $user . '/files_trashbin');
$entries = $userTrashBin->getById($fileId);
if (empty($entries)) {
throw new NotFoundException('No entries returned');
}

/** @var Node $entry */
$entry = array_shift($entries);

$cache = [
'path' => substr($path, strlen('/files')),
'path' => substr($entry->getPath(), strlen($userTrashBin->getPath())),
'exists' => true,
'is_dir' => (bool)$this->view->is_dir($path),
'is_dir' => $entry instanceof Folder,
'view' => 'trashbin',
'node' => $entry,
];
} catch (NotFoundException $e) {
$notFound = true;
Expand Down
Loading