From 4a7f92250af5985e76b6cccf698dbf876b44577e Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Fri, 14 May 2021 15:15:09 +0200 Subject: [PATCH 1/4] Asset path -> data --- src/Assets/Asset.php | 26 +++++++++++++++++++------- src/Util/File.php | 20 ++++++++++++++++++++ src/Util/Str.php | 17 +++++++++++++++++ src/Util/Url.php | 9 +++++---- 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index ce878a774..29e3f3522 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -34,7 +34,7 @@ class Asset implements \ArrayAccess protected $minified = false; /** - * Creates an Asset from file(s) path. + * Creates an Asset from a source. * * $options[ * 'fingerprint' => true, @@ -43,15 +43,15 @@ class Asset implements \ArrayAccess * 'ignore_missing' => false, * ]; * - * @param Builder $builder - * @param string|array $path - * @param array|null $options + * @param Builder $builder + * @param mixed $source + * @param array|null $options */ - public function __construct(Builder $builder, $path, array $options = null) + public function __construct(Builder $builder, $source, array $options = null) { $this->builder = $builder; $this->config = $builder->getConfig(); - $path = is_array($path) ? $path : [$path]; + $source = is_array($source) ? $source : [$source]; // handles options $fingerprint = (bool) $this->config->get('assets.fingerprint.enabled'); @@ -60,7 +60,19 @@ public function __construct(Builder $builder, $path, array $options = null) $ignore_missing = false; extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); - // loads file(s) + // determines source type + switch (true) { + case Util\Str::isBinary(): + //return $this->initFromBinary($this->data); + case Util\Url::isUrl(): + //return $this->initFromUrl($this->data); + case Util\File::isFilePath(): + //return $this->initFromPath($this->data); + default: + throw new Exception('Asset source not readable'); + } + + // loads $file = []; $prevType = ''; $prevExt = ''; diff --git a/src/Util/File.php b/src/Util/File.php index 4668bd530..34cd9c124 100644 --- a/src/Util/File.php +++ b/src/Util/File.php @@ -77,4 +77,24 @@ public static function getMimeType(string $filename): array $subtype, ]; } + + /** + * Determines if data is file path + * + * @param mixed $data + * + * @return bool + */ + public static function isFilePath($data): bool + { + if (is_string($data)) { + try { + return is_file($data); + } catch (\Exception $e) { + return false; + } + } + + return false; + } } diff --git a/src/Util/Str.php b/src/Util/Str.php index 586640745..1407fb667 100644 --- a/src/Util/Str.php +++ b/src/Util/Str.php @@ -57,4 +57,21 @@ public static function combineArrayToString( return substr($string, 0, -2); } + + /** + * Determines if data is binary. + * + * @param mixed $data + * + * @return bool + */ + public static function isBinary($data): bool + { + if (is_string($data)) { + $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data); + return $mime != 'application/x-empty'; + } + + return false; + } } diff --git a/src/Util/Url.php b/src/Util/Url.php index 9fd37597a..42fecdf1d 100644 --- a/src/Util/Url.php +++ b/src/Util/Url.php @@ -13,15 +13,16 @@ class Url { /** - * Tests if a string is an URL. + * Tests if data is a valid URL. * - * @param string $url + * @param mixed $data * * @return bool */ - public static function isUrl(string $url): bool + public static function isUrl($data): bool { - return (bool) preg_match('~^(?:f|ht)tps?://~i', $url); + //return (bool) preg_match('~^(?:f|ht)tps?://~i', $data); + return (bool) filter_var($data, FILTER_VALIDATE_URL); } /** From c0212e1f23d4c8c5eda580495ac127c9538bacc8 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Fri, 14 May 2021 15:17:57 +0200 Subject: [PATCH 2/4] Apply fixes from StyleCI (#1051) --- src/Util/File.php | 2 +- src/Util/Str.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Util/File.php b/src/Util/File.php index 34cd9c124..c6b32b2ce 100644 --- a/src/Util/File.php +++ b/src/Util/File.php @@ -79,7 +79,7 @@ public static function getMimeType(string $filename): array } /** - * Determines if data is file path + * Determines if data is file path. * * @param mixed $data * diff --git a/src/Util/Str.php b/src/Util/Str.php index 1407fb667..e3b14a7af 100644 --- a/src/Util/Str.php +++ b/src/Util/Str.php @@ -58,7 +58,7 @@ public static function combineArrayToString( return substr($string, 0, -2); } - /** + /** * Determines if data is binary. * * @param mixed $data @@ -69,6 +69,7 @@ public static function isBinary($data): bool { if (is_string($data)) { $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data); + return $mime != 'application/x-empty'; } From 9d789454847d35066f257860fba2e0f110d3e57d Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sun, 16 May 2021 22:26:36 +0200 Subject: [PATCH 3/4] WIP --- src/Assets/Asset.php | 46 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 29e3f3522..b83576cb3 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -60,28 +60,26 @@ public function __construct(Builder $builder, $source, array $options = null) $ignore_missing = false; extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); - // determines source type - switch (true) { - case Util\Str::isBinary(): - //return $this->initFromBinary($this->data); - case Util\Url::isUrl(): - //return $this->initFromUrl($this->data); - case Util\File::isFilePath(): - //return $this->initFromPath($this->data); - default: - throw new Exception('Asset source not readable'); - } - // loads $file = []; $prevType = ''; $prevExt = ''; - foreach ($path as $p) { - $file = $this->loadFile($p, $ignore_missing); - if ($file['missing']) { - $this->data['path'] = ''; - - continue; + foreach ($source as $s) { + // determines source type + switch (true) { + case Util\Str::isBinary(): + //return $this->initFromBinary($this->data); + case Util\Url::isUrl(): + //return $this->initFromUrl($this->data); + case Util\File::isFilePath(): + $file = $this->loadFile($s, $ignore_missing); + if ($file['missing']) { + $this->data['path'] = ''; + + continue; + } + default: + throw new Exception('Asset source not readable'); } // bundle: same type only @@ -112,7 +110,7 @@ public function __construct(Builder $builder, $source, array $options = null) $prevExt = $file['ext']; } // bundle: define path - if (count($path) > 1) { + if (count($source) > 1) { $this->data['path'] = $filename; if (empty($filename)) { switch ($this->data['ext']) { @@ -333,6 +331,9 @@ public function getIntegrity(string $algo = 'sha384'): string */ public function getWidth() { + if ($this->data['type'] != 'image') { + throw new Exception(\sprintf('Can\'t get width of a non image asset ("%s")', $this->data['path'])); + } if (false === $size = $this->getImageSize()) { return false; } @@ -347,6 +348,9 @@ public function getWidth() */ public function getHeight() { + if ($this->data['type'] != 'image') { + throw new Exception(\sprintf('Can\'t get height of a non image asset ("%s")', $this->data['path'])); + } if (false === $size = $this->getImageSize()) { return false; } @@ -363,6 +367,10 @@ public function getHeight() */ public function getAudio(): Mp3Info { + if ($this->data['subtype'] != 'audio/mpeg') { + throw new Exception(\sprintf('Can\'t get audio infos of a non MP3 asset ("%s")', $this->data['path'])); + } + return new Mp3Info($this->data['file']); } From fc7e657a26efa915a68fb9917348a5213f1cca64 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sun, 16 May 2021 22:26:55 +0200 Subject: [PATCH 4/4] Apply fixes from StyleCI (#1052) --- src/Assets/Asset.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index b83576cb3..07c5c0063 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -76,7 +76,7 @@ public function __construct(Builder $builder, $source, array $options = null) if ($file['missing']) { $this->data['path'] = ''; - continue; + break; } default: throw new Exception('Asset source not readable');