[Share 2.0] Make link share download use share manager

This commit is contained in:
Roeland Jago Douma 2016-01-15 11:49:50 +01:00
parent 717697313b
commit 8734ebe505
3 changed files with 79 additions and 72 deletions

View File

@ -54,7 +54,8 @@ class Application extends App {
$server->getActivityManager(), $server->getActivityManager(),
$c->query('ShareManager'), $c->query('ShareManager'),
$c->query('Session'), $c->query('Session'),
$server->getPreviewManager() $server->getPreviewManager(),
$c->query('RootFolder')
); );
}); });
$container->registerService('ExternalSharesController', function (SimpleContainer $c) { $container->registerService('ExternalSharesController', function (SimpleContainer $c) {
@ -69,6 +70,9 @@ class Application extends App {
/** /**
* Core class wrappers * Core class wrappers
*/ */
$container->registerService('RootFolder', function(SimpleContainer $c) use ($server) {
return $server->getRootFolder();
});
$container->registerService('Session', function(SimpleContainer $c) use ($server) { $container->registerService('Session', function(SimpleContainer $c) use ($server) {
return $server->getSession(); return $server->getSession();
}); });

View File

@ -52,6 +52,7 @@ use OCP\Util;
use OCA\Files_Sharing\Activity; use OCA\Files_Sharing\Activity;
use \OCP\Files\NotFoundException; use \OCP\Files\NotFoundException;
use \OC\Share20\IShare; use \OC\Share20\IShare;
use OCP\Files\IRootFolder;
/** /**
* Class ShareController * Class ShareController
@ -76,6 +77,8 @@ class ShareController extends Controller {
protected $session; protected $session;
/** @var IPreview */ /** @var IPreview */
protected $previewManager; protected $previewManager;
/** @var IRootFolder */
protected $rootFolder;
/** /**
* @param string $appName * @param string $appName
@ -98,7 +101,8 @@ class ShareController extends Controller {
\OCP\Activity\IManager $activityManager, \OCP\Activity\IManager $activityManager,
\OC\Share20\Manager $shareManager, \OC\Share20\Manager $shareManager,
ISession $session, ISession $session,
IPreview $previewManager) { IPreview $previewManager,
IRootFolder $rootFolder) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->config = $config; $this->config = $config;
@ -109,6 +113,7 @@ class ShareController extends Controller {
$this->shareManager = $shareManager; $this->shareManager = $shareManager;
$this->session = $session; $this->session = $session;
$this->previewManager = $previewManager; $this->previewManager = $previewManager;
$this->rootFolder = $rootFolder;
} }
/** /**
@ -286,14 +291,12 @@ class ShareController extends Controller {
public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') {
\OC_User::setIncognitoMode(true); \OC_User::setIncognitoMode(true);
$linkItem = OCP\Share::getShareByToken($token, false); $share = $this->shareManager->getShareByToken($token);
// Share is password protected - check whether the user is permitted to access the share // Share is password protected - check whether the user is permitted to access the share
if (isset($linkItem['share_with'])) { if ($share->getPassword() !== null && !$this->linkShareAuth($share)) {
if(!Helper::authenticate($linkItem)) { return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', ['token' => $token]));
array('token' => $token)));
}
} }
$files_list = null; $files_list = null;
@ -301,41 +304,74 @@ class ShareController extends Controller {
$files_list = json_decode($files); $files_list = json_decode($files);
// in case we get only a single file // in case we get only a single file
if ($files_list === null) { if ($files_list === null) {
$files_list = array($files); $files_list = [$files];
} }
} }
$originalSharePath = self::getPath($token); $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()->getUID());
$originalSharePath = $userFolder->getRelativePath($share->getPath()->getPath());
// Create the activities // Single file share
if (isset($originalSharePath) && Filesystem::isReadable($originalSharePath . $path)) { if ($share->getPath() instanceof \OCP\Files\File) {
$originalSharePath = Filesystem::normalizePath($originalSharePath . $path); $this->activityManager->publishActivity(
$isDir = \OC\Files\Filesystem::is_dir($originalSharePath); 'files_sharing', Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$originalSharePath], '', [],
$originalSharePath, '', $share->getShareOwner()->getUID(), Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
);
}
// Directory share
else {
/** @var \OCP\Files\Folder $node */
$node = $share->getPath();
$activities = []; // Try to get the path
if (!$isDir) { if ($path !== '') {
// Single file public share try {
$activities[$originalSharePath] = Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; $node = $node->get($path);
} catch (NotFoundException $e) {
return new NotFoundResponse();
}
}
$originalSharePath = $userFolder->getRelativePath($node->getPath());
if ($node instanceof \OCP\Files\File) {
// Single file download
$this->activityManager->publishActivity(
'files_sharing', Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$originalSharePath], '', [],
$originalSharePath, '', $share->getShareOwner()->getUID(), Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
);
} else if (!empty($files_list)) { } else if (!empty($files_list)) {
// Only some files are downloaded /** @var \OCP\Files\Folder $node */
// Subset of files is downloaded
foreach ($files_list as $file) { foreach ($files_list as $file) {
$filePath = Filesystem::normalizePath($originalSharePath . '/' . $file); $subNode = $node->get($file);
$isDir = \OC\Files\Filesystem::is_dir($filePath); $nodePath = $userFolder->getRelativePath($node->getPath());
$activities[$filePath] = ($isDir) ? Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED : Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; if ($subNode instanceof \OCP\Files\File) {
$this->activityManager->publishActivity(
'files_sharing', Activity::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED, [$nodePath], '', [],
$nodePath, '', $share->getShareOwner()->getUID(), Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
);
} else {
$this->activityManager->publishActivity(
'files_sharing', Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED, [$nodePath], '', [],
$nodePath, '', $share->getShareOwner()->getUID(), Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
);
}
} }
} else { } else {
// The folder is downloaded // The folder is downloaded
$activities[$originalSharePath] = Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
}
foreach ($activities as $filePath => $subject) {
$this->activityManager->publishActivity( $this->activityManager->publishActivity(
'files_sharing', $subject, array($filePath), '', array(), 'files_sharing', Activity::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED, [$originalSharePath], '', [],
$filePath, '', $linkItem['uid_owner'], Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM $originalSharePath, '', $share->getShareOwner()->getUID(), Activity::TYPE_PUBLIC_LINKS, Activity::PRIORITY_MEDIUM
); );
} }
} }
/* FIXME: We should do this all nicely in OCP */
OC_Util::tearDownFS();
OC_Util::setupFS($share->getShareOwner()->getUID());
/** /**
* this sets a cookie to be able to recognize the start of the download * this sets a cookie to be able to recognize the start of the download
* the content must not be longer than 32 characters and must only contain * the content must not be longer than 32 characters and must only contain
@ -362,30 +398,4 @@ class ShareController extends Controller {
exit(); exit();
} }
} }
/**
* @param string $token
* @return string Resolved file path of the token
* @throws NotFoundException In case share could not get properly resolved
*/
private function getPath($token) {
$linkItem = Share::getShareByToken($token, false);
if (is_array($linkItem) && isset($linkItem['uid_owner'])) {
// seems to be a valid share
$rootLinkItem = Share::resolveReShare($linkItem);
if (isset($rootLinkItem['uid_owner'])) {
if(!$this->userManager->userExists($rootLinkItem['uid_owner'])) {
throw new NotFoundException('Owner of the share does not exist anymore');
}
OC_Util::tearDownFS();
OC_Util::setupFS($rootLinkItem['uid_owner']);
$path = Filesystem::getPath($linkItem['file_source']);
if(Filesystem::isReadable($path)) {
return $path;
}
}
}
throw new NotFoundException('No file found belonging to file.');
}
} }

View File

@ -317,27 +317,20 @@ class ShareControllerTest extends \Test\TestCase {
} }
public function testDownloadShare() { public function testDownloadShare() {
$share = $this->getMock('\OC\Share20\IShare');
$share->method('getPassword')->willReturn('password');
$this->shareManager
->expects($this->once())
->method('getShareByToken')
->with('validtoken')
->willReturn($share);
// Test with a password protected share and no authentication // Test with a password protected share and no authentication
$response = $this->shareController->downloadShare($this->token); $response = $this->shareController->downloadShare('validtoken');
$expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', $expectedResponse = new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate',
array('token' => $this->token))); ['token' => 'validtoken']));
$this->assertEquals($expectedResponse, $response); $this->assertEquals($expectedResponse, $response);
} }
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testDownloadShareWithDeletedFile() {
$this->container['UserManager']->expects($this->once())
->method('userExists')
->with($this->user)
->will($this->returnValue(true));
$view = new View('/'. $this->user . '/files');
$view->unlink('file1.txt');
$linkItem = Share::getShareByToken($this->token, false);
\OC::$server->getSession()->set('public_link_authenticated', $linkItem['id']);
$this->shareController->downloadShare($this->token);
}
} }