Skip to content

Commit

Permalink
Move thumb nailing to the next level ....
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 authored and patrickjahns committed Jun 11, 2018
1 parent 1ead426 commit 73a54c1
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 179 deletions.
10 changes: 9 additions & 1 deletion apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType;
use OCA\DAV\Files\IFileNode;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Events\EventEmitterTrait;
use OCP\Files\EntityTooLargeException;
Expand All @@ -61,7 +62,7 @@
use Sabre\DAV\IFile;
use Symfony\Component\EventDispatcher\GenericEvent;

class File extends Node implements IFile {
class File extends Node implements IFile, IFileNode {

use EventEmitterTrait;
protected $request;
Expand Down Expand Up @@ -714,4 +715,11 @@ public function getChecksum($algo = null) {
protected function header($string) {
\header($string);
}

/**
* @return \OCP\Files\Node
*/
public function getNode() {
return \OC::$server->getRootFolder()->get($this->getFileInfo()->getPath());
}
}
35 changes: 35 additions & 0 deletions apps/dav/lib/Files/IFileNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/


namespace OCA\DAV\Files;


use OCP\Files\Node;

interface IFileNode {

/**
* @return Node
*/
public function getNode();

}
117 changes: 117 additions & 0 deletions apps/dav/lib/Files/PreviewPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* @author Thomas Müller <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\DAV\Files;

use OCP\Files\IPreviewNode;
use OCP\ILogger;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;

class PreviewPlugin extends ServerPlugin {

/** @var Server */
protected $server;
/** @var ILogger */
private $logger;

public function __construct(ILogger $logger) {
$this->logger = $logger;
}

/**
* Initializes the plugin and registers event handlers
*
* @param Server $server
* @return void
*/
function initialize(Server $server) {

$this->server = $server;
$this->server->on('method:GET', [$this, 'httpGet'], 90);
}

/**
* Intercepts GET requests on node urls ending with ?preview.
* The node has to implement IPreviewNode
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
* @throws NotFound
*/
function httpGet(RequestInterface $request, ResponseInterface $response) {

$queryParams = $request->getQueryParameters();
if (!array_key_exists('preview', $queryParams)) {
return true;
}

$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);

if (!$node instanceof IFileNode) {
return false;
}
$fileNode = $node->getNode();
if (!$fileNode instanceof IPreviewNode) {
return false;
}

// Checking ACL, if available.
if ($aclPlugin = $this->server->getPlugin('acl')) {
/** @var \Sabre\DAVACL\Plugin $aclPlugin */
$aclPlugin->checkPrivileges($path, '{DAV:}read');
}

if ($image = $fileNode->getThumbnail($queryParams)) {
if ($image === null || !$image->valid()) {
return false;
}
$type = $image->mimeType();
if (!in_array($type, ['image/png', 'image/jpeg', 'image/gif'])) {
$type = 'application/octet-stream';
}

// Enable output buffering
ob_start();
// Capture the output
$image->show();
$imageData = ob_get_contents();
// Clear the output buffer
ob_end_clean();

$response->setHeader('Content-Type', $type);
$response->setHeader('Content-Disposition', 'attachment');
$response->setStatus(200);

$response->setBody($imageData);

// Returning false to break the event chain
return false;
}
// TODO: add forceIcon handling .... if still needed
throw new NotFound();
}
}
11 changes: 10 additions & 1 deletion apps/dav/lib/Meta/MetaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use OC\Files\Meta\MetaFileVersionNode;
use OCA\DAV\Files\ICopySource;
use OCA\DAV\Files\IProvidesAdditionalHeaders;
use OCA\DAV\Files\IFileNode;
use OCP\Files\Node;
use Sabre\DAV\File;

/**
Expand All @@ -34,7 +36,7 @@
*
* @package OCA\DAV\Meta
*/
class MetaFile extends File implements ICopySource, IProvidesAdditionalHeaders {
class MetaFile extends File implements ICopySource, IFileNode, IProvidesAdditionalHeaders {

/** @var \OCP\Files\File */
private $file;
Expand Down Expand Up @@ -119,4 +121,11 @@ public function getContentDispositionFileName() {
}
return $this->getName();
}

/**
* @return Node
*/
public function getNode() {
return $this->file;
}
}
2 changes: 2 additions & 0 deletions apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OCA\DAV\DAV\MiscCustomPropertiesBackend;
use OCA\DAV\DAV\PublicAuth;
use OCA\DAV\Files\BrowserErrorPagePlugin;
use OCA\DAV\Files\PreviewPlugin;
use OCA\DAV\SystemTag\SystemTagPlugin;
use OCA\DAV\Upload\ChunkingPlugin;
use OCP\IRequest;
Expand Down Expand Up @@ -185,6 +186,7 @@ public function __construct(IRequest $request, $baseUri) {
$this->server->addPlugin(new BrowserErrorPagePlugin());
}

$this->server->addPlugin(new PreviewPlugin($logger));
// wait with registering these until auth is handled and the filesystem is setup
$this->server->on('beforeMethod', function () use ($root) {
// custom properties plugin must be the last one
Expand Down
36 changes: 33 additions & 3 deletions lib/private/Files/Meta/MetaFileVersionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@
*
*/


namespace OC\Files\Meta;


use OC\Files\Node\AbstractFile;
use OC\Files\Node\File;
use OCP\Files\ForbiddenException;
use OCP\Files\IProvidesAdditionalHeaders;
use OC\Preview;
use OCP\Files\IRootFolder;
use OCP\Files\IPreviewNode;
use OCP\Files\Storage\IVersionedStorage;
use OCP\Files\Storage;
use OCP\IImage;

/**
* Class MetaFileVersionNode - this class represents a version of a file in the
* meta endpoint
*
* @package OC\Files\Meta
*/
class MetaFileVersionNode extends AbstractFile implements IProvidesAdditionalHeaders {
class MetaFileVersionNode extends AbstractFile implements IPreviewNode, IProvidesAdditionalHeaders {

/** @var string */
private $versionId;
Expand Down Expand Up @@ -143,4 +144,33 @@ public function getContentDispositionFileName() {
return basename($this->internalPath);
}

public function getId() {
return $this->parent->getId();
}

public function getPath() {
return $this->parent->getPath() . '/' . $this->getName();
}

/**
* @param array $options
* @return IImage
* @since 10.1.0
*/
public function getThumbnail($options) {
$maxX = array_key_exists('x', $options) ? (int)$options['x'] : 32;
$maxY = array_key_exists('y', $options) ? (int)$options['y'] : 32;
$scalingUp = array_key_exists('scalingup', $options) ? (bool)$options['scalingup'] : true;
$keepAspect = array_key_exists('a', $options) ? true : false;
$mode = array_key_exists('mode', $options) ? $options['mode'] : 'fill';

$preview = new Preview();
$preview->setFile($this, $this->versionId);
$preview->setMaxX($maxX);
$preview->setMaxY($maxY);
$preview->setScalingUp($scalingUp);
$preview->setMode($mode);
$preview->setKeepAspect($keepAspect);
return $preview->getPreview();
}
}
26 changes: 25 additions & 1 deletion lib/private/Files/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

namespace OC\Files\Node;

use OCP\Files\IPreviewNode;
use OCP\Files\NotPermittedException;
use OCP\IImage;

class File extends Node implements \OCP\Files\File {
class File extends Node implements \OCP\Files\File, IPreviewNode {
/**
* Creates a Folder that represents a non-existing path
*
Expand Down Expand Up @@ -138,4 +140,26 @@ public function hash($type, $raw = false) {
public function getChecksum() {
return $this->getFileInfo()->getChecksum();
}

/**
* @param array $options
* @return IImage
* @since 10.1.0
*/
public function getThumbnail($options) {
$maxX = array_key_exists('x', $options) ? (int)$options['x'] : 32;
$maxY = array_key_exists('y', $options) ? (int)$options['y'] : 32;
$scalingUp = array_key_exists('scalingup', $options) ? (bool)$options['scalingup'] : true;
$keepAspect = array_key_exists('a', $options) ? true : false;
$mode = array_key_exists('mode', $options) ? $options['mode'] : 'fill';

$preview = new \OC\Preview();
$preview->setFile($this->getInternalPath(), $this->getFileInfo());
$preview->setMaxX($maxX);
$preview->setMaxY($maxY);
$preview->setScalingUp($scalingUp);
$preview->setMode($mode);
$preview->setKeepAspect($keepAspect);
return $preview->getPreview();
}
}
Loading

0 comments on commit 73a54c1

Please sign in to comment.