diff options
author | Olivier Paroz <github@oparoz.com> | 2015-01-10 00:00:20 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-01-10 00:00:20 +0300 |
commit | b73f5cce23e875c99cb66e6bcbaa3249b1d255b6 (patch) | |
tree | 555b66ab2492c2eeb08745999590dec9b552a6aa | |
parent | 0e9271b1424837b8de44c47eea14867ea2dce28d (diff) |
Some optimisations
-rw-r--r-- | service/previewservice.php | 112 | ||||
-rw-r--r-- | utility/normalizer.php | 11 |
2 files changed, 75 insertions, 48 deletions
diff --git a/service/previewservice.php b/service/previewservice.php index 9b80c070..73f68064 100644 --- a/service/previewservice.php +++ b/service/previewservice.php @@ -219,21 +219,51 @@ class PreviewService extends Service { * @return bool */ private function previewRequired($file, $preview) { + $svgPreviewRequired = $this->isSvgPreviewRequired($file, $preview); + $gifPreviewRequired = $this->isGifPreviewRequired($file); + + return $svgPreviewRequired && $gifPreviewRequired && !$this->download; + } + + /** + * Decides if we should download the SVG or generate a preview + * + * @param File $file + * @param \OC\Preview $preview + * + * @return bool + */ + private function isSvgPreviewRequired($file, $preview) { + $mime = $file->getMimeType(); + + /** + * SVGs are downloaded if the SVG converter is disabled + * Files of any media type are downloaded if requested by the client + */ + if ($mime === 'image/svg+xml' && !$preview->isMimeSupported($mime)) { + return false; + } + + return true; + } + + /** + * Decides if we should download the GIF or generate a preview + * + * @param File $file + * + * @return bool + */ + private function isGifPreviewRequired($file) { $animatedPreview = $this->animatedPreview; - $download = $this->download; $mime = $file->getMimeType(); $animatedGif = $this->isGifAnimated($file); /** * GIFs are downloaded if they're animated and we want to show * animations - * SVGs are downloaded if the SVG converter is disabled - * Files of any media type are downloaded if requested by the client */ - if (($mime === 'image/gif' && $animatedPreview && $animatedGif) - || ($mime === 'image/svg+xml' && !$preview->isMimeSupported($mime) - || $download === true) - ) { + if ($mime === 'image/gif' && $animatedPreview && $animatedGif) { return false; } @@ -241,6 +271,40 @@ class PreviewService extends Service { } /** + * Tests if a GIF is animated + * + * @link http://php.net/manual/en/function.imagecreatefromgif.php#104473 + * + * @param File $file + * + * @return bool + */ + private function isGifAnimated($file) { + $fileHandle = $file->fopen('rb'); + $count = 0; + /** + * An animated gif contains multiple "frames", with each frame having a + * header made up of: + * * a static 4-byte sequence (\x00\x21\xF9\x04) + * * 4 variable bytes + * * a static 2-byte sequence (\x00\x2C) (Photoshop uses \x00\x21) + * + * We read through the file until we reach the end of the file, or we've + * found at least 2 frame headers + */ + while (!feof($fileHandle) && $count < 2) { + $chunk = fread($fileHandle, 1024 * 100); //read 100kb at a time + $count += preg_match_all( + '#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches + ); + } + + fclose($fileHandle); + + return $count > 1; + } + + /** * Returns a preview based on OC's preview class and our custom methods * * @param string $owner @@ -293,40 +357,6 @@ class PreviewService extends Service { } /** - * Tests if a GIF is animated - * - * @link http://php.net/manual/en/function.imagecreatefromgif.php#104473 - * - * @param File $file - * - * @return bool - */ - private function isGifAnimated($file) { - $fileHandle = $file->fopen('rb'); - $count = 0; - /** - * An animated gif contains multiple "frames", with each frame having a - * header made up of: - * * a static 4-byte sequence (\x00\x21\xF9\x04) - * * 4 variable bytes - * * a static 2-byte sequence (\x00\x2C) (Photoshop uses \x00\x21) - * - * We read through the file until we reach the end of the file, or we've - * found at least 2 frame headers - */ - while (!feof($fileHandle) && $count < 2) { - $chunk = fread($fileHandle, 1024 * 100); //read 100kb at a time - $count += preg_match_all( - '#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches - ); - } - - fclose($fileHandle); - - return $count > 1; - } - - /** * Makes sure we return previews of the asked dimensions and fix the cache * if necessary * diff --git a/utility/normalizer.php b/utility/normalizer.php index 09dcaace..a5228756 100644 --- a/utility/normalizer.php +++ b/utility/normalizer.php @@ -31,23 +31,20 @@ class Normalizer { * @return string|array */ public function normalize($data, $depth = 0) { - $scalar = $this->normalizeScalar($data); + $traversable = $this->normalizeTraversable($data, $depth); + $object = $this->normalizeObject($data, $depth); + $resource = $this->normalizeResource($data); + if (!is_array($scalar)) { return $scalar; } - - $traversable = $this->normalizeTraversable($data, $depth); if ($traversable !== null) { return $traversable; } - - $object = $this->normalizeObject($data, $depth); if ($object !== null) { return $object; } - - $resource = $this->normalizeResource($data); if ($resource !== null) { return $resource; } |