commit d4f6fb683da8d016e5299303bc421edc354b41da
parent dec280ead9a7c37260b0e2b811e134209355a8d1
Author: Felicitus <felicitus@felicitus.org>
Date: Tue, 10 Jul 2012 21:40:55 +0200
Cache the isImage flag in the database
Diffstat:
1 file changed, 43 insertions(+), 3 deletions(-)
diff --git a/src/backend/PartKeepr/Part/PartAttachment.php b/src/backend/PartKeepr/Part/PartAttachment.php
@@ -16,6 +16,13 @@ class PartAttachment extends UploadedFile implements Serializable, Deserializabl
* @var string
*/
private $description;
+
+ /**
+ * Defines if the attachment is an image.
+ * @Column(type="boolean",nullable=true)
+ * @var boolean
+ */
+ private $isImage;
/**
* Creates a new part attachment
@@ -23,6 +30,7 @@ class PartAttachment extends UploadedFile implements Serializable, Deserializabl
public function __construct () {
parent::__construct();
$this->setType("PartAttachment");
+ $this->isImage = null;
}
/**
@@ -109,12 +117,25 @@ class PartAttachment extends UploadedFile implements Serializable, Deserializabl
/**
* Returns if the attachment is an image or not.
*
- * Ths method uses ImageMagick to find out if this is an image. Limitations apply; if ImageMagick doesn't support
- * the image format, this method would return false, even if it is an image.
- *
* @return True if the attachment is an image, false otherwise
*/
public function isImage () {
+ if ($this->isImage === null) {
+ $this->isImage = $this->canImagickParseImage();
+ }
+
+ return $this->isImage;
+ }
+
+ /**
+ * Checks if ImageMagick can parse the image.
+ *
+ * This method uses ImageMagick to find out if this is an image. Limitations apply; if ImageMagick doesn't support
+ * the image format, this method would return false, even if it is an image.
+ *
+ * @return boolean true if ImageMagick can parse the file, false otherwise
+ */
+ private function canImagickParseImage () {
/**
* Special case: Check if it's a PDF. If yes, return immediately.
* This is because ImageMagick outputs warning messages for malformed PDF files, and halts the execution
@@ -131,4 +152,23 @@ class PartAttachment extends UploadedFile implements Serializable, Deserializabl
return false;
}
}
+
+ /**
+ * Replaces the current file with a new file.
+ *
+ * @param string $path The path to the original file
+ */
+ public function replace ($path) {
+ $this->isImage = $this->canImagickParseImage();
+ parent::replace($path);
+ }
+
+ /**
+ * Replaces the file from an URL. Does some tricks to avoid 403 forbidden on some sites.
+ * @param string $url
+ */
+ public function replaceFromURL ($url) {
+ $this->isImage = $this->canImagickParseImage();
+ parent::replaceFromURL($url);
+ }
}
\ No newline at end of file