From 65a2f83ed1da4cfec2f01749ca3b91bea48c14d3 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 12 Sep 2024 05:08:22 +0200 Subject: [PATCH] Update 2024-09-12 05:08:23 --- .../code.php | 7 +- .../code.power | 6 +- .../settings.json | 4 +- .../code.php | 2 +- .../code.power | 2 +- .../README.md | 4 +- .../code.php | 20 ++-- .../code.power | 14 +-- .../settings.json | 2 +- .../README.md | 21 +++- .../code.php | 97 +++++++++++++++---- 11 files changed, 126 insertions(+), 53 deletions(-) diff --git a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.php b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.php index bb7f297..479f41b 100644 --- a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.php +++ b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.php @@ -12,6 +12,7 @@ namespace VDM\Joomla\Componentbuilder\File; +use Joomla\CMS\Language\Text; use VDM\Joomla\Interfaces\Data\ItemInterface as Item; use VDM\Joomla\Interfaces\Data\ItemsInterface as Items; use VDM\Joomla\Data\Guid; @@ -120,7 +121,7 @@ final class Manager // store file in the file table $this->item->set( - $this->modelFileDetails($details, $guid) + $this->modelFileDetails($details, $guid, $entity, $target) ); } @@ -153,13 +154,13 @@ final class Manager protected function modelFileDetails(array $details, string $guid, string $entity, string $target): object { return (object) [ - 'name' => 'soon', + 'name' => $details['name'], 'file_type' => $guid, 'ext' => 'me', 'size_kb' => 45, 'filepath' => $details['full_path'], 'entity_type' => $target, - 'entity' => $guid, + 'entity' => $entity, 'guid' => $this->getGuid('file'), ]; } diff --git a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.power b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.power index 1773e50..7dbcc49 100644 --- a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.power +++ b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/code.power @@ -92,7 +92,7 @@ // store file in the file table $this->item->set( - $this->modelFileDetails($details, $guid) + $this->modelFileDetails($details, $guid, $entity, $target) ); } @@ -125,13 +125,13 @@ protected function modelFileDetails(array $details, string $guid, string $entity, string $target): object { return (object) [ - 'name' => 'soon', + 'name' => $details['name'], 'file_type' => $guid, 'ext' => 'me', 'size_kb' => 45, 'filepath' => $details['full_path'], 'entity_type' => $target, - 'entity' => $guid, + 'entity' => $entity, 'guid' => $this->getGuid('file'), ]; } \ No newline at end of file diff --git a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/settings.json b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/settings.json index 33c6aac..95d452e 100644 --- a/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/settings.json +++ b/src/67a5e0ca-0ff0-4979-9b41-da0b09988016/settings.json @@ -1,5 +1,5 @@ { - "add_head": "0", + "add_head": "1", "add_licensing_template": "2", "extends": "", "guid": "67a5e0ca-0ff0-4979-9b41-da0b09988016", @@ -35,6 +35,6 @@ "namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Manager", "description": "File Manager Class\r\n\r\n@since 5.0.2", "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe \r\n * @git Joomla Component Builder \r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n", - "head": "", + "head": "use Joomla\\CMS\\Language\\Text;", "composer": "" } \ No newline at end of file diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php index 864ba67..e87f382 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php @@ -53,7 +53,7 @@ final class Display */ public function get(string $entity, string $target): ?array { - if (($files = $this->items->table->('file')->get([$entity], 'entity')) !== null) + if (($files = $this->items->table('file')->get([$entity], 'entity')) !== null) { foreach ($files as $n => $file) { diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power index da9dfb3..07f81cd 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power @@ -29,7 +29,7 @@ */ public function get(string $entity, string $target): ?array { - if (($files = $this->items->table->('file')->get([$entity], 'entity')) !== null) + if (($files = $this->items->table('file')->get([$entity], 'entity')) !== null) { foreach ($files as $n => $file) { diff --git a/src/d7600b43-771a-4747-9f5d-952765721799/README.md b/src/d7600b43-771a-4747-9f5d-952765721799/README.md index 2ac2ed9..6633a7d 100644 --- a/src/d7600b43-771a-4747-9f5d-952765721799/README.md +++ b/src/d7600b43-771a-4747-9f5d-952765721799/README.md @@ -14,7 +14,7 @@ abstract UploadHelper #Orange { + {static} get(string $field, string $type, ...) : ?array + {static} getError(bool $toString = false) : array|string - # {static} check(array $upload, string $type) : array|null + # {static} check(array $upload, string $type) : ?array # {static} remove(string $fullPath) : boolean # {static} setError(string $message) : void } @@ -42,7 +42,7 @@ end note note right of UploadHelper::check Check a file and verifies it as a allowed file format file - return: array|null + return: ?array end note note right of UploadHelper::remove diff --git a/src/d7600b43-771a-4747-9f5d-952765721799/code.php b/src/d7600b43-771a-4747-9f5d-952765721799/code.php index af72277..0cfb5c3 100644 --- a/src/d7600b43-771a-4747-9f5d-952765721799/code.php +++ b/src/d7600b43-771a-4747-9f5d-952765721799/code.php @@ -14,9 +14,9 @@ namespace VDM\Joomla\Utilities; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; -use Joomla\CMS\Filesystem\File; -use Joomla\CMS\Filesystem\Folder; -use Joomla\CMS\Filesystem\Path; +use Joomla\Filesystem\File; +use Joomla\Filesystem\Folder; +use Joomla\Filesystem\Path; use VDM\Joomla\Utilities\Component\Helper; @@ -84,9 +84,9 @@ abstract class UploadHelper /** * Get file/files from a HTTP upload. * - * @param string $field The input field name - * @param string $type The file type - * @param string|null $filter The filter to use when uploading the file + * @param string $field The input field name + * @param string $type The file type + * @param string|null $filter The filter to use when uploading the file * @param string|null $path The path to the directory where the file must be placed * * @return array|null File details or false on failure. @@ -187,7 +187,7 @@ abstract class UploadHelper // check that his name has file format if (is_string($name) && strpos($name, '.') === false) { - $name = $name . '.' . MimeHelper::extension(null, $userfile['type']); + $name = $name . '.' . MimeHelper::extension($userfile['name']); } $userfile['file_name'] = $name; } @@ -231,10 +231,10 @@ abstract class UploadHelper * @return array|null of elements * */ - protected static function check(array $upload, string $type)? ?array + protected static function check(array $upload, string $type): ?array { // Default formats - $formats = MimeHelper::extensions($type); + $formats = MimeHelper::getFileExtensions($type); // Clean the path $upload_path = Path::clean($upload['full_path']); @@ -250,7 +250,7 @@ abstract class UploadHelper { // get allowed formats $legal_formats = (array) Helper::getParams()->get($type . '_formats', []); - $legal = array_values(array_unique(array_merge($legal_formats, static::$legalFormats))) + $legal = array_values(array_unique(array_merge($legal_formats, static::$legalFormats))); } // check the extension diff --git a/src/d7600b43-771a-4747-9f5d-952765721799/code.power b/src/d7600b43-771a-4747-9f5d-952765721799/code.power index a399991..2e0115e 100644 --- a/src/d7600b43-771a-4747-9f5d-952765721799/code.power +++ b/src/d7600b43-771a-4747-9f5d-952765721799/code.power @@ -55,9 +55,9 @@ /** * Get file/files from a HTTP upload. * - * @param string $field The input field name - * @param string $type The file type - * @param string|null $filter The filter to use when uploading the file + * @param string $field The input field name + * @param string $type The file type + * @param string|null $filter The filter to use when uploading the file * @param string|null $path The path to the directory where the file must be placed * * @return array|null File details or false on failure. @@ -158,7 +158,7 @@ // check that his name has file format if (is_string($name) && strpos($name, '.') === false) { - $name = $name . '.' . MimeHelper::extension(null, $userfile['type']); + $name = $name . '.' . MimeHelper::extension($userfile['name']); } $userfile['file_name'] = $name; } @@ -202,10 +202,10 @@ * @return array|null of elements * */ - protected static function check(array $upload, string $type)? ?array + protected static function check(array $upload, string $type): ?array { // Default formats - $formats = MimeHelper::extensions($type); + $formats = MimeHelper::getFileExtensions($type); // Clean the path $upload_path = Path::clean($upload['full_path']); @@ -221,7 +221,7 @@ { // get allowed formats $legal_formats = (array) Helper::getParams()->get($type . '_formats', []); - $legal = array_values(array_unique(array_merge($legal_formats, static::$legalFormats))) + $legal = array_values(array_unique(array_merge($legal_formats, static::$legalFormats))); } // check the extension diff --git a/src/d7600b43-771a-4747-9f5d-952765721799/settings.json b/src/d7600b43-771a-4747-9f5d-952765721799/settings.json index bdc5840..7eee705 100644 --- a/src/d7600b43-771a-4747-9f5d-952765721799/settings.json +++ b/src/d7600b43-771a-4747-9f5d-952765721799/settings.json @@ -26,6 +26,6 @@ "namespace": "[[[NamespacePrefix]]]\\Joomla\\Utilities.UploadHelper", "description": "Upload Helper\r\n\r\n@since 3.0.11", "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe \r\n * @git Joomla Component Builder \r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n", - "head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\Language\\Text;\r\nuse Joomla\\CMS\\Filesystem\\File;\r\nuse Joomla\\CMS\\Filesystem\\Folder;\r\nuse Joomla\\CMS\\Filesystem\\Path;", + "head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\Language\\Text;\r\nuse Joomla\\Filesystem\\File;\r\nuse Joomla\\Filesystem\\Folder;\r\nuse Joomla\\Filesystem\\Path;", "composer": "" } \ No newline at end of file diff --git a/src/f11dc790-713e-4706-9a85-a318ed3ad56e/README.md b/src/f11dc790-713e-4706-9a85-a318ed3ad56e/README.md index 69b9361..33ad7a4 100644 --- a/src/f11dc790-713e-4706-9a85-a318ed3ad56e/README.md +++ b/src/f11dc790-713e-4706-9a85-a318ed3ad56e/README.md @@ -12,13 +12,30 @@ ```uml @startuml abstract MimeHelper #Orange { + + {static} extension(string $file) : string + {static} mimeType(string $file) : string + # {static} mimeTypeFilename(string $file) : string + {static} getFileExtensions(string $target = null, boolean $sorted = false) : array } -note right of MimeHelper::mimeType - Get the mime type based on file extension +note right of MimeHelper::extension + Get the file extension from a full path OR file name. + since: 5.0.3 + return: string +end note + +note right of MimeHelper::mimeType + Get the mime type based on the full file path. + + since: 5.0.3 + return: string +end note + +note right of MimeHelper::mimeTypeFilename + Get the mime type based on the file extension from a file name. + + since: 5.0.3 return: string end note diff --git a/src/f11dc790-713e-4706-9a85-a318ed3ad56e/code.php b/src/f11dc790-713e-4706-9a85-a318ed3ad56e/code.php index 2fc06df..2f57706 100644 --- a/src/f11dc790-713e-4706-9a85-a318ed3ad56e/code.php +++ b/src/f11dc790-713e-4706-9a85-a318ed3ad56e/code.php @@ -1015,14 +1015,34 @@ abstract class MimeHelper /** - * Get the mime type based on file extension + * Get the file extension from a full path OR file name. * - * @param string $file The file name or path + * @param string $file The full file path or file name. * - * @return string the mime type on success - * + * @return string The file extension in lowercase or an empty string if none found. + * @since 5.0.3 */ - public static function mimeType($file) + public static function extension(string $file): string + { + // Ensure the input is a valid string and contains a file extension + if (strpos($file, '.') === false) + { + return ''; + } + + // Extract and return the extension from the path + return strtolower(pathinfo($file, PATHINFO_EXTENSION)); + } + + /** + * Get the mime type based on the full file path. + * + * @param string $file The full file path. + * + * @return string The mime type or 'application/octet-stream' if none found. + * @since 5.0.3 + */ + public static function mimeType(string $file): string { /** * **DISCLAIMER** @@ -1030,26 +1050,61 @@ abstract class MimeHelper * array. It does not guarantee that the file is TRULY that * of the extension that this function returns. * https://gist.github.com/Llewellynvdm/74be373357e131b8775a7582c3de508b - */ + */ - // get the extension form file - $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); - // check if we have the extension listed - if (isset(self::$fileExtensionToMimeType[$extension])) + // Ensure the file exists to avoid unnecessary processing + if (!is_file($file) || !is_readable($file)) + { + return self::mimeTypeFilename($file); + } + + // Use finfo to get MIME type based on file content if available + if (function_exists('finfo_open')) + { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mimetype = finfo_file($finfo, $file); + finfo_close($finfo); + + if ($mimetype) + { + return $mimetype; + } + } + + // Fallback to mime_content_type() if available + if (function_exists('mime_content_type')) + { + $mimetype = mime_content_type($file); + if ($mimetype) + { + return $mimetype; + } + } + + // Fallback to application/octet-stream as a generic binary stream type + return 'application/octet-stream'; + } + + /** + * Get the mime type based on the file extension from a file name. + * + * @param string $file The file name (without path). + * + * @return string The mime type or 'application/octet-stream' if none found. + * @since 5.0.3 + */ + protected static function mimeTypeFilename(string $file): string + { + // Get the extension + $extension = self::extension($file); + + // Predefined MIME types by extension for quick lookup + if (!empty($extension) && isset(self::$fileExtensionToMimeType[$extension])) { return self::$fileExtensionToMimeType[$extension]; } - elseif (function_exists('mime_content_type')) - { - return mime_content_type($file); - } - elseif (function_exists('finfo_open')) - { - $finfo = finfo_open(FILEINFO_MIME); - $mimetype = finfo_file($finfo, $file); - finfo_close($finfo); - return $mimetype; - } + + // Fallback to application/octet-stream if extension is unknown return 'application/octet-stream'; }