Skip to content

Commit

Permalink
Fix preview provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 authored and gitmate-bot committed Jan 18, 2018
1 parent 4c10724 commit 7694d80
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 99 deletions.
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function unMount($mount) {
* @param string $path
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @return string
* @return File|Folder
*/
public function get($path) {
$path = $this->normalizePath($path);
Expand Down
116 changes: 56 additions & 60 deletions lib/private/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
use OC\Files\View;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\IImage;
use OCP\Preview\IProvider2;
use OCP\Response;
use OCP\Util;

class Preview {
Expand Down Expand Up @@ -84,7 +88,7 @@ class Preview {
/**
* preview images object
*
* @var \OCP\IImage
* @var IImage
*/
private $preview;
/** @var string */
Expand All @@ -95,22 +99,19 @@ class Preview {
*
* @param string $user userid - if no user is given, OC_User::getUser will be used
* @param string $root path of root
* @param File $file The path to the file where you want a thumbnail from
* @param Node $file The path to the file where you want a thumbnail from
* @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the
* shape of the image
* @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the
* shape of the image
* @param bool $scalingUp Disable/Enable upscaling of previews
*
* @param bool $scalingUp Disable/Enable up-scaling of previews
* @param string $versionId
* @throws \Exception
* @return mixed (bool / string)
* false if thumbnail does not exist
* path to thumbnail if thumbnail exists
*/
public function __construct(
$user = '',
$root = '/',
$file = null, $maxX = 1,
Node $file = null, $maxX = 1,
$maxY = 1,
$scalingUp = true,
$versionId = null
Expand Down Expand Up @@ -234,7 +235,7 @@ protected function getFileInfo() {
* @return array|null
*/
private function getChildren() {
$absPath = $this->fileView->getAbsolutePath($this->file);
$absPath = $this->file->getPath();
$absPath = Files\Filesystem::normalizePath($absPath);

if (array_key_exists($absPath, self::$deleteChildrenMapper)) {
Expand All @@ -247,13 +248,12 @@ private function getChildren() {
/**
* Sets the path of the file you want a preview of
*
* @param File $file
* @param \OCP\Files\FileInfo|null $info
* @param Node $file
* @param string $versionId
*
* @return Preview
*/
public function setFile(File $file, $versionId = null) {
public function setFile(Node $file, $versionId = null) {
$this->file = $file;
$this->versionId = $versionId;
$this->mimeType = $this->file->getMimetype();
Expand Down Expand Up @@ -413,7 +413,7 @@ public function deleteAllPreviews() {
// getId() might return null, e.g. when the file is a
// .ocTransferId*.part file from chunked file upload.
if (!empty($fileId)) {
$previewPath = $this->getPreviewPath();
$previewPath = $this->getPreviewPath($fileId);
$this->userView->rmdir($previewPath);
}
}
Expand Down Expand Up @@ -739,7 +739,7 @@ private function unscalable($x, $y) {
* The cache is searched first and if nothing usable was found then a preview is
* generated by one of the providers
*
* @return \OCP\IImage
* @return IImage
*/
public function getPreview() {
if (!is_null($this->preview) && $this->preview->valid()) {
Expand Down Expand Up @@ -785,9 +785,9 @@ public function showPreview($mimeTypeForHeaders = null) {
if (is_null($this->preview)) {
$this->getPreview();
}
if ($this->preview instanceof \OCP\IImage) {
if ($this->preview instanceof IImage) {
if ($this->preview->valid()) {
\OCP\Response::enableCaching(3600 * 24); // 24 hours
Response::enableCaching(3600 * 24); // 24 hours
} else {
$this->getMimeIcon();
}
Expand Down Expand Up @@ -832,7 +832,7 @@ private function getCachedPreview($cached) {
*/
private function resizeAndStore() {
$image = $this->preview;
if (!($image instanceof \OCP\IImage)) {
if (!($image instanceof IImage)) {
Util::writeLog(
'core', '$this->preview is not an instance of \OCP\IImage', Util::DEBUG
);
Expand Down Expand Up @@ -904,7 +904,7 @@ private function resizeAndStore() {
*
* The new dimensions can be larger or smaller than the ones of the preview we have to resize
*
* @param \OCP\IImage $image
* @param IImage $image
* @param int $askedWidth
* @param int $askedHeight
* @param int $previewWidth
Expand Down Expand Up @@ -954,7 +954,7 @@ private function scale($image, $askedWidth, $askedHeight, $previewWidth, $previe
/**
* Crops a preview which is larger than the dimensions we've received
*
* @param \OCP\IImage $image
* @param IImage $image
* @param int $askedWidth
* @param int $askedHeight
* @param int $previewWidth
Expand All @@ -973,7 +973,7 @@ private function crop($image, $askedWidth, $askedHeight, $previewWidth, $preview
* Crops an image if it's larger than the dimensions we've received and fills the empty space
* with a transparent background
*
* @param \OCP\IImage $image
* @param IImage $image
* @param int $askedWidth
* @param int $askedHeight
* @param int $previewWidth
Expand Down Expand Up @@ -1075,11 +1075,13 @@ private function buildCachePath($maxX = null, $maxY = null) {
*
* @return string
*/
private function getPreviewPath() {
$fileId = $this->getFileInfo()->getId();
if ($this->versionId !== null) {
$fileId .= '/';
$fileId .= $this->versionId;
private function getPreviewPath($fileId = null) {
if ($fileId === null) {
$fileId = $this->getFileInfo()->getId();
if ($this->versionId !== null) {
$fileId .= '/';
$fileId .= $this->versionId;
}
}
return $this->getThumbnailsFolder() . '/' . $fileId . '/';
}
Expand Down Expand Up @@ -1119,7 +1121,7 @@ private function generatePreview() {

$preview = $provider->getThumbnail($file, $this->configMaxWidth, $this->configMaxHeight, false);

if (!($preview instanceof \OCP\IImage)) {
if (!($preview instanceof IImage)) {
continue;
}

Expand Down Expand Up @@ -1220,14 +1222,14 @@ private function limitMaxDim($dim, $maxDim, $dimName) {
* @param array $args
*/
public static function post_write($args) {
// self::post_delete($args, 'files/');
self::post_delete($args, 'files/');
}

/**
* @param array $args
*/
public static function prepare_delete_files($args) {
// self::prepare_delete($args, 'files/');
self::prepare_delete($args, 'files/');
}

/**
Expand All @@ -1239,18 +1241,11 @@ public static function prepare_delete(array $args, $prefix = '') {
if (substr($path, 0, 1) === '/') {
$path = substr($path, 1);
}

$view = new View('/' . \OC_User::getUser() . '/' . $prefix);

$absPath = Files\Filesystem::normalizePath($view->getAbsolutePath($path));
$fileInfo = $view->getFileInfo($path);
if ($fileInfo === false) {
return;
}
self::addPathToDeleteFileMapper($absPath, $fileInfo);
if ($view->is_dir($path)) {
$children = self::getAllChildren($view, $path);
self::$deleteChildrenMapper[$absPath] = $children;
$node = \OC::$server->getUserFolder()->get($path);
self::addPathToDeleteFileMapper($node->getPath(), $node);
if ($node->getType() === FileInfo::TYPE_FOLDER) {
$children = self::getAllChildren($node);
self::$deleteChildrenMapper[$node->getPath()] = $children;
}
}

Expand All @@ -1263,47 +1258,41 @@ private static function addPathToDeleteFileMapper($absolutePath, $info) {
}

/**
* @param View $view
* @param string $path
* @param Folder $node
*
* @return array
*/
private static function getAllChildren($view, $path) {
$children = $view->getDirectoryContent($path);
$childrensFiles = [];

$fakeRootLength = strlen($view->getRoot());
private static function getAllChildren($node) {
$children = $node->getDirectoryListing();
$childrenFiles = [];

for ($i = 0; $i < count($children); $i++) {
$child = $children[$i];
foreach ($children as $child) {

$childsPath = substr($child->getPath(), $fakeRootLength);

if ($view->is_dir($childsPath)) {
$children = array_merge(
$children,
$view->getDirectoryContent($childsPath)
if ($child->getType() === FileInfo::TYPE_FOLDER) {
$childrenFiles = array_merge(
$childrenFiles,
self::getAllChildren($child)
);
} else {
$childrensFiles[] = $child;
$childrenFiles[] = $child;
}
}

return $childrensFiles;
return $childrenFiles;
}

/**
* @param array $args
*/
public static function post_delete_files($args) {
// self::post_delete($args, 'files/');
self::post_delete($args, 'files/');
}

/**
* @param array $args
*/
public static function post_delete_versions($args) {
// self::post_delete($args, 'files/');
self::post_delete($args, 'files/');
}

/**
Expand All @@ -1314,7 +1303,14 @@ public static function post_delete($args, $prefix = '') {
$path = Files\Filesystem::normalizePath($args['path']);
$user = isset($args['user']) ? $args['user'] : \OC_User::getUser();

$preview = new Preview($user, $prefix, $path);
$view = new View('/' . $user . '/' . $prefix);
$absPath = Files\Filesystem::normalizePath($view->getAbsolutePath($path));
if (!isset(self::$deleteFileMapper[$absPath])) {
return;
}
$node = self::$deleteFileMapper[$absPath];

$preview = new Preview($user, $prefix, $node);
$preview->deleteAllPreviews();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/private/Preview/Office.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
//create imagick object from pdf
$pdfPreview = null;
try {
list($dirname, , , $filename) = array_values(pathinfo($absPath));
$pdfPreview = $dirname . '/' . $filename . '.pdf';
$pathInfo = pathinfo($absPath);
$pdfPreview = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.pdf';

$pdf = new \imagick($pdfPreview . '[0]');
$pdf->setImageFormat('jpg');
} catch (\Exception $e) {
unlink($absPath);
unlink($pdfPreview);
@unlink($pdfPreview);
\OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR);
return false;
}
Expand Down
15 changes: 8 additions & 7 deletions tests/lib/Preview/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@

namespace Test\Preview;

use OCP\Files\Node;
use OCP\Preview\IProvider2;
use Test\Traits\UserTrait;

abstract class Provider extends \Test\TestCase {
use UserTrait;

/** @var string */
/** @var Node */
protected $imgPath;
/** @var int */
protected $width;
/** @var int */
protected $height;
/** @var \OC\Preview\Provider */
/** @var IProvider2 */
protected $provider;
/** @var int */
protected $maxWidth = 1024;
Expand Down Expand Up @@ -116,7 +118,7 @@ public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
* @param string $fileName name of the file to create
* @param string $fileContent path to file to use for test
*
* @return string
* @return Node
*/
protected function prepareTestFile($fileName, $fileContent) {
$imgData = file_get_contents($fileContent);
Expand All @@ -125,19 +127,18 @@ protected function prepareTestFile($fileName, $fileContent) {

$scanner = $this->storage->getScanner();
$scanner->scan('');

return $imgPath;
return \OC::$server->getUserFolder($this->userId)->get($fileName);
}

/**
* Retrieves a max size thumbnail can be created
*
* @param \OC\Preview\Provider $provider
* @param IProvider2 $provider
*
* @return bool|\OCP\IImage
*/
private function getPreview($provider) {
$preview = $provider->getThumbnail($this->imgPath, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView);
$preview = $provider->getThumbnail($this->imgPath, $this->maxWidth, $this->maxHeight, $this->scalingUp);

$this->assertNotFalse($preview);
$this->assertTrue($preview->valid());
Expand Down
Loading

0 comments on commit 7694d80

Please sign in to comment.