summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-01-17 11:46:30 +0100
committerMorris Jobke <hey@morrisjobke.de>2018-01-18 13:40:55 +0100
commit3d4c698f448925aca62976175ffd4bab9694c7d6 (patch)
tree6ab0f4840ef17e67cc97426cc323b46dc31f80c0 /lib
parentc121610d5a42b8a30a328c0a2f9704fb20d4c369 (diff)
downloadnextcloud-server-3d4c698f448925aca62976175ffd4bab9694c7d6.tar.gz
nextcloud-server-3d4c698f448925aca62976175ffd4bab9694c7d6.zip
Improve OC_Image code to not guess the type of input, but actually request the specific methods to be called
Followup to #7836 Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Avatar.php19
-rw-r--r--lib/private/Preview/TXT.php5
-rw-r--r--lib/private/PreviewManager.php4
-rw-r--r--lib/private/legacy/image.php50
4 files changed, 38 insertions, 40 deletions
diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php
index afa9118c509..ce3fcff396e 100644
--- a/lib/private/Avatar.php
+++ b/lib/private/Avatar.php
@@ -117,7 +117,24 @@ class Avatar implements IAvatar {
$img = $data;
$data = $img->data();
} else {
- $img = new OC_Image($data);
+ $img = new OC_Image();
+ if (is_resource($data) && get_resource_type($data) === "gd") {
+ $img->setResource($data);
+ } elseif(is_resource($data)) {
+ $img->loadFromFileHandle($data);
+ } else {
+ try {
+ // detect if it is a path or maybe the images as string
+ $result = @realpath($data);
+ if ($result === false || $result === null) {
+ $img->loadFromData($data);
+ } else {
+ $img->loadFromFile($data);
+ }
+ } catch (\Error $e) {
+ $img->loadFromData($data);
+ }
+ }
}
$type = substr($img->mimeType(), -3);
if ($type === 'peg') {
diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php
index a4b7ec769c0..88c6e87f38e 100644
--- a/lib/private/Preview/TXT.php
+++ b/lib/private/Preview/TXT.php
@@ -91,8 +91,9 @@ class TXT extends Provider {
}
}
- $image = new \OC_Image($image);
+ $imageObject = new \OC_Image();
+ $imageObject->setResource($image);
- return $image->valid() ? $image : false;
+ return $imageObject->valid() ? $imageObject : false;
}
}
diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php
index db575a8cbe0..46ef8862e6b 100644
--- a/lib/private/PreviewManager.php
+++ b/lib/private/PreviewManager.php
@@ -166,7 +166,9 @@ class PreviewManager implements IPreview {
return new \OC_Image();
}
- return new \OC_Image($preview->getContent());
+ $previewImage = new \OC_Image();
+ $previewImage->loadFromData($preview->getContent());
+ return $previewImage;
}
/**
diff --git a/lib/private/legacy/image.php b/lib/private/legacy/image.php
index 873f9711d5c..6ad9426a717 100644
--- a/lib/private/legacy/image.php
+++ b/lib/private/legacy/image.php
@@ -69,6 +69,7 @@ class OC_Image implements \OCP\IImage {
* an imagecreate* function.
* @param \OCP\ILogger $logger
* @param \OCP\IConfig $config
+ * @throws \InvalidArgumentException in case the $imageRef parameter is not null
*/
public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) {
$this->logger = $logger;
@@ -85,7 +86,7 @@ class OC_Image implements \OCP\IImage {
}
if ($imageRef !== null) {
- $this->load($imageRef);
+ throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.');
}
}
@@ -298,6 +299,18 @@ class OC_Image implements \OCP\IImage {
}
/**
+ * @param resource Returns the image resource in any.
+ * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd"
+ */
+ public function setResource($resource) {
+ if (get_resource_type($resource) === 'gd') {
+ $this->resource = $resource;
+ return;
+ }
+ throw new \InvalidArgumentException('Supplied resource is not of type "gd".');
+ }
+
+ /**
* @return resource Returns the image resource in any.
*/
public function resource() {
@@ -504,31 +517,6 @@ class OC_Image implements \OCP\IImage {
}
/**
- * Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
- *
- * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle ).
- * @return resource|false An image resource or false on error
- */
- public function load($imageRef) {
- if (is_resource($imageRef)) {
- if (get_resource_type($imageRef) === 'gd') {
- $this->resource = $imageRef;
- return $this->resource;
- } elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) {
- return $this->loadFromFileHandle($imageRef);
- }
- } elseif ($this->loadFromBase64($imageRef) !== false) {
- return $this->resource;
- } elseif ($this->loadFromFile($imageRef) !== false) {
- return $this->resource;
- } elseif ($this->loadFromData($imageRef) !== false) {
- return $this->resource;
- }
- $this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
- return false;
- }
-
- /**
* Loads an image from an open file handle.
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
*
@@ -550,16 +538,6 @@ class OC_Image implements \OCP\IImage {
* @return bool|resource An image resource or false on error
*/
public function loadFromFile($imagePath = false) {
- try {
- // detect if it is a path or maybe the images as string
- // needed because the constructor iterates over all load* methods
- $result = @realpath($imagePath);
- if ($result === false) {
- return false;
- }
- } catch (Error $e) {
- return false;
- }
// exif_imagetype throws "read error!" if file is less than 12 byte
if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
return false;