Skip to content

Commit

Permalink
Merge pull request #26611 from owncloud/publicpreview-share20
Browse files Browse the repository at this point in the history
Port publicpreview.php to the share 2.0 API
  • Loading branch information
DeepDiver1975 authored Nov 21, 2016
2 parents 65f642f + 7d57c77 commit 9412235
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions apps/files_sharing/ajax/publicpreview.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Björn Schießle <[email protected]>
* @author Georg Ehrke <[email protected]>
Expand All @@ -24,6 +25,9 @@
*
*/

// FIXME: rewrite this as a controller
use OCP\Share\Exceptions\ShareNotFound;

OCP\JSON::checkAppEnabled('files_sharing');

\OC_User::setIncognitoMode(true);
Expand All @@ -41,40 +45,36 @@
exit;
}

$linkedItem = \OCP\Share::getShareByToken($token);
if($linkedItem === false || ($linkedItem['item_type'] !== 'file' && $linkedItem['item_type'] !== 'folder')) {
$shareManager = \OC::$server->getShareManager();
try {
$linkedItem = $shareManager->getShareByToken($token);
} catch (ShareNotFound $e) {
\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
\OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG);
exit;
}

if(!isset($linkedItem['uid_owner']) || !isset($linkedItem['file_source'])) {
$userId = $linkedItem->getShareOwner();
if(is_null($userId)) {
\OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
\OCP\Util::writeLog('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OCP\Util::WARN);
exit;
}

$rootLinkItem = OCP\Share::resolveReShare($linkedItem);
$userId = $rootLinkItem['uid_owner'];

OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
\OC_Util::setupFS($userId);
\OC\Files\Filesystem::initMountPoints($userId);
$view = new \OC\Files\View('/' . $userId . '/files');
OCP\JSON::checkUserExists($userId);

$pathId = $linkedItem['file_source'];
$path = $view->getPath($pathId);

if($path === null) {
// FS setup and file existence check is already done in getNode()
try {
$node = $linkedItem->getNode();
} catch (\OCP\Files\NotFoundException $e) {
\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
\OCP\Util::writeLog('core-preview', 'Could not resolve file for shared item', \OCP\Util::WARN);
exit;
}

$pathInfo = $view->getFileInfo($path);
$sharedFile = null;
$path = $node->getPath();

if($linkedItem['item_type'] === 'folder') {
if($linkedItem->getNodeType() === 'folder') {
$isValid = \OC\Files\Filesystem::isValidPath($file);
if(!$isValid) {
\OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
Expand All @@ -84,24 +84,22 @@
$sharedFile = \OC\Files\Filesystem::normalizePath($file);
}

if($linkedItem['item_type'] === 'file') {
$parent = $pathInfo['parent'];
$path = $view->getPath($parent);
$sharedFile = $pathInfo['name'];
if($linkedItem->getNodeType() === 'file') {
$path = $node->getParent()->getPath();
$sharedFile = $node->getName();
}

$path = \OC\Files\Filesystem::normalizePath($path, false);
if(substr($path, 0, 1) === '/') {
$path = substr($path, 1);
}
$path = ltrim(\OC\Files\Filesystem::normalizePath($path, false), '/');

if($maxX === 0 || $maxY === 0) {
\OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
\OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG);
exit;
}

$root = 'files/' . $path;
// $path is relative to the data directory but Preview expects it to be relative to the user's
// so strip the first component
$root = substr($path, strpos($path, '/') + 1);

try{
$preview = new \OC\Preview($userId, $root);
Expand All @@ -112,6 +110,9 @@
$preview->setKeepAspect($keepAspect);

$preview->showPreview();
} catch (\OCP\Files\NotFoundException $e) {
\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
\OCP\Util::writeLog('core-preview', 'Requested file not found', \OCP\Util::WARN);
} catch (\Exception $e) {
\OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
\OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG);
Expand Down

0 comments on commit 9412235

Please sign in to comment.