2012-11-13 18:11:02 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Frank Karlitschek <frank@owncloud.org>
|
|
|
|
* @author Georg Ehrke <georg@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Tobias Kaminsky <tobias@kaminsky.me>
|
2013-05-10 01:59:16 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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/>
|
2013-11-28 21:52:58 +04:00
|
|
|
*
|
2013-04-25 13:18:45 +04:00
|
|
|
*/
|
2013-05-29 14:33:24 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2014-04-04 13:37:47 +04:00
|
|
|
use OC\Preview\Provider;
|
2015-03-05 18:07:42 +03:00
|
|
|
use OCP\Files\FileInfo;
|
2014-08-09 12:39:12 +04:00
|
|
|
use OCP\Files\NotFoundException;
|
2014-04-04 13:37:47 +04:00
|
|
|
|
2013-05-29 14:33:24 +04:00
|
|
|
class Preview {
|
2013-07-10 19:57:04 +04:00
|
|
|
//the thumbnail folder
|
2013-04-25 13:18:45 +04:00
|
|
|
const THUMBNAILS_FOLDER = 'thumbnails';
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-08-28 18:50:40 +03:00
|
|
|
const MODE_FILL = 'fill';
|
|
|
|
const MODE_COVER = 'cover';
|
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
//config
|
2013-07-11 21:03:21 +04:00
|
|
|
private $maxScaleFactor;
|
2015-06-06 17:21:36 +03:00
|
|
|
/** @var int maximum width allowed for a preview */
|
|
|
|
private $configMaxWidth;
|
|
|
|
/** @var int maximum height allowed for a preview */
|
|
|
|
private $configMaxHeight;
|
2012-11-13 18:11:02 +04:00
|
|
|
|
2013-04-25 13:18:45 +04:00
|
|
|
//fileview object
|
2013-08-19 14:16:55 +04:00
|
|
|
private $fileView = null;
|
|
|
|
private $userView = null;
|
2013-05-10 01:59:16 +04:00
|
|
|
|
|
|
|
//vars
|
|
|
|
private $file;
|
|
|
|
private $maxX;
|
|
|
|
private $maxY;
|
2014-04-04 18:21:50 +04:00
|
|
|
private $scalingUp;
|
|
|
|
private $mimeType;
|
2014-04-29 19:07:10 +04:00
|
|
|
private $keepAspect = false;
|
2015-08-28 18:50:40 +03:00
|
|
|
private $mode = self::MODE_FILL;
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
//used to calculate the size of the preview to generate
|
|
|
|
/** @var int $maxPreviewWidth max width a preview can have */
|
|
|
|
private $maxPreviewWidth;
|
|
|
|
/** @var int $maxPreviewHeight max height a preview can have */
|
|
|
|
private $maxPreviewHeight;
|
|
|
|
/** @var int $previewWidth calculated width of the preview we're looking for */
|
|
|
|
private $previewWidth;
|
|
|
|
/** @var int $previewHeight calculated height of the preview we're looking for */
|
|
|
|
private $previewHeight;
|
|
|
|
|
2014-03-11 17:21:27 +04:00
|
|
|
//filemapper used for deleting previews
|
|
|
|
// index is path, value is fileinfo
|
|
|
|
static public $deleteFileMapper = array();
|
2014-08-26 14:41:03 +04:00
|
|
|
static public $deleteChildrenMapper = array();
|
2014-03-11 17:21:27 +04:00
|
|
|
|
2013-09-05 01:45:11 +04:00
|
|
|
/**
|
2014-04-04 13:37:47 +04:00
|
|
|
* preview images object
|
|
|
|
*
|
2015-03-13 12:10:11 +03:00
|
|
|
* @var \OCP\IImage
|
2013-09-05 01:45:11 +04:00
|
|
|
*/
|
2013-05-10 01:59:16 +04:00
|
|
|
private $preview;
|
2012-11-13 18:11:02 +04:00
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
/**
|
|
|
|
* @var \OCP\Files\FileInfo
|
|
|
|
*/
|
|
|
|
protected $info;
|
|
|
|
|
2013-04-25 13:18:45 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* check if thumbnail or bigger version of thumbnail of file is cached
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-07-11 21:21:37 +04:00
|
|
|
* @param string $user userid - if no user is given, OC_User::getUser will be used
|
|
|
|
* @param string $root path of root
|
|
|
|
* @param string $file The path to the file where you want a thumbnail from
|
2015-06-06 17:21:36 +03:00
|
|
|
* @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
|
2013-07-30 14:29:12 +04:00
|
|
|
* @param bool $scalingUp Disable/Enable upscaling of previews
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @throws \Exception
|
2013-11-28 21:52:58 +04:00
|
|
|
* @return mixed (bool / string)
|
2014-02-27 16:15:18 +04:00
|
|
|
* false if thumbnail does not exist
|
|
|
|
* path to thumbnail if thumbnail exists
|
|
|
|
*/
|
2015-06-06 17:21:36 +03:00
|
|
|
public function __construct(
|
|
|
|
$user = '',
|
|
|
|
$root = '/',
|
|
|
|
$file = '', $maxX = 1,
|
|
|
|
$maxY = 1,
|
|
|
|
$scalingUp = true
|
|
|
|
) {
|
2013-11-28 21:52:58 +04:00
|
|
|
//init fileviews
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($user === '') {
|
2013-11-28 21:52:58 +04:00
|
|
|
$user = \OC_User::getUser();
|
|
|
|
}
|
|
|
|
$this->fileView = new \OC\Files\View('/' . $user . '/' . $root);
|
|
|
|
$this->userView = new \OC\Files\View('/' . $user);
|
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
//set config
|
2015-06-06 17:21:36 +03:00
|
|
|
$sysConfig = \OC::$server->getConfig();
|
|
|
|
$this->configMaxWidth = $sysConfig->getSystemValue('preview_max_x', 2048);
|
|
|
|
$this->configMaxHeight = $sysConfig->getSystemValue('preview_max_y', 2048);
|
|
|
|
$this->maxScaleFactor = $sysConfig->getSystemValue('preview_max_scale_factor', 2);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
//save parameters
|
2013-07-10 19:57:04 +04:00
|
|
|
$this->setFile($file);
|
2015-06-06 17:21:36 +03:00
|
|
|
$this->setMaxX((int)$maxX);
|
|
|
|
$this->setMaxY((int)$maxY);
|
2013-07-30 14:29:12 +04:00
|
|
|
$this->setScalingUp($scalingUp);
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
$this->preview = null;
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
//check if there are preview backends
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!\OC::$server->getPreviewManager()
|
|
|
|
->hasProviders()
|
|
|
|
&& \OC::$server->getConfig()
|
|
|
|
->getSystemValue('enable_previews', true)
|
|
|
|
) {
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('core', 'No preview providers exist', \OCP\Util::ERROR);
|
2013-07-10 19:57:04 +04:00
|
|
|
throw new \Exception('No preview providers');
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the path of the file you want a thumbnail from
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return string
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2014-04-03 14:00:53 +04:00
|
|
|
public function getFile() {
|
2013-05-10 01:59:16 +04:00
|
|
|
return $this->file;
|
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the max width of the preview
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return integer
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getMaxX() {
|
2013-05-10 01:59:16 +04:00
|
|
|
return $this->maxX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the max height of the preview
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return integer
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getMaxY() {
|
2013-05-10 01:59:16 +04:00
|
|
|
return $this->maxY;
|
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns whether or not scalingup is enabled
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return bool
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-07-30 14:29:12 +04:00
|
|
|
public function getScalingUp() {
|
2014-04-04 18:21:50 +04:00
|
|
|
return $this->scalingUp;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the name of the thumbnailfolder
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return string
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-07-10 19:57:04 +04:00
|
|
|
public function getThumbnailsFolder() {
|
2013-05-10 01:59:16 +04:00
|
|
|
return self::THUMBNAILS_FOLDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the max scale factor
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return string
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getMaxScaleFactor() {
|
2013-07-11 21:03:21 +04:00
|
|
|
return $this->maxScaleFactor;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the max width set in ownCloud's config
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return string
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getConfigMaxX() {
|
2015-06-06 17:21:36 +03:00
|
|
|
return $this->configMaxWidth;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns the max height set in ownCloud's config
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return string
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getConfigMaxY() {
|
2015-06-06 17:21:36 +03:00
|
|
|
return $this->configMaxHeight;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Returns the FileInfo object associated with the file to preview
|
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return false|Files\FileInfo|\OCP\Files\FileInfo
|
|
|
|
*/
|
2014-02-27 16:15:18 +04:00
|
|
|
protected function getFileInfo() {
|
2014-03-11 17:21:27 +04:00
|
|
|
$absPath = $this->fileView->getAbsolutePath($this->file);
|
|
|
|
$absPath = Files\Filesystem::normalizePath($absPath);
|
2015-06-06 17:21:36 +03:00
|
|
|
if (array_key_exists($absPath, self::$deleteFileMapper)) {
|
2014-03-11 17:21:27 +04:00
|
|
|
$this->info = self::$deleteFileMapper[$absPath];
|
|
|
|
} else if (!$this->info) {
|
2014-02-27 16:15:18 +04:00
|
|
|
$this->info = $this->fileView->getFileInfo($this->file);
|
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
return $this->info;
|
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
private function getChildren() {
|
|
|
|
$absPath = $this->fileView->getAbsolutePath($this->file);
|
|
|
|
$absPath = Files\Filesystem::normalizePath($absPath);
|
|
|
|
|
|
|
|
if (array_key_exists($absPath, self::$deleteChildrenMapper)) {
|
|
|
|
return self::$deleteChildrenMapper[$absPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sets the path of the file you want a preview of
|
|
|
|
*
|
2013-07-11 21:21:37 +04:00
|
|
|
* @param string $file
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
|
|
|
* @return \OC\Preview
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-07-10 19:57:04 +04:00
|
|
|
public function setFile($file) {
|
|
|
|
$this->file = $file;
|
2014-02-27 16:15:18 +04:00
|
|
|
$this->info = null;
|
2015-01-08 20:33:35 +03:00
|
|
|
|
2013-11-28 21:52:58 +04:00
|
|
|
if ($file !== '') {
|
2014-03-13 15:32:02 +04:00
|
|
|
$this->getFileInfo();
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($this->info instanceof \OCP\Files\FileInfo) {
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->mimeType = $this->info->getMimetype();
|
2014-03-13 15:32:02 +04:00
|
|
|
}
|
2013-11-28 21:52:58 +04:00
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-11-28 22:05:43 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Forces the use of a specific media type
|
|
|
|
*
|
2014-04-04 18:21:50 +04:00
|
|
|
* @param string $mimeType
|
2013-11-28 22:05:43 +04:00
|
|
|
*/
|
2014-04-04 18:21:50 +04:00
|
|
|
public function setMimetype($mimeType) {
|
|
|
|
$this->mimeType = $mimeType;
|
2013-11-28 21:52:58 +04:00
|
|
|
}
|
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sets the max width of the preview. It's capped by the maximum allowed size set in the
|
|
|
|
* configuration
|
|
|
|
*
|
2013-07-11 21:21:37 +04:00
|
|
|
* @param int $maxX
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @throws \Exception
|
2015-06-06 17:21:36 +03:00
|
|
|
* @return \OC\Preview
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
|
|
|
public function setMaxX($maxX = 1) {
|
|
|
|
if ($maxX <= 0) {
|
2013-07-30 14:29:12 +04:00
|
|
|
throw new \Exception('Cannot set width of 0 or smaller!');
|
2013-07-10 19:57:04 +04:00
|
|
|
}
|
|
|
|
$configMaxX = $this->getConfigMaxX();
|
2015-06-06 17:21:36 +03:00
|
|
|
$maxX = $this->limitMaxDim($maxX, $configMaxX, 'maxX');
|
2013-07-10 19:57:04 +04:00
|
|
|
$this->maxX = $maxX;
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sets the max height of the preview. It's capped by the maximum allowed size set in the
|
|
|
|
* configuration
|
|
|
|
*
|
2013-07-11 21:21:37 +04:00
|
|
|
* @param int $maxY
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @throws \Exception
|
2015-06-06 17:21:36 +03:00
|
|
|
* @return \OC\Preview
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
|
|
|
public function setMaxY($maxY = 1) {
|
|
|
|
if ($maxY <= 0) {
|
2013-07-30 14:29:12 +04:00
|
|
|
throw new \Exception('Cannot set height of 0 or smaller!');
|
2013-07-10 19:57:04 +04:00
|
|
|
}
|
|
|
|
$configMaxY = $this->getConfigMaxY();
|
2015-06-06 17:21:36 +03:00
|
|
|
$maxY = $this->limitMaxDim($maxY, $configMaxY, 'maxY');
|
2013-07-10 19:57:04 +04:00
|
|
|
$this->maxY = $maxY;
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sets whether we're allowed to scale up when generating a preview. It's capped by the maximum
|
|
|
|
* allowed scale factor set in the configuration
|
|
|
|
*
|
2013-07-30 14:29:12 +04:00
|
|
|
* @param bool $scalingUp
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
|
|
|
* @return \OC\Preview
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-07-30 14:29:12 +04:00
|
|
|
public function setScalingup($scalingUp) {
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($this->getMaxScaleFactor() === 1) {
|
2013-07-30 14:29:12 +04:00
|
|
|
$scalingUp = false;
|
2013-07-10 19:57:04 +04:00
|
|
|
}
|
2014-04-04 18:21:50 +04:00
|
|
|
$this->scalingUp = $scalingUp;
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:50:40 +03:00
|
|
|
/**
|
|
|
|
* Set whether to cover or fill the specified dimensions
|
|
|
|
*
|
|
|
|
* @param string $mode
|
|
|
|
*
|
|
|
|
* @return \OC\Preview
|
|
|
|
*/
|
|
|
|
public function setMode($mode) {
|
|
|
|
$this->mode = $mode;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sets whether we need to generate a preview which keeps the aspect ratio of the original file
|
|
|
|
*
|
2014-08-26 14:41:03 +04:00
|
|
|
* @param bool $keepAspect
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
|
|
|
* @return \OC\Preview
|
2014-08-26 14:41:03 +04:00
|
|
|
*/
|
2014-04-29 19:07:10 +04:00
|
|
|
public function setKeepAspect($keepAspect) {
|
|
|
|
$this->keepAspect = $keepAspect;
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2014-04-29 19:07:10 +04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Makes sure we were given a file to preview and that it exists in the filesystem
|
|
|
|
*
|
2013-07-11 21:21:37 +04:00
|
|
|
* @return bool
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-07-10 19:57:04 +04:00
|
|
|
public function isFileValid() {
|
|
|
|
$file = $this->getFile();
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($file === '') {
|
2015-04-07 17:45:16 +03:00
|
|
|
\OCP\Util::writeLog('core', 'No filename passed', \OCP\Util::DEBUG);
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
if (!$this->fileView->file_exists($file)) {
|
2015-04-07 17:45:16 +03:00
|
|
|
\OCP\Util::writeLog('core', 'File:"' . $file . '" not found', \OCP\Util::DEBUG);
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-07-10 19:57:04 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Deletes the preview of a file with specific width and height
|
|
|
|
*
|
|
|
|
* This should never delete the max preview, use deleteAllPreviews() instead
|
|
|
|
*
|
2013-05-10 01:59:16 +04:00
|
|
|
* @return bool
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function deletePreview() {
|
2015-04-07 17:45:16 +03:00
|
|
|
$fileInfo = $this->getFileInfo();
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($fileInfo !== null && $fileInfo !== false) {
|
2014-03-13 15:32:02 +04:00
|
|
|
$fileId = $fileInfo->getId();
|
2013-05-29 14:01:43 +04:00
|
|
|
|
2014-04-29 19:07:10 +04:00
|
|
|
$previewPath = $this->buildCachePath($fileId);
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!strpos($previewPath, 'max')) {
|
|
|
|
return $this->userView->unlink($previewPath);
|
|
|
|
}
|
2014-03-13 15:32:02 +04:00
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2014-03-13 15:32:02 +04:00
|
|
|
return false;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Deletes all previews of a file
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function deleteAllPreviews() {
|
2014-08-26 14:41:03 +04:00
|
|
|
$toDelete = $this->getChildren();
|
2015-03-05 18:07:42 +03:00
|
|
|
$toDelete[] = $this->getFileInfo();
|
2014-08-26 14:41:03 +04:00
|
|
|
|
|
|
|
foreach ($toDelete as $delete) {
|
2015-03-05 18:07:42 +03:00
|
|
|
if ($delete instanceof FileInfo) {
|
2014-08-26 14:41:03 +04:00
|
|
|
/** @var \OCP\Files\FileInfo $delete */
|
|
|
|
$fileId = $delete->getId();
|
|
|
|
|
2015-03-05 18:07:42 +03:00
|
|
|
// getId() might return null, e.g. when the file is a
|
|
|
|
// .ocTransferId*.part file from chunked file upload.
|
|
|
|
if (!empty($fileId)) {
|
|
|
|
$previewPath = $this->getPreviewPath($fileId);
|
|
|
|
$this->userView->deleteAll($previewPath);
|
|
|
|
$this->userView->rmdir($previewPath);
|
|
|
|
}
|
2014-08-26 14:41:03 +04:00
|
|
|
}
|
2014-03-13 15:32:02 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Checks if a preview matching the asked dimensions or a bigger version is already cached
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
2015-06-06 17:21:36 +03:00
|
|
|
* * We first retrieve the size of the max preview since this is what we be used to create
|
|
|
|
* all our preview. If it doesn't exist we return false, so that it can be generated
|
|
|
|
* * Using the dimensions of the max preview, we calculate what the size of the new
|
|
|
|
* thumbnail should be
|
|
|
|
* * And finally, we look for a suitable candidate in the cache
|
|
|
|
*
|
|
|
|
* @param int $fileId fileId of the original file we need a preview of
|
|
|
|
*
|
|
|
|
* @return string|false path to the cached preview if it exists or false
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2014-04-04 18:21:50 +04:00
|
|
|
public function isCached($fileId) {
|
2014-02-27 16:15:18 +04:00
|
|
|
if (is_null($fileId)) {
|
2013-07-12 13:50:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Phase 1: Looking for the max preview
|
|
|
|
*/
|
|
|
|
$previewPath = $this->getPreviewPath($fileId);
|
|
|
|
// We currently can't look for a single file due to bugs related to #16478
|
|
|
|
$allThumbnails = $this->userView->getDirectoryContent($previewPath);
|
|
|
|
list($maxPreviewWidth, $maxPreviewHeight) = $this->getMaxPreviewSize($allThumbnails);
|
|
|
|
|
|
|
|
// Only use the cache if we have a max preview
|
|
|
|
if (!is_null($maxPreviewWidth) && !is_null($maxPreviewHeight)) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phase 2: Calculating the size of the preview we need to send back
|
|
|
|
*/
|
|
|
|
$this->maxPreviewWidth = $maxPreviewWidth;
|
|
|
|
$this->maxPreviewHeight = $maxPreviewHeight;
|
|
|
|
|
|
|
|
list($previewWidth, $previewHeight) = $this->simulatePreviewDimensions();
|
|
|
|
if (empty($previewWidth) || empty($previewHeight)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->previewWidth = $previewWidth;
|
|
|
|
$this->previewHeight = $previewHeight;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phase 3: We look for a preview of the exact size
|
|
|
|
*/
|
|
|
|
// This gives us a calculated path to a preview of asked dimensions
|
|
|
|
// thumbnailFolder/fileId/<maxX>-<maxY>(-max|-with-aspect).png
|
|
|
|
$preview = $this->buildCachePath($fileId, $previewWidth, $previewHeight);
|
|
|
|
|
|
|
|
// This checks if we have a preview of those exact dimensions in the cache
|
|
|
|
if ($this->userView->file_exists($preview)) {
|
|
|
|
return $preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phase 4: We look for a larger preview, matching the aspect ratio
|
|
|
|
*/
|
|
|
|
if (($this->getMaxX() >= $maxPreviewWidth)
|
|
|
|
&& ($this->getMaxY() >= $maxPreviewHeight)
|
|
|
|
) {
|
|
|
|
// The preview we-re looking for is the exact size or larger than the max preview,
|
|
|
|
// so return that
|
|
|
|
return $this->buildCachePath($fileId, $maxPreviewWidth, $maxPreviewHeight);
|
|
|
|
} else {
|
|
|
|
// The last resort is to look for something bigger than what we've calculated,
|
|
|
|
// but still smaller than the max preview
|
|
|
|
return $this->isCachedBigger($fileId, $allThumbnails);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the dimensions of the max preview
|
|
|
|
*
|
|
|
|
* @param FileInfo[] $allThumbnails the list of all our cached thumbnails
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
private function getMaxPreviewSize($allThumbnails) {
|
|
|
|
$maxPreviewX = null;
|
|
|
|
$maxPreviewY = null;
|
|
|
|
|
|
|
|
foreach ($allThumbnails as $thumbnail) {
|
|
|
|
$name = $thumbnail['name'];
|
|
|
|
if (strpos($name, 'max')) {
|
|
|
|
list($maxPreviewX, $maxPreviewY) = $this->getDimensionsFromFilename($name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$maxPreviewX, $maxPreviewY];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the size of the preview we should be looking for in the cache
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
private function simulatePreviewDimensions() {
|
|
|
|
$askedWidth = $this->getMaxX();
|
|
|
|
$askedHeight = $this->getMaxY();
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($this->keepAspect) {
|
|
|
|
list($newPreviewWidth, $newPreviewHeight) =
|
|
|
|
$this->applyAspectRatio($askedWidth, $askedHeight);
|
|
|
|
} else {
|
|
|
|
list($newPreviewWidth, $newPreviewHeight) = $this->fixSize($askedWidth, $askedHeight);
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return [(int)$newPreviewWidth, (int)$newPreviewHeight];
|
2014-04-02 19:59:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Resizes the boundaries to match the aspect ratio
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
2015-06-06 17:21:36 +03:00
|
|
|
* @param int $askedWidth
|
|
|
|
* @param int $askedHeight
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
2015-08-28 18:50:40 +03:00
|
|
|
* @param int $originalWidth
|
|
|
|
* @param int $originalHeight
|
2015-06-06 17:21:36 +03:00
|
|
|
* @return \int[]
|
2015-04-07 17:45:16 +03:00
|
|
|
*/
|
2015-08-28 18:50:40 +03:00
|
|
|
private function applyAspectRatio($askedWidth, $askedHeight, $originalWidth = 0, $originalHeight = 0) {
|
|
|
|
if(!$originalWidth){
|
|
|
|
$originalWidth= $this->maxPreviewWidth;
|
|
|
|
}
|
|
|
|
if (!$originalHeight) {
|
|
|
|
$originalHeight = $this->maxPreviewHeight;
|
|
|
|
}
|
|
|
|
$originalRatio = $originalWidth / $originalHeight;
|
2015-06-06 17:21:36 +03:00
|
|
|
// Defines the box in which the preview has to fit
|
|
|
|
$scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
|
2015-08-28 18:50:40 +03:00
|
|
|
$askedWidth = min($askedWidth, $originalWidth * $scaleFactor);
|
|
|
|
$askedHeight = min($askedHeight, $originalHeight * $scaleFactor);
|
2015-06-06 17:21:36 +03:00
|
|
|
|
|
|
|
if ($askedWidth / $originalRatio < $askedHeight) {
|
|
|
|
// width restricted
|
|
|
|
$askedHeight = round($askedWidth / $originalRatio);
|
|
|
|
} else {
|
|
|
|
$askedWidth = round($askedHeight * $originalRatio);
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return [(int)$askedWidth, (int)$askedHeight];
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:50:40 +03:00
|
|
|
/**
|
|
|
|
* Resizes the boundaries to cover the area
|
|
|
|
*
|
|
|
|
* @param int $askedWidth
|
|
|
|
* @param int $askedHeight
|
|
|
|
* @param int $previewWidth
|
|
|
|
* @param int $previewHeight
|
|
|
|
* @return \int[]
|
|
|
|
*/
|
|
|
|
private function applyCover($askedWidth, $askedHeight, $previewWidth, $previewHeight) {
|
|
|
|
$originalRatio = $previewWidth / $previewHeight;
|
|
|
|
// Defines the box in which the preview has to fit
|
|
|
|
$scaleFactor = $this->scalingUp ? $this->maxScaleFactor : 1;
|
|
|
|
$askedWidth = min($askedWidth, $previewWidth * $scaleFactor);
|
|
|
|
$askedHeight = min($askedHeight, $previewHeight * $scaleFactor);
|
|
|
|
|
|
|
|
if ($askedWidth / $originalRatio > $askedHeight) {
|
|
|
|
// height restricted
|
|
|
|
$askedHeight = round($askedWidth / $originalRatio);
|
|
|
|
} else {
|
|
|
|
$askedWidth = round($askedHeight * $originalRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [(int)$askedWidth, (int)$askedHeight];
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Makes sure an upscaled preview doesn't end up larger than the max dimensions defined in the
|
|
|
|
* config
|
|
|
|
*
|
|
|
|
* @param int $askedWidth
|
|
|
|
* @param int $askedHeight
|
|
|
|
*
|
|
|
|
* @return \int[]
|
|
|
|
*/
|
|
|
|
private function fixSize($askedWidth, $askedHeight) {
|
|
|
|
if ($this->scalingUp) {
|
|
|
|
$askedWidth = min($this->configMaxWidth, $askedWidth);
|
|
|
|
$askedHeight = min($this->configMaxHeight, $askedHeight);
|
2013-07-12 13:50:24 +04:00
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return [(int)$askedWidth, (int)$askedHeight];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a bigger version of a file preview is cached and if not
|
|
|
|
* return the preview of max allowed dimensions
|
|
|
|
*
|
|
|
|
* @param int $fileId fileId of the original image
|
|
|
|
* @param FileInfo[] $allThumbnails the list of all our cached thumbnails
|
|
|
|
*
|
|
|
|
* @return string path to bigger thumbnail
|
|
|
|
*/
|
|
|
|
private function isCachedBigger($fileId, $allThumbnails) {
|
|
|
|
// This is used to eliminate any thumbnail narrower than what we need
|
2014-04-02 19:59:39 +04:00
|
|
|
$maxX = $this->getMaxX();
|
|
|
|
|
2014-04-02 20:32:32 +04:00
|
|
|
//array for usable cached thumbnails
|
2015-06-06 17:21:36 +03:00
|
|
|
$possibleThumbnails = $this->getPossibleThumbnails($allThumbnails);
|
2014-04-02 20:32:32 +04:00
|
|
|
|
|
|
|
foreach ($possibleThumbnails as $width => $path) {
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($width < $maxX) {
|
2014-04-02 20:32:32 +04:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return $path;
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// At this stage, we didn't find a preview, so we return the max preview
|
|
|
|
return $this->buildCachePath($fileId, $this->maxPreviewWidth, $this->maxPreviewHeight);
|
2014-04-02 20:32:32 +04:00
|
|
|
}
|
2014-04-04 13:37:47 +04:00
|
|
|
|
2014-04-02 20:32:32 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Get possible bigger thumbnails of the given image with the proper aspect ratio
|
|
|
|
*
|
|
|
|
* @param FileInfo[] $allThumbnails the list of all our cached thumbnails
|
|
|
|
*
|
|
|
|
* @return string[] an array of paths to bigger thumbnails
|
|
|
|
*/
|
|
|
|
private function getPossibleThumbnails($allThumbnails) {
|
|
|
|
if ($this->keepAspect) {
|
|
|
|
$wantedAspectRatio = (float)($this->maxPreviewWidth / $this->maxPreviewHeight);
|
|
|
|
} else {
|
|
|
|
$wantedAspectRatio = (float)($this->getMaxX() / $this->getMaxY());
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2013-04-25 13:18:45 +04:00
|
|
|
//array for usable cached thumbnails
|
2013-07-30 14:29:12 +04:00
|
|
|
$possibleThumbnails = array();
|
2014-02-27 16:15:18 +04:00
|
|
|
foreach ($allThumbnails as $thumbnail) {
|
2013-07-12 13:50:24 +04:00
|
|
|
$name = rtrim($thumbnail['name'], '.png');
|
2014-04-02 20:32:32 +04:00
|
|
|
list($x, $y, $aspectRatio) = $this->getDimensionsFromFilename($name);
|
2014-04-03 14:00:53 +04:00
|
|
|
if (abs($aspectRatio - $wantedAspectRatio) >= 0.000001
|
2014-04-02 20:32:32 +04:00
|
|
|
|| $this->unscalable($x, $y)
|
|
|
|
) {
|
2013-04-25 13:18:45 +04:00
|
|
|
continue;
|
|
|
|
}
|
2013-07-30 14:29:12 +04:00
|
|
|
$possibleThumbnails[$x] = $thumbnail['path'];
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2013-07-30 14:29:12 +04:00
|
|
|
ksort($possibleThumbnails);
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2014-04-02 20:32:32 +04:00
|
|
|
return $possibleThumbnails;
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Looks at the preview filename from the cache and extracts the size of the preview
|
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param string $name
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
|
|
|
* @return array<int,int,float>
|
2014-04-21 17:44:54 +04:00
|
|
|
*/
|
2014-04-02 20:32:32 +04:00
|
|
|
private function getDimensionsFromFilename($name) {
|
2015-06-06 17:21:36 +03:00
|
|
|
$size = explode('-', $name);
|
|
|
|
$x = (int)$size[0];
|
|
|
|
$y = (int)$size[1];
|
|
|
|
$aspectRatio = (float)($x / $y);
|
|
|
|
|
|
|
|
return array($x, $y, $aspectRatio);
|
2014-04-02 20:32:32 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @param int $x
|
|
|
|
* @param int $y
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2014-04-02 20:32:32 +04:00
|
|
|
private function unscalable($x, $y) {
|
2014-05-19 19:50:53 +04:00
|
|
|
|
2014-04-02 20:32:32 +04:00
|
|
|
$maxX = $this->getMaxX();
|
|
|
|
$maxY = $this->getMaxY();
|
|
|
|
$scalingUp = $this->getScalingUp();
|
|
|
|
$maxScaleFactor = $this->getMaxScaleFactor();
|
2014-05-19 19:50:53 +04:00
|
|
|
|
2014-04-02 20:32:32 +04:00
|
|
|
if ($x < $maxX || $y < $maxY) {
|
|
|
|
if ($scalingUp) {
|
2015-06-06 17:21:36 +03:00
|
|
|
$scaleFactor = $maxX / $x;
|
|
|
|
if ($scaleFactor > $maxScaleFactor) {
|
2014-04-02 20:32:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-27 16:15:18 +04:00
|
|
|
} else {
|
2014-04-02 20:32:32 +04:00
|
|
|
return true;
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2014-04-02 19:34:48 +04:00
|
|
|
return false;
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2012-11-13 19:14:16 +04:00
|
|
|
|
2013-04-25 13:18:45 +04:00
|
|
|
/**
|
2015-04-07 17:45:16 +03:00
|
|
|
* Returns a preview of a file
|
|
|
|
*
|
|
|
|
* The cache is searched first and if nothing usable was found then a preview is
|
|
|
|
* generated by one of the providers
|
|
|
|
*
|
2015-03-13 12:10:11 +03:00
|
|
|
* @return \OCP\IImage
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public function getPreview() {
|
2014-02-27 16:15:18 +04:00
|
|
|
if (!is_null($this->preview) && $this->preview->valid()) {
|
2013-07-10 19:57:04 +04:00
|
|
|
return $this->preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->preview = null;
|
2015-04-07 17:45:16 +03:00
|
|
|
$fileInfo = $this->getFileInfo();
|
|
|
|
if ($fileInfo === null || $fileInfo === false) {
|
2014-03-13 15:32:02 +04:00
|
|
|
return new \OC_Image();
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-04-07 17:45:16 +03:00
|
|
|
$fileId = $fileInfo->getId();
|
2014-04-02 20:32:32 +04:00
|
|
|
$cached = $this->isCached($fileId);
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($cached) {
|
2015-04-07 17:45:16 +03:00
|
|
|
$this->getCachedPreview($fileId, $cached);
|
2013-07-10 19:57:04 +04:00
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
if (is_null($this->preview)) {
|
2015-04-07 17:45:16 +03:00
|
|
|
$this->generatePreview($fileId);
|
2013-07-10 19:57:04 +04:00
|
|
|
}
|
2013-06-26 12:57:37 +04:00
|
|
|
|
2015-06-19 16:07:49 +03:00
|
|
|
// We still don't have a preview, so we send back an empty object
|
2014-02-27 16:15:18 +04:00
|
|
|
if (is_null($this->preview)) {
|
2015-06-19 16:07:49 +03:00
|
|
|
$this->preview = new \OC_Image();
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2013-07-10 19:57:04 +04:00
|
|
|
|
2013-05-10 01:59:16 +04:00
|
|
|
return $this->preview;
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2012-11-13 18:11:02 +04:00
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Sends the preview, including the headers to client which requested it
|
|
|
|
*
|
2015-06-19 16:07:49 +03:00
|
|
|
* @param null|string $mimeTypeForHeaders the media type to use when sending back the reply
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-09-19 15:26:41 +04:00
|
|
|
* @throws NotFoundException
|
2014-02-27 16:15:18 +04:00
|
|
|
*/
|
2015-06-19 16:07:49 +03:00
|
|
|
public function showPreview($mimeTypeForHeaders = null) {
|
2014-09-19 15:26:41 +04:00
|
|
|
// Check if file is valid
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($this->isFileValid() === false) {
|
2014-09-19 15:26:41 +04:00
|
|
|
throw new NotFoundException('File not found.');
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
if (is_null($this->preview)) {
|
2013-07-10 19:57:04 +04:00
|
|
|
$this->getPreview();
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2015-03-13 12:10:11 +03:00
|
|
|
if ($this->preview instanceof \OCP\IImage) {
|
2015-06-19 16:07:49 +03:00
|
|
|
if ($this->preview->valid()) {
|
|
|
|
\OCP\Response::enableCaching(3600 * 24); // 24 hours
|
|
|
|
} else {
|
|
|
|
$this->getMimeIcon();
|
|
|
|
}
|
|
|
|
$this->preview->show($mimeTypeForHeaders);
|
2014-09-21 19:30:29 +04:00
|
|
|
}
|
2013-07-11 12:10:07 +04:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:45:16 +03:00
|
|
|
/**
|
|
|
|
* Retrieves the preview from the cache and resizes it if necessary
|
|
|
|
*
|
|
|
|
* @param int $fileId fileId of the original image
|
|
|
|
* @param string $cached the path to the cached preview
|
|
|
|
*/
|
|
|
|
private function getCachedPreview($fileId, $cached) {
|
|
|
|
$stream = $this->userView->fopen($cached, 'r');
|
|
|
|
$this->preview = null;
|
|
|
|
if ($stream) {
|
|
|
|
$image = new \OC_Image();
|
|
|
|
$image->loadFromFileHandle($stream);
|
|
|
|
|
|
|
|
$this->preview = $image->valid() ? $image : null;
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!is_null($this->preview)) {
|
|
|
|
// Size of the preview we calculated
|
|
|
|
$maxX = $this->previewWidth;
|
|
|
|
$maxY = $this->previewHeight;
|
|
|
|
// Size of the preview we retrieved from the cache
|
|
|
|
$previewX = (int)$this->preview->width();
|
|
|
|
$previewY = (int)$this->preview->height();
|
|
|
|
|
|
|
|
// We don't have an exact match
|
|
|
|
if ($previewX !== $maxX || $previewY !== $maxY) {
|
|
|
|
$this->resizeAndStore($fileId);
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose($stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes, crops, fixes orientation and stores in the cache
|
|
|
|
*
|
|
|
|
* @param int $fileId fileId of the original image
|
|
|
|
*/
|
|
|
|
private function resizeAndStore($fileId) {
|
2013-05-10 01:59:16 +04:00
|
|
|
$image = $this->preview;
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!($image instanceof \OCP\IImage)) {
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'core', '$this->preview is not an instance of \OCP\IImage', \OCP\Util::DEBUG
|
|
|
|
);
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$previewWidth = (int)$image->width();
|
|
|
|
$previewHeight = (int)$image->height();
|
|
|
|
$askedWidth = $this->getMaxX();
|
|
|
|
$askedHeight = $this->getMaxY();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phase 1: If required, adjust boundaries to keep aspect ratio
|
|
|
|
*/
|
|
|
|
if ($this->keepAspect) {
|
|
|
|
list($askedWidth, $askedHeight) =
|
2015-08-28 18:50:40 +03:00
|
|
|
$this->applyAspectRatio($askedWidth, $askedHeight, $previewWidth, $previewHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->mode === self::MODE_COVER) {
|
|
|
|
list($scaleWidth, $scaleHeight) =
|
|
|
|
$this->applyCover($askedWidth, $askedHeight, $previewWidth, $previewHeight);
|
|
|
|
} else {
|
|
|
|
$scaleWidth = $askedWidth;
|
|
|
|
$scaleHeight = $askedHeight;
|
2015-06-06 17:21:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phase 2: Resizes preview to try and match requirements.
|
|
|
|
* Takes the scaling ratio into consideration
|
|
|
|
*/
|
|
|
|
list($newPreviewWidth, $newPreviewHeight) = $this->scale(
|
2015-08-28 18:50:40 +03:00
|
|
|
$image, $scaleWidth, $scaleHeight, $previewWidth, $previewHeight
|
2015-06-06 17:21:36 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// The preview has been resized and should now have the asked dimensions
|
|
|
|
if ($newPreviewWidth === $askedWidth && $newPreviewHeight === $askedHeight) {
|
|
|
|
$this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
|
2013-05-10 01:59:16 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Phase 3: We're still not there yet, so we're clipping and filling
|
|
|
|
* to match the asked dimensions
|
|
|
|
*/
|
|
|
|
// It turns out the scaled preview is now too big, so we crop the image
|
|
|
|
if ($newPreviewWidth >= $askedWidth && $newPreviewHeight >= $askedHeight) {
|
2015-06-18 14:30:10 +03:00
|
|
|
$this->crop($image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight);
|
|
|
|
$this->storePreview($fileId, $askedWidth, $askedHeight);
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return;
|
2014-04-29 19:07:10 +04:00
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// At least one dimension of the scaled preview is too small,
|
|
|
|
// so we fill the space with a transparent background
|
|
|
|
if (($newPreviewWidth < $askedWidth || $newPreviewHeight < $askedHeight)) {
|
2015-06-18 14:30:10 +03:00
|
|
|
$this->cropAndFill(
|
|
|
|
$image, $askedWidth, $askedHeight, $newPreviewWidth, $newPreviewHeight
|
|
|
|
);
|
|
|
|
$this->storePreview($fileId, $askedWidth, $askedHeight);
|
2015-06-06 17:21:36 +03:00
|
|
|
|
2013-08-19 14:16:55 +04:00
|
|
|
return;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
// The preview is smaller, but we can't touch it
|
|
|
|
$this->storePreview($fileId, $newPreviewWidth, $newPreviewHeight);
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Calculates the new dimensions of the preview
|
|
|
|
*
|
|
|
|
* The new dimensions can be larger or smaller than the ones of the preview we have to resize
|
|
|
|
*
|
|
|
|
* @param \OCP\IImage $image
|
|
|
|
* @param int $askedWidth
|
|
|
|
* @param int $askedHeight
|
|
|
|
* @param int $previewWidth
|
|
|
|
* @param null $previewHeight
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
private function scale($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) {
|
|
|
|
$scalingUp = $this->getScalingUp();
|
|
|
|
$maxScaleFactor = $this->getMaxScaleFactor();
|
|
|
|
|
|
|
|
$factorX = $askedWidth / $previewWidth;
|
|
|
|
$factorY = $askedHeight / $previewHeight;
|
2013-07-10 19:57:04 +04:00
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($factorX >= $factorY) {
|
2013-05-10 01:59:16 +04:00
|
|
|
$factor = $factorX;
|
2014-02-27 16:15:18 +04:00
|
|
|
} else {
|
2013-05-10 01:59:16 +04:00
|
|
|
$factor = $factorY;
|
|
|
|
}
|
2013-07-10 19:57:04 +04:00
|
|
|
|
2014-02-27 16:15:18 +04:00
|
|
|
if ($scalingUp === false) {
|
|
|
|
if ($factor > 1) {
|
2013-07-10 19:57:04 +04:00
|
|
|
$factor = 1;
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2013-07-10 19:57:04 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// We cap when upscaling
|
2014-04-04 18:21:50 +04:00
|
|
|
if (!is_null($maxScaleFactor)) {
|
|
|
|
if ($factor > $maxScaleFactor) {
|
2015-06-06 17:21:36 +03:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'core', 'scale factor reduced from ' . $factor . ' to ' . $maxScaleFactor,
|
|
|
|
\OCP\Util::DEBUG
|
|
|
|
);
|
2014-04-04 18:21:50 +04:00
|
|
|
$factor = $maxScaleFactor;
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2012-11-13 18:11:02 +04:00
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$newPreviewWidth = round($previewWidth * $factor);
|
|
|
|
$newPreviewHeight = round($previewHeight * $factor);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$image->preciseResize($newPreviewWidth, $newPreviewHeight);
|
|
|
|
$this->preview = $image;
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
return [$newPreviewWidth, $newPreviewHeight];
|
|
|
|
}
|
2013-05-10 01:59:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Crops a preview which is larger than the dimensions we've received
|
|
|
|
*
|
|
|
|
* @param \OCP\IImage $image
|
|
|
|
* @param int $askedWidth
|
|
|
|
* @param int $askedHeight
|
|
|
|
* @param int $previewWidth
|
|
|
|
* @param null $previewHeight
|
|
|
|
*/
|
|
|
|
private function crop($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight = null) {
|
|
|
|
$cropX = floor(abs($askedWidth - $previewWidth) * 0.5);
|
|
|
|
//don't crop previews on the Y axis, this sucks if it's a document.
|
|
|
|
//$cropY = floor(abs($y - $newPreviewHeight) * 0.5);
|
|
|
|
$cropY = 0;
|
|
|
|
$image->crop($cropX, $cropY, $askedWidth, $askedHeight);
|
|
|
|
$this->preview = $image;
|
|
|
|
}
|
2013-11-28 21:52:58 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* 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 int $askedWidth
|
|
|
|
* @param int $askedHeight
|
|
|
|
* @param int $previewWidth
|
|
|
|
* @param null $previewHeight
|
|
|
|
*/
|
|
|
|
private function cropAndFill($image, $askedWidth, $askedHeight, $previewWidth, $previewHeight) {
|
|
|
|
if ($previewWidth > $askedWidth) {
|
|
|
|
$cropX = floor(($previewWidth - $askedWidth) * 0.5);
|
|
|
|
$image->crop($cropX, 0, $askedWidth, $previewHeight);
|
|
|
|
$previewWidth = $askedWidth;
|
2013-05-10 01:59:16 +04:00
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($previewHeight > $askedHeight) {
|
|
|
|
$cropY = floor(($previewHeight - $askedHeight) * 0.5);
|
|
|
|
$image->crop(0, $cropY, $previewWidth, $askedHeight);
|
|
|
|
$previewHeight = $askedHeight;
|
|
|
|
}
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// Creates a transparent background
|
|
|
|
$backgroundLayer = imagecreatetruecolor($askedWidth, $askedHeight);
|
|
|
|
imagealphablending($backgroundLayer, false);
|
|
|
|
$transparency = imagecolorallocatealpha($backgroundLayer, 0, 0, 0, 127);
|
|
|
|
imagefill($backgroundLayer, 0, 0, $transparency);
|
|
|
|
imagesavealpha($backgroundLayer, true);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$image = $image->resource();
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$mergeX = floor(abs($askedWidth - $previewWidth) * 0.5);
|
|
|
|
$mergeY = floor(abs($askedHeight - $previewHeight) * 0.5);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// Pastes the preview on top of the background
|
|
|
|
imagecopy(
|
|
|
|
$backgroundLayer, $image, $mergeX, $mergeY, 0, 0, $previewWidth,
|
|
|
|
$previewHeight
|
|
|
|
);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$image = new \OC_Image($backgroundLayer);
|
2013-05-17 21:08:16 +04:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$this->preview = $image;
|
2013-04-25 13:18:45 +04:00
|
|
|
}
|
2012-11-13 18:11:02 +04:00
|
|
|
|
2015-04-07 17:45:16 +03:00
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Saves a preview in the cache to speed up future calls
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
2015-06-06 17:21:36 +03:00
|
|
|
* Do not nullify the preview as it might send the whole process in a loop
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
2015-06-06 17:21:36 +03:00
|
|
|
* @param int $fileId fileId of the original image
|
|
|
|
* @param int $previewWidth
|
|
|
|
* @param int $previewHeight
|
2015-04-07 17:45:16 +03:00
|
|
|
*/
|
2015-06-06 17:21:36 +03:00
|
|
|
private function storePreview($fileId, $previewWidth, $previewHeight) {
|
|
|
|
if (empty($previewWidth) || empty($previewHeight)) {
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'core', 'Cannot save preview of dimension ' . $previewWidth . 'x' . $previewHeight,
|
|
|
|
\OCP\Util::DEBUG
|
|
|
|
);
|
|
|
|
|
2015-04-07 17:45:16 +03:00
|
|
|
} else {
|
2015-06-06 17:21:36 +03:00
|
|
|
$cachePath = $this->buildCachePath($fileId, $previewWidth, $previewHeight);
|
|
|
|
$this->userView->file_put_contents($cachePath, $this->preview->data());
|
2015-04-07 17:45:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path to a preview based on its dimensions and aspect
|
|
|
|
*
|
|
|
|
* @param int $fileId
|
2015-06-06 17:21:36 +03:00
|
|
|
* @param int|null $maxX
|
|
|
|
* @param int|null $maxY
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-06-06 17:21:36 +03:00
|
|
|
private function buildCachePath($fileId, $maxX = null, $maxY = null) {
|
|
|
|
if (is_null($maxX)) {
|
|
|
|
$maxX = $this->getMaxX();
|
|
|
|
}
|
|
|
|
if (is_null($maxY)) {
|
|
|
|
$maxY = $this->getMaxY();
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
|
|
|
|
$previewPath = $this->getPreviewPath($fileId);
|
|
|
|
$previewPath = $previewPath . strval($maxX) . '-' . strval($maxY);
|
2015-06-06 17:21:36 +03:00
|
|
|
$isMaxPreview =
|
|
|
|
($maxX === $this->maxPreviewWidth && $maxY === $this->maxPreviewHeight) ? true : false;
|
|
|
|
if ($isMaxPreview) {
|
|
|
|
$previewPath .= '-max';
|
|
|
|
}
|
|
|
|
if ($this->keepAspect && !$isMaxPreview) {
|
2015-04-07 17:45:16 +03:00
|
|
|
$previewPath .= '-with-aspect';
|
|
|
|
}
|
2015-08-28 18:50:40 +03:00
|
|
|
if ($this->mode === self::MODE_COVER) {
|
|
|
|
$previewPath .= '-cover';
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
$previewPath .= '.png';
|
|
|
|
|
|
|
|
return $previewPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Returns the path to the folder where the previews are stored, identified by the fileId
|
|
|
|
*
|
2015-04-07 17:45:16 +03:00
|
|
|
* @param int $fileId
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getPreviewPath($fileId) {
|
|
|
|
return $this->getThumbnailsFolder() . '/' . $fileId . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 17:21:36 +03:00
|
|
|
* Asks the provider to send a preview of the file which respects the maximum dimensions
|
|
|
|
* defined in the configuration and after saving it in the cache, it is then resized to the
|
|
|
|
* asked dimensions
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
|
|
|
* This is only called once in order to generate a large PNG of dimensions defined in the
|
|
|
|
* configuration file. We'll be able to quickly resize it later on.
|
2015-06-06 17:21:36 +03:00
|
|
|
* We never upscale the original conversion as this will be done later by the resizing
|
|
|
|
* operation
|
2015-04-07 17:45:16 +03:00
|
|
|
*
|
|
|
|
* @param int $fileId fileId of the original image
|
|
|
|
*/
|
|
|
|
private function generatePreview($fileId) {
|
|
|
|
$file = $this->getFile();
|
|
|
|
$preview = null;
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$previewProviders = \OC::$server->getPreviewManager()
|
|
|
|
->getProviders();
|
|
|
|
foreach ($previewProviders as $supportedMimeType => $providers) {
|
|
|
|
if (!preg_match($supportedMimeType, $this->mimeType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($providers as $closure) {
|
|
|
|
$provider = $closure();
|
|
|
|
if (!($provider instanceof \OCP\Preview\IProvider)) {
|
2015-04-07 17:45:16 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'core', 'Generating preview for "' . $file . '" with "' . get_class($provider)
|
|
|
|
. '"', \OCP\Util::DEBUG
|
|
|
|
);
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/** @var $provider Provider */
|
|
|
|
$preview = $provider->getThumbnail(
|
|
|
|
$file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false,
|
|
|
|
$this->fileView
|
|
|
|
);
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!($preview instanceof \OCP\IImage)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
$this->preview = $preview;
|
|
|
|
$previewPath = $this->getPreviewPath($fileId);
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) {
|
|
|
|
$this->userView->mkdir($this->getThumbnailsFolder() . '/');
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
if ($this->userView->is_dir($previewPath) === false) {
|
|
|
|
$this->userView->mkdir($previewPath);
|
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
// This stores our large preview so that it can be used in subsequent resizing requests
|
|
|
|
$this->storeMaxPreview($previewPath);
|
2015-04-07 17:45:16 +03:00
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
break 2;
|
2015-04-07 17:45:16 +03:00
|
|
|
}
|
2015-06-06 17:21:36 +03:00
|
|
|
}
|
2015-04-07 17:45:16 +03:00
|
|
|
|
|
|
|
// The providers have been kind enough to give us a preview
|
|
|
|
if ($preview) {
|
|
|
|
$this->resizeAndStore($fileId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 14:24:15 +03:00
|
|
|
/**
|
2015-06-19 16:07:49 +03:00
|
|
|
* Defines the media icon, for the media type of the original file, as the preview
|
2015-06-18 14:24:15 +03:00
|
|
|
*/
|
|
|
|
private function getMimeIcon() {
|
|
|
|
$image = new \OC_Image();
|
|
|
|
$mimeIconWebPath = \OC_Helper::mimetypeIcon($this->mimeType);
|
|
|
|
if (empty(\OC::$WEBROOT)) {
|
|
|
|
$mimeIconServerPath = \OC::$SERVERROOT . $mimeIconWebPath;
|
|
|
|
} else {
|
|
|
|
$mimeIconServerPath = str_replace(\OC::$WEBROOT, \OC::$SERVERROOT, $mimeIconWebPath);
|
|
|
|
}
|
|
|
|
$image->loadFromFile($mimeIconServerPath);
|
|
|
|
|
2015-06-19 16:07:49 +03:00
|
|
|
$this->preview = $image;
|
2015-06-18 14:24:15 +03:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:45:16 +03:00
|
|
|
/**
|
|
|
|
* Stores the max preview in the cache
|
|
|
|
*
|
|
|
|
* @param string $previewPath path to the preview
|
|
|
|
*/
|
|
|
|
private function storeMaxPreview($previewPath) {
|
2015-06-06 17:21:36 +03:00
|
|
|
$maxPreviewExists = false;
|
2015-04-07 17:45:16 +03:00
|
|
|
$preview = $this->preview;
|
|
|
|
|
|
|
|
$allThumbnails = $this->userView->getDirectoryContent($previewPath);
|
|
|
|
// This is so that the cache doesn't need emptying when upgrading
|
|
|
|
// Can be replaced by an upgrade script...
|
|
|
|
foreach ($allThumbnails as $thumbnail) {
|
|
|
|
$name = rtrim($thumbnail['name'], '.png');
|
|
|
|
if (strpos($name, 'max')) {
|
2015-06-06 17:21:36 +03:00
|
|
|
$maxPreviewExists = true;
|
2015-04-07 17:45:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We haven't found the max preview, so we create it
|
2015-06-06 17:21:36 +03:00
|
|
|
if (!$maxPreviewExists) {
|
|
|
|
$previewWidth = $preview->width();
|
|
|
|
$previewHeight = $preview->height();
|
|
|
|
$previewPath = $previewPath . strval($previewWidth) . '-' . strval($previewHeight);
|
2015-04-07 17:45:16 +03:00
|
|
|
$previewPath .= '-max.png';
|
|
|
|
$this->userView->file_put_contents($previewPath, $preview->data());
|
2015-06-06 17:21:36 +03:00
|
|
|
$this->maxPreviewWidth = $previewWidth;
|
|
|
|
$this->maxPreviewHeight = $previewHeight;
|
2015-04-07 17:45:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:36 +03:00
|
|
|
/**
|
|
|
|
* Limits a dimension to the maximum dimension provided as argument
|
|
|
|
*
|
|
|
|
* @param int $dim
|
|
|
|
* @param int $maxDim
|
|
|
|
* @param string $dimName
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function limitMaxDim($dim, $maxDim, $dimName) {
|
|
|
|
if (!is_null($maxDim)) {
|
|
|
|
if ($dim > $maxDim) {
|
|
|
|
\OCP\Util::writeLog(
|
|
|
|
'core', $dimName . ' reduced from ' . $dim . ' to ' . $maxDim, \OCP\Util::DEBUG
|
|
|
|
);
|
|
|
|
$dim = $maxDim;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dim;
|
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
|
|
|
* @param array $args
|
|
|
|
*/
|
2013-05-29 15:03:33 +04:00
|
|
|
public static function post_write($args) {
|
2014-04-04 17:02:08 +04:00
|
|
|
self::post_delete($args, 'files/');
|
2013-05-29 14:01:43 +04:00
|
|
|
}
|
2013-11-28 21:52:58 +04:00
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
|
|
|
* @param array $args
|
|
|
|
*/
|
2014-03-11 17:21:27 +04:00
|
|
|
public static function prepare_delete_files($args) {
|
|
|
|
self::prepare_delete($args, 'files/');
|
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
|
|
|
* @param array $args
|
|
|
|
* @param string $prefix
|
|
|
|
*/
|
2015-06-06 17:21:36 +03:00
|
|
|
public static function prepare_delete($args, $prefix = '') {
|
2013-05-29 14:01:43 +04:00
|
|
|
$path = $args['path'];
|
2014-02-27 16:15:18 +04:00
|
|
|
if (substr($path, 0, 1) === '/') {
|
2013-05-29 14:01:43 +04:00
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
2014-03-11 17:21:27 +04:00
|
|
|
|
|
|
|
$view = new \OC\Files\View('/' . \OC_User::getUser() . '/' . $prefix);
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
$absPath = Files\Filesystem::normalizePath($view->getAbsolutePath($path));
|
|
|
|
self::addPathToDeleteFileMapper($absPath, $view->getFileInfo($path));
|
|
|
|
if ($view->is_dir($path)) {
|
|
|
|
$children = self::getAllChildren($view, $path);
|
|
|
|
self::$deleteChildrenMapper[$absPath] = $children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $absolutePath
|
|
|
|
* @param \OCP\Files\FileInfo $info
|
|
|
|
*/
|
|
|
|
private static function addPathToDeleteFileMapper($absolutePath, $info) {
|
|
|
|
self::$deleteFileMapper[$absolutePath] = $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\Files\View $view
|
|
|
|
* @param string $path
|
2015-06-06 17:21:36 +03:00
|
|
|
*
|
2014-08-26 14:41:03 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getAllChildren($view, $path) {
|
|
|
|
$children = $view->getDirectoryContent($path);
|
|
|
|
$childrensFiles = array();
|
|
|
|
|
|
|
|
$fakeRootLength = strlen($view->getRoot());
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($children); $i++) {
|
|
|
|
$child = $children[$i];
|
|
|
|
|
|
|
|
$childsPath = substr($child->getPath(), $fakeRootLength);
|
|
|
|
|
|
|
|
if ($view->is_dir($childsPath)) {
|
|
|
|
$children = array_merge(
|
|
|
|
$children,
|
|
|
|
$view->getDirectoryContent($childsPath)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$childrensFiles[] = $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $childrensFiles;
|
2014-03-11 17:21:27 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
|
|
|
* @param array $args
|
|
|
|
*/
|
2014-03-11 17:21:27 +04:00
|
|
|
public static function post_delete_files($args) {
|
|
|
|
self::post_delete($args, 'files/');
|
|
|
|
}
|
|
|
|
|
2014-08-26 14:41:03 +04:00
|
|
|
/**
|
|
|
|
* @param array $args
|
|
|
|
* @param string $prefix
|
|
|
|
*/
|
2015-06-06 17:21:36 +03:00
|
|
|
public static function post_delete($args, $prefix = '') {
|
2014-03-11 17:21:27 +04:00
|
|
|
$path = Files\Filesystem::normalizePath($args['path']);
|
|
|
|
|
|
|
|
$preview = new Preview(\OC_User::getUser(), $prefix, $path);
|
2013-05-29 14:01:43 +04:00
|
|
|
$preview->deleteAllPreviews();
|
|
|
|
}
|
2013-07-29 17:47:17 +04:00
|
|
|
|
2013-09-05 01:45:11 +04:00
|
|
|
}
|