Skip to content

Commit

Permalink
Merge pull request #6599 from nextcloud/fix_2523
Browse files Browse the repository at this point in the history
Add direct preview link
  • Loading branch information
MorrisJobke authored Sep 27, 2017
2 parents 72889e5 + 4077f68 commit 5f25dd7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
47 changes: 47 additions & 0 deletions apps/files_sharing/lib/Controller/PublicPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,51 @@ public function getPreview(
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}

/**
* @PublicPage
* @NoCSRFRequired
* @NoSameSiteCookieRequired
*
* @param $token
* @return DataResponse|FileDisplayResponse
*/
public function directLink($token) {
// No token no image
if ($token === '') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

// No share no image
try {
$share = $this->shareManager->getShareByToken($token);
} catch (ShareNotFound $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

// No permissions no image
if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

// Password protected shares have no direct link!
if ($share->getPassword() !== null) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

try {
$node = $share->getNode();
if ($node instanceof Folder) {
// Direct link only works for single files
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$f = $this->previewManager->getPreview($node, -1, -1, false);
return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
}
}
5 changes: 5 additions & 0 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,14 @@ public function showShare($token, $path = '') {
$shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024);
$shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024);
$shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null);
$shareTmpl['previewURL'] = $shareTmpl['downloadURL'];
if ($shareTmpl['previewSupported']) {
$shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview',
['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]);
// We just have direct previews for image files
if ($share->getNode()->getMimePart() === 'image') {
$shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
}
} else {
$shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/templates/public.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</div>
<div class="directLink">
<label for="directLink"><?php p($l->t('Direct link')) ?></label>
<input id="directLink" type="text" readonly value="<?php p($_['downloadURL']); ?>">
<input id="directLink" type="text" readonly value="<?php p($_['previewURL']); ?>">
</div>
<?php endif; ?>
</div>
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ public function testShowShare() {
'shareOwner' => 'ownerDisplay',
'disclaimer' => 'My disclaimer text',
'shareUrl' => null,
'previewImage' => null
'previewImage' => null,
'previewURL' => null,
);

$csp = new \OCP\AppFramework\Http\ContentSecurityPolicy();
Expand Down
8 changes: 8 additions & 0 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@
throw new \OC\HintException('App file sharing is not enabled');
}
});
$this->create('files_sharing.publicpreview.directLink', '/s/{token}/preview')->get()->action(function($urlParams) {
if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) {
$app = new \OCA\Files_Sharing\AppInfo\Application($urlParams);
$app->dispatch('PublicPreviewController', 'directLink');
} else {
throw new \OC\HintException('App file sharing is not enabled');
}
});

// used for heartbeat
$this->create('heartbeat', '/heartbeat')->action(function(){
Expand Down
6 changes: 6 additions & 0 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);

// If both width and heigth are -1 we just want the max preview
if ($width === -1 && $height === -1) {
$width = $maxWidth;
$height = $maxHeight;
}

// Calculate the preview size
list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);

Expand Down

0 comments on commit 5f25dd7

Please sign in to comment.