allow generating multiple preview sizes for a single file at once
this saves having to do some of the overhead multiple times Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
981278a666
commit
5cd12cd7c3
|
@ -91,22 +91,37 @@ class Generator {
|
||||||
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
||||||
*/
|
*/
|
||||||
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
|
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
|
||||||
|
$specification = [
|
||||||
|
'width' => $width,
|
||||||
|
'height' => $height,
|
||||||
|
'crop' => $crop,
|
||||||
|
'mode' => $mode,
|
||||||
|
];
|
||||||
|
$this->eventDispatcher->dispatch(
|
||||||
|
IPreview::EVENT,
|
||||||
|
new GenericEvent($file, $specification)
|
||||||
|
);
|
||||||
|
|
||||||
|
// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
|
||||||
|
return $this->generatePreviews($file, [$specification], $mimeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates previews of a file
|
||||||
|
*
|
||||||
|
* @param File $file
|
||||||
|
* @param array $specifications
|
||||||
|
* @param string $mimeType
|
||||||
|
* @return ISimpleFile the last preview that was generated
|
||||||
|
* @throws NotFoundException
|
||||||
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
||||||
|
*/
|
||||||
|
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
|
||||||
//Make sure that we can read the file
|
//Make sure that we can read the file
|
||||||
if (!$file->isReadable()) {
|
if (!$file->isReadable()) {
|
||||||
throw new NotFoundException('Cannot read file');
|
throw new NotFoundException('Cannot read file');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$this->eventDispatcher->dispatch(
|
|
||||||
IPreview::EVENT,
|
|
||||||
new GenericEvent($file, [
|
|
||||||
'width' => $width,
|
|
||||||
'height' => $height,
|
|
||||||
'crop' => $crop,
|
|
||||||
'mode' => $mode
|
|
||||||
])
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($mimeType === null) {
|
if ($mimeType === null) {
|
||||||
$mimeType = $file->getMimeType();
|
$mimeType = $file->getMimeType();
|
||||||
}
|
}
|
||||||
|
@ -128,7 +143,15 @@ class Generator {
|
||||||
throw new NotFoundException('Max preview size 0, invalid!');
|
throw new NotFoundException('Max preview size 0, invalid!');
|
||||||
}
|
}
|
||||||
|
|
||||||
list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview, $previewVersion);
|
[$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
|
||||||
|
|
||||||
|
$preview = null;
|
||||||
|
|
||||||
|
foreach ($specifications as $specification) {
|
||||||
|
$width = $specification['width'] ?? -1;
|
||||||
|
$height = $specification['height'] ?? -1;
|
||||||
|
$crop = $specification['crop'] ?? false;
|
||||||
|
$mode = $specification['mode'] ?? IPreview::MODE_FILL;
|
||||||
|
|
||||||
// If both width and heigth are -1 we just want the max preview
|
// If both width and heigth are -1 we just want the max preview
|
||||||
if ($width === -1 && $height === -1) {
|
if ($width === -1 && $height === -1) {
|
||||||
|
@ -137,11 +160,13 @@ class Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the preview size
|
// Calculate the preview size
|
||||||
list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
|
[$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
|
||||||
|
|
||||||
// No need to generate a preview that is just the max preview
|
// No need to generate a preview that is just the max preview
|
||||||
if ($width === $maxWidth && $height === $maxHeight) {
|
if ($width === $maxWidth && $height === $maxHeight) {
|
||||||
return $maxPreview;
|
// ensure correct return value if this was the last one
|
||||||
|
$preview = $maxPreview;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get a cached preview. Else generate (and store) one
|
// Try to get a cached preview. Else generate (and store) one
|
||||||
|
@ -159,6 +184,7 @@ class Generator {
|
||||||
$preview->delete();
|
$preview->delete();
|
||||||
throw new NotFoundException('Cached preview size 0, invalid!');
|
throw new NotFoundException('Cached preview size 0, invalid!');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $preview;
|
return $preview;
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,6 +151,22 @@ class PreviewManager implements IPreview {
|
||||||
return !empty($this->providers);
|
return !empty($this->providers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getGenerator(): Generator {
|
||||||
|
if ($this->generator === null) {
|
||||||
|
$this->generator = new Generator(
|
||||||
|
$this->config,
|
||||||
|
$this,
|
||||||
|
$this->appData,
|
||||||
|
new GeneratorHelper(
|
||||||
|
$this->rootFolder,
|
||||||
|
$this->config
|
||||||
|
),
|
||||||
|
$this->eventDispatcher
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $this->generator;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a preview of a file
|
* Returns a preview of a file
|
||||||
*
|
*
|
||||||
|
@ -169,20 +185,22 @@ class PreviewManager implements IPreview {
|
||||||
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
|
* @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
|
||||||
*/
|
*/
|
||||||
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
|
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
|
||||||
if ($this->generator === null) {
|
return $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType);
|
||||||
$this->generator = new Generator(
|
|
||||||
$this->config,
|
|
||||||
$this,
|
|
||||||
$this->appData,
|
|
||||||
new GeneratorHelper(
|
|
||||||
$this->rootFolder,
|
|
||||||
$this->config
|
|
||||||
),
|
|
||||||
$this->eventDispatcher
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
|
/**
|
||||||
|
* Generates previews of a file
|
||||||
|
*
|
||||||
|
* @param File $file
|
||||||
|
* @param array $specifications
|
||||||
|
* @param string $mimeType
|
||||||
|
* @return ISimpleFile the last preview that was generated
|
||||||
|
* @throws NotFoundException
|
||||||
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
||||||
|
* @since 19.0.0
|
||||||
|
*/
|
||||||
|
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
|
||||||
|
return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -115,4 +115,17 @@ interface IPreview {
|
||||||
* @since 8.0.0
|
* @since 8.0.0
|
||||||
*/
|
*/
|
||||||
public function isAvailable(\OCP\Files\FileInfo $file);
|
public function isAvailable(\OCP\Files\FileInfo $file);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates previews of a file
|
||||||
|
*
|
||||||
|
* @param File $file
|
||||||
|
* @param array $specifications
|
||||||
|
* @param string $mimeType
|
||||||
|
* @return ISimpleFile the last preview that was generated
|
||||||
|
* @throws NotFoundException
|
||||||
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
||||||
|
* @since 19.0.0
|
||||||
|
*/
|
||||||
|
public function generatePreviews(File $file, array $specifications, $mimeType = null);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue