|
|
|
@ -37,16 +37,9 @@ use OCP\IPreview; |
|
|
|
|
use OCP\Preview\IProvider; |
|
|
|
|
|
|
|
|
|
class Generator { |
|
|
|
|
//the thumbnail folder |
|
|
|
|
const THUMBNAILS_FOLDER = 'thumbnails'; |
|
|
|
|
|
|
|
|
|
const MODE_FILL = 'fill'; |
|
|
|
|
const MODE_COVER = 'cover'; |
|
|
|
|
|
|
|
|
|
/** @var IRootFolder*/ |
|
|
|
|
private $rootFolder; |
|
|
|
|
/** @var File */ |
|
|
|
|
private $file; |
|
|
|
|
/** @var IPreview */ |
|
|
|
|
private $previewManager; |
|
|
|
|
/** @var IConfig */ |
|
|
|
@ -58,19 +51,16 @@ class Generator { |
|
|
|
|
* @param IRootFolder $rootFolder |
|
|
|
|
* @param IConfig $config |
|
|
|
|
* @param IPreview $previewManager |
|
|
|
|
* @param File $file |
|
|
|
|
* @param IAppData $appData |
|
|
|
|
*/ |
|
|
|
|
public function __construct( |
|
|
|
|
IRootFolder $rootFolder, |
|
|
|
|
IConfig $config, |
|
|
|
|
IPreview $previewManager, |
|
|
|
|
File $file, |
|
|
|
|
IAppData $appData |
|
|
|
|
) { |
|
|
|
|
$this->rootFolder = $rootFolder; |
|
|
|
|
$this->config = $config; |
|
|
|
|
$this->file = $file; |
|
|
|
|
$this->previewManager = $previewManager; |
|
|
|
|
$this->appData = $appData; |
|
|
|
|
} |
|
|
|
@ -81,6 +71,7 @@ class Generator { |
|
|
|
|
* The cache is searched first and if nothing usable was found then a preview is |
|
|
|
|
* generated by one of the providers |
|
|
|
|
* |
|
|
|
|
* @param File $file |
|
|
|
|
* @param int $width |
|
|
|
|
* @param int $height |
|
|
|
|
* @param bool $crop |
|
|
|
@ -88,8 +79,8 @@ class Generator { |
|
|
|
|
* @return ISimpleFile |
|
|
|
|
* @throws NotFoundException |
|
|
|
|
*/ |
|
|
|
|
public function getPreview($width = -1, $height = -1, $crop = false, $mode = Generator::MODE_FILL) { |
|
|
|
|
if (!$this->previewManager->isMimeSupported($this->file->getMimeType())) { |
|
|
|
|
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL) { |
|
|
|
|
if (!$this->previewManager->isMimeSupported($file->getMimeType())) { |
|
|
|
|
throw new NotFoundException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -97,10 +88,10 @@ class Generator { |
|
|
|
|
* Get the preview folder |
|
|
|
|
* TODO: Separate preview creation from storing previews |
|
|
|
|
*/ |
|
|
|
|
$previewFolder = $this->getPreviewFolder(); |
|
|
|
|
$previewFolder = $this->getPreviewFolder($file); |
|
|
|
|
|
|
|
|
|
// Get the max preview and infer the max preview sizes from that |
|
|
|
|
$maxPreview = $this->getMaxPreview($previewFolder); |
|
|
|
|
$maxPreview = $this->getMaxPreview($previewFolder, $file); |
|
|
|
|
list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); |
|
|
|
|
|
|
|
|
|
// Calculate the preview size |
|
|
|
@ -118,10 +109,11 @@ class Generator { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param ISimpleFolder $previewFolder |
|
|
|
|
* @param File $file |
|
|
|
|
* @return ISimpleFile |
|
|
|
|
* @throws NotFoundException |
|
|
|
|
*/ |
|
|
|
|
private function getMaxPreview(ISimpleFolder $previewFolder) { |
|
|
|
|
private function getMaxPreview(ISimpleFolder $previewFolder, File $file) { |
|
|
|
|
$nodes = $previewFolder->getDirectoryListing(); |
|
|
|
|
|
|
|
|
|
foreach ($nodes as $node) { |
|
|
|
@ -132,7 +124,7 @@ class Generator { |
|
|
|
|
|
|
|
|
|
$previewProviders = $this->previewManager->getProviders(); |
|
|
|
|
foreach ($previewProviders as $supportedMimeType => $providers) { |
|
|
|
|
if (!preg_match($supportedMimeType, $this->file->getMimeType())) { |
|
|
|
|
if (!preg_match($supportedMimeType, $file->getMimeType())) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -142,7 +134,7 @@ class Generator { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
list($view, $path) = $this->getViewAndPath($this->file); |
|
|
|
|
list($view, $path) = $this->getViewAndPath($file); |
|
|
|
|
|
|
|
|
|
$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048); |
|
|
|
|
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048); |
|
|
|
@ -239,13 +231,13 @@ class Generator { |
|
|
|
|
* Fill means that the $height and $width are the max |
|
|
|
|
* Cover means min. |
|
|
|
|
*/ |
|
|
|
|
if ($mode === self::MODE_FILL) { |
|
|
|
|
if ($mode === IPreview::MODE_FILL) { |
|
|
|
|
if ($ratioH > $ratioW) { |
|
|
|
|
$height = $width * $ratio; |
|
|
|
|
} else { |
|
|
|
|
$width = $height / $ratio; |
|
|
|
|
} |
|
|
|
|
} else if ($mode === self::MODE_COVER) { |
|
|
|
|
} else if ($mode === IPreview::MODE_COVER) { |
|
|
|
|
if ($ratioH > $ratioW) { |
|
|
|
|
$width = $height / $ratio; |
|
|
|
|
} else { |
|
|
|
@ -352,13 +344,14 @@ class Generator { |
|
|
|
|
/** |
|
|
|
|
* Get the specific preview folder for this file |
|
|
|
|
* |
|
|
|
|
* @param File $file |
|
|
|
|
* @return ISimpleFolder |
|
|
|
|
*/ |
|
|
|
|
private function getPreviewFolder() { |
|
|
|
|
private function getPreviewFolder(File $file) { |
|
|
|
|
try { |
|
|
|
|
$folder = $this->appData->getFolder($this->file->getId()); |
|
|
|
|
$folder = $this->appData->getFolder($file->getId()); |
|
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
$folder = $this->appData->newFolder($this->file->getId()); |
|
|
|
|
$folder = $this->appData->newFolder($file->getId()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $folder; |
|
|
|
|