Skip to content
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
18 changes: 15 additions & 3 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\AppFramework\Http;

/**
* Class ViewController
Expand Down Expand Up @@ -282,12 +283,14 @@ public function showFile($fileId) {
$files = $baseFolder->getById($fileId);
$params = [];

$isFilesView = true;
if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
// Access files_trashbin if it exists
if ( $this->rootFolder->nodeExists($uid . '/files_trashbin/files/')) {
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$files = $baseFolder->getById($fileId);
$params['view'] = 'trashbin';
$isFilesView = false;
}
}

Expand All @@ -302,14 +305,23 @@ public function showFile($fileId) {
// and scroll to the entry
$params['scrollto'] = $file->getName();
}
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
$response = new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
if ($isFilesView) {
$webdavUrl = $this->urlGenerator->linkTo('', 'remote.php') . '/dav/files/' . $uid . '/';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PVince81 shouldn't this be endpoint-aware instead of hardcoded? i.e. using the same endpoint as the request that originated this $response? My endpoint-juggling context might be playing mind tricks on me, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endpoint-aware in what way ?

The ViewController is not part of Webdav but it's the web page from "index.php/apps/files" so what decision do you think should be made based on this ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's the web page from "index.php/apps/files"

You're right. I was thinking on a different (non-existent) workflow.

What I really meant was If there won't be a problem to use a hardcoded WebDAV path regardless of what the client that is accessing the private link normally uses/understands.

e.g. since this request came from the iOS team and they talk with the old endpoint, the new might offer different properties, no? Or if, like in the past; we detect some problems with the endpoint on the webUI that forces us to temporarily switch back to old, etc.

$webdavUrl .= ltrim($baseFolder->getRelativePath($file->getPath()), '/');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PVince81 take into account that the file name at this point is not URL-encoded: a file called level7$éâ .txt should point to remote.php/dav/files/user/Documents/level7$%C3%A9%C3%A2%20.txt but header is Webdav-Location: /remote.php/dav/files/user/Documents/level7$éâ .txt.

Clients will query a non-existent file then. We should add a test for it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I'll then add some encoding in a subsequent PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we go, please have a look: #30503

$response->addHeader('Webdav-Location', $webdavUrl);
}
return $response;
}

if ( $this->userSession->isLoggedIn() and empty($files)) {
if ($this->userSession->isLoggedIn() and empty($files)) {
$param["error"] = $this->l10n->t("You don't have permissions to access this file/folder - Please contact the owner to share it with you.");
return new TemplateResponse("core", 'error', ["errors" => [$param]], 'guest');
$response = new TemplateResponse("core", 'error', ["errors" => [$param]], 'guest');
$response->setStatus(Http::STATUS_NOT_FOUND);
return $response;
}

// FIXME: potentially dead code as the user is normally always logged in non-public routes
throw new \OCP\Files\NotFoundException();
}
}
77 changes: 57 additions & 20 deletions apps/files/tests/Controller/ViewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public function setUp() {
'renderScript'
])
->getMock();

$this->urlGenerator
->expects($this->any())
->method('linkTo')
->with('', 'remote.php')
->will($this->returnValue('/owncloud/remote.php'));
}

public function testIndexWithIE8RedirectAndDirDefined() {
Expand All @@ -114,9 +120,9 @@ public function testIndexWithIE8RedirectAndDirDefined() {
->expects($this->once())
->method('linkToRoute')
->with('files.view.index')
->will($this->returnValue('/apps/files/'));
->will($this->returnValue('/owncloud/index.php/apps/files/'));

$expected = new Http\RedirectResponse('/apps/files/#?dir=MyDir');
$expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/#?dir=MyDir');
$this->assertEquals($expected, $this->viewController->index('MyDir'));
}

Expand All @@ -130,9 +136,9 @@ public function testIndexWithIE8RedirectAndViewDefined() {
->expects($this->once())
->method('linkToRoute')
->with('files.view.index')
->will($this->returnValue('/apps/files/'));
->will($this->returnValue('/owncloud/index.php/apps/files/'));

$expected = new Http\RedirectResponse('/apps/files/#?dir=/&view=MyView');
$expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/#?dir=/&view=MyView');
$this->assertEquals($expected, $this->viewController->index('', 'MyView'));
}

Expand All @@ -146,9 +152,9 @@ public function testIndexWithIE8RedirectAndViewAndDirDefined() {
->expects($this->once())
->method('linkToRoute')
->with('files.view.index')
->will($this->returnValue('/apps/files/'));
->will($this->returnValue('/owncloud/index.php/apps/files/'));

$expected = new RedirectResponse('/apps/files/#?dir=MyDir&view=MyView');
$expected = new RedirectResponse('/owncloud/index.php/apps/files/#?dir=MyDir&view=MyView');
$this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
}

Expand Down Expand Up @@ -321,7 +327,7 @@ public function showFileMethodProvider() {
*/
public function testShowFileRouteWithFolder($useShowFile) {
$node = $this->createMock('\OCP\Files\Folder');
$node->expects($this->once())
$node->expects($this->any())
->method('getPath')
->will($this->returnValue('/testuser1/files/test/sub'));

Expand All @@ -332,11 +338,11 @@ public function testShowFileRouteWithFolder($useShowFile) {
->with('testuser1/files/')
->will($this->returnValue($baseFolder));

$baseFolder->expects($this->at(0))
$baseFolder->expects($this->any())
->method('getById')
->with(123)
->will($this->returnValue([$node]));
$baseFolder->expects($this->at(1))
$baseFolder->expects($this->any())
->method('getRelativePath')
->with('/testuser1/files/test/sub')
->will($this->returnValue('/test/sub'));
Expand All @@ -345,9 +351,10 @@ public function testShowFileRouteWithFolder($useShowFile) {
->expects($this->once())
->method('linkToRoute')
->with('files.view.index', ['dir' => '/test/sub'])
->will($this->returnValue('/apps/files/?dir=/test/sub'));
->will($this->returnValue('/owncloud/index.php/apps/files/?dir=/test/sub'));

$expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
$expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?dir=/test/sub');
$expected->addHeader('Webdav-Location', '/owncloud/remote.php/dav/files/testuser1/test/sub');
if ($useShowFile) {
$this->assertEquals($expected, $this->viewController->showFile(123));
} else {
Expand All @@ -360,7 +367,7 @@ public function testShowFileRouteWithFolder($useShowFile) {
*/
public function testShowFileRouteWithFile($useShowFile) {
$parentNode = $this->createMock('\OCP\Files\Folder');
$parentNode->expects($this->once())
$parentNode->expects($this->any())
->method('getPath')
->will($this->returnValue('testuser1/files/test'));

Expand All @@ -378,23 +385,29 @@ public function testShowFileRouteWithFile($useShowFile) {
$node->expects($this->once())
->method('getName')
->will($this->returnValue('somefile.txt'));
$node->expects($this->any())
->method('getPath')
->will($this->returnValue('testuser1/files/test/somefile.txt'));

$baseFolder->expects($this->at(0))
$baseFolder->expects($this->any())
->method('getById')
->with(123)
->will($this->returnValue([$node]));
$baseFolder->expects($this->at(1))
$baseFolder->expects($this->any())
->method('getRelativePath')
->with('testuser1/files/test')
->will($this->returnValue('/test'));
->will($this->returnValueMap([
['testuser1/files/test', '/test'],
['testuser1/files/test/somefile.txt', '/test/somefile.txt'],
]));

$this->urlGenerator
->expects($this->once())
->method('linkToRoute')
->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
->will($this->returnValue('/apps/files/?dir=/test/sub&scrollto=somefile.txt'));
->will($this->returnValue('/owncloud/index.php/apps/files/?dir=/test&scrollto=somefile.txt'));

$expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
$expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?dir=/test&scrollto=somefile.txt');
$expected->addHeader('Webdav-Location', '/owncloud/remote.php/dav/files/testuser1/test/somefile.txt');
if ($useShowFile) {
$this->assertEquals($expected, $this->viewController->showFile(123));
} else {
Expand Down Expand Up @@ -428,6 +441,30 @@ public function testShowFileRouteWithInvalidFileId($useShowFile) {
}
}

/**
*/
public function testShowFileRouteWithInvalidFileIdLoggedIn() {
$baseFolder = $this->createMock('\OCP\Files\Folder');
$this->rootFolder->expects($this->once())
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolder));

$baseFolder->expects($this->at(0))
->method('getById')
->with(123)
->will($this->returnValue([]));

$this->userSession->expects($this->any())
->method('isLoggedIn')
->will($this->returnValue(true));

$response = $this->viewController->index('MyDir', 'MyView', '123');
$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $response);
$params = $response->getParams();
$this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
}

/**
* @dataProvider showFileMethodProvider
*/
Expand Down Expand Up @@ -485,9 +522,9 @@ public function testShowFileRouteWithTrashedFile($useShowFile) {
->expects($this->once())
->method('linkToRoute')
->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'));
->will($this->returnValue('/owncloud/index.php/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'));

$expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
$expected = new Http\RedirectResponse('/owncloud/index.php/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
if ($useShowFile) {
$this->assertEquals($expected, $this->viewController->showFile(123));
} else {
Expand Down