Update 2024-09-12 05:08:23

This commit is contained in:
Robot 2024-09-12 05:08:22 +02:00
parent b0cdf9d36e
commit 65a2f83ed1
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
11 changed files with 126 additions and 53 deletions

View File

@ -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'),
];
}

View File

@ -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'),
];
}

View File

@ -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 <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/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": ""
}

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/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": ""
}

View File

@ -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

View File

@ -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
*
* @return string the mime type on success
* @param string $file The full file path or file name.
*
* @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**
@ -1032,24 +1052,59 @@ abstract class MimeHelper
* 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';
}