Update 2024-09-12 05:08:23
This commit is contained in:
parent
b0cdf9d36e
commit
65a2f83ed1
@ -12,6 +12,7 @@
|
|||||||
namespace VDM\Joomla\Componentbuilder\File;
|
namespace VDM\Joomla\Componentbuilder\File;
|
||||||
|
|
||||||
|
|
||||||
|
use Joomla\CMS\Language\Text;
|
||||||
use VDM\Joomla\Interfaces\Data\ItemInterface as Item;
|
use VDM\Joomla\Interfaces\Data\ItemInterface as Item;
|
||||||
use VDM\Joomla\Interfaces\Data\ItemsInterface as Items;
|
use VDM\Joomla\Interfaces\Data\ItemsInterface as Items;
|
||||||
use VDM\Joomla\Data\Guid;
|
use VDM\Joomla\Data\Guid;
|
||||||
@ -120,7 +121,7 @@ final class Manager
|
|||||||
|
|
||||||
// store file in the file table
|
// store file in the file table
|
||||||
$this->item->set(
|
$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
|
protected function modelFileDetails(array $details, string $guid, string $entity, string $target): object
|
||||||
{
|
{
|
||||||
return (object) [
|
return (object) [
|
||||||
'name' => 'soon',
|
'name' => $details['name'],
|
||||||
'file_type' => $guid,
|
'file_type' => $guid,
|
||||||
'ext' => 'me',
|
'ext' => 'me',
|
||||||
'size_kb' => 45,
|
'size_kb' => 45,
|
||||||
'filepath' => $details['full_path'],
|
'filepath' => $details['full_path'],
|
||||||
'entity_type' => $target,
|
'entity_type' => $target,
|
||||||
'entity' => $guid,
|
'entity' => $entity,
|
||||||
'guid' => $this->getGuid('file'),
|
'guid' => $this->getGuid('file'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@
|
|||||||
|
|
||||||
// store file in the file table
|
// store file in the file table
|
||||||
$this->item->set(
|
$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
|
protected function modelFileDetails(array $details, string $guid, string $entity, string $target): object
|
||||||
{
|
{
|
||||||
return (object) [
|
return (object) [
|
||||||
'name' => 'soon',
|
'name' => $details['name'],
|
||||||
'file_type' => $guid,
|
'file_type' => $guid,
|
||||||
'ext' => 'me',
|
'ext' => 'me',
|
||||||
'size_kb' => 45,
|
'size_kb' => 45,
|
||||||
'filepath' => $details['full_path'],
|
'filepath' => $details['full_path'],
|
||||||
'entity_type' => $target,
|
'entity_type' => $target,
|
||||||
'entity' => $guid,
|
'entity' => $entity,
|
||||||
'guid' => $this->getGuid('file'),
|
'guid' => $this->getGuid('file'),
|
||||||
];
|
];
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"add_head": "0",
|
"add_head": "1",
|
||||||
"add_licensing_template": "2",
|
"add_licensing_template": "2",
|
||||||
"extends": "",
|
"extends": "",
|
||||||
"guid": "67a5e0ca-0ff0-4979-9b41-da0b09988016",
|
"guid": "67a5e0ca-0ff0-4979-9b41-da0b09988016",
|
||||||
@ -35,6 +35,6 @@
|
|||||||
"namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Manager",
|
"namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Manager",
|
||||||
"description": "File Manager Class\r\n\r\n@since 5.0.2",
|
"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",
|
"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": ""
|
"composer": ""
|
||||||
}
|
}
|
@ -53,7 +53,7 @@ final class Display
|
|||||||
*/
|
*/
|
||||||
public function get(string $entity, string $target): ?array
|
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)
|
foreach ($files as $n => $file)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
*/
|
*/
|
||||||
public function get(string $entity, string $target): ?array
|
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)
|
foreach ($files as $n => $file)
|
||||||
{
|
{
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
abstract UploadHelper #Orange {
|
abstract UploadHelper #Orange {
|
||||||
+ {static} get(string $field, string $type, ...) : ?array
|
+ {static} get(string $field, string $type, ...) : ?array
|
||||||
+ {static} getError(bool $toString = false) : array|string
|
+ {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} remove(string $fullPath) : boolean
|
||||||
# {static} setError(string $message) : void
|
# {static} setError(string $message) : void
|
||||||
}
|
}
|
||||||
@ -42,7 +42,7 @@ end note
|
|||||||
note right of UploadHelper::check
|
note right of UploadHelper::check
|
||||||
Check a file and verifies it as a allowed file format file
|
Check a file and verifies it as a allowed file format file
|
||||||
|
|
||||||
return: array|null
|
return: ?array
|
||||||
end note
|
end note
|
||||||
|
|
||||||
note right of UploadHelper::remove
|
note right of UploadHelper::remove
|
||||||
|
@ -14,9 +14,9 @@ namespace VDM\Joomla\Utilities;
|
|||||||
|
|
||||||
use Joomla\CMS\Factory;
|
use Joomla\CMS\Factory;
|
||||||
use Joomla\CMS\Language\Text;
|
use Joomla\CMS\Language\Text;
|
||||||
use Joomla\CMS\Filesystem\File;
|
use Joomla\Filesystem\File;
|
||||||
use Joomla\CMS\Filesystem\Folder;
|
use Joomla\Filesystem\Folder;
|
||||||
use Joomla\CMS\Filesystem\Path;
|
use Joomla\Filesystem\Path;
|
||||||
use VDM\Joomla\Utilities\Component\Helper;
|
use VDM\Joomla\Utilities\Component\Helper;
|
||||||
|
|
||||||
|
|
||||||
@ -84,9 +84,9 @@ abstract class UploadHelper
|
|||||||
/**
|
/**
|
||||||
* Get file/files from a HTTP upload.
|
* Get file/files from a HTTP upload.
|
||||||
*
|
*
|
||||||
* @param string $field The input field name
|
* @param string $field The input field name
|
||||||
* @param string $type The file type
|
* @param string $type The file type
|
||||||
* @param string|null $filter The filter to use when uploading the file
|
* @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
|
* @param string|null $path The path to the directory where the file must be placed
|
||||||
*
|
*
|
||||||
* @return array|null File details or false on failure.
|
* @return array|null File details or false on failure.
|
||||||
@ -187,7 +187,7 @@ abstract class UploadHelper
|
|||||||
// check that his name has file format
|
// check that his name has file format
|
||||||
if (is_string($name) && strpos($name, '.') === false)
|
if (is_string($name) && strpos($name, '.') === false)
|
||||||
{
|
{
|
||||||
$name = $name . '.' . MimeHelper::extension(null, $userfile['type']);
|
$name = $name . '.' . MimeHelper::extension($userfile['name']);
|
||||||
}
|
}
|
||||||
$userfile['file_name'] = $name;
|
$userfile['file_name'] = $name;
|
||||||
}
|
}
|
||||||
@ -231,10 +231,10 @@ abstract class UploadHelper
|
|||||||
* @return array|null of elements
|
* @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
|
// Default formats
|
||||||
$formats = MimeHelper::extensions($type);
|
$formats = MimeHelper::getFileExtensions($type);
|
||||||
|
|
||||||
// Clean the path
|
// Clean the path
|
||||||
$upload_path = Path::clean($upload['full_path']);
|
$upload_path = Path::clean($upload['full_path']);
|
||||||
@ -250,7 +250,7 @@ abstract class UploadHelper
|
|||||||
{
|
{
|
||||||
// get allowed formats
|
// get allowed formats
|
||||||
$legal_formats = (array) Helper::getParams()->get($type . '_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
|
// check the extension
|
||||||
|
@ -55,9 +55,9 @@
|
|||||||
/**
|
/**
|
||||||
* Get file/files from a HTTP upload.
|
* Get file/files from a HTTP upload.
|
||||||
*
|
*
|
||||||
* @param string $field The input field name
|
* @param string $field The input field name
|
||||||
* @param string $type The file type
|
* @param string $type The file type
|
||||||
* @param string|null $filter The filter to use when uploading the file
|
* @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
|
* @param string|null $path The path to the directory where the file must be placed
|
||||||
*
|
*
|
||||||
* @return array|null File details or false on failure.
|
* @return array|null File details or false on failure.
|
||||||
@ -158,7 +158,7 @@
|
|||||||
// check that his name has file format
|
// check that his name has file format
|
||||||
if (is_string($name) && strpos($name, '.') === false)
|
if (is_string($name) && strpos($name, '.') === false)
|
||||||
{
|
{
|
||||||
$name = $name . '.' . MimeHelper::extension(null, $userfile['type']);
|
$name = $name . '.' . MimeHelper::extension($userfile['name']);
|
||||||
}
|
}
|
||||||
$userfile['file_name'] = $name;
|
$userfile['file_name'] = $name;
|
||||||
}
|
}
|
||||||
@ -202,10 +202,10 @@
|
|||||||
* @return array|null of elements
|
* @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
|
// Default formats
|
||||||
$formats = MimeHelper::extensions($type);
|
$formats = MimeHelper::getFileExtensions($type);
|
||||||
|
|
||||||
// Clean the path
|
// Clean the path
|
||||||
$upload_path = Path::clean($upload['full_path']);
|
$upload_path = Path::clean($upload['full_path']);
|
||||||
@ -221,7 +221,7 @@
|
|||||||
{
|
{
|
||||||
// get allowed formats
|
// get allowed formats
|
||||||
$legal_formats = (array) Helper::getParams()->get($type . '_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
|
// check the extension
|
||||||
|
@ -26,6 +26,6 @@
|
|||||||
"namespace": "[[[NamespacePrefix]]]\\Joomla\\Utilities.UploadHelper",
|
"namespace": "[[[NamespacePrefix]]]\\Joomla\\Utilities.UploadHelper",
|
||||||
"description": "Upload Helper\r\n\r\n@since 3.0.11",
|
"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",
|
"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": ""
|
"composer": ""
|
||||||
}
|
}
|
@ -12,13 +12,30 @@
|
|||||||
```uml
|
```uml
|
||||||
@startuml
|
@startuml
|
||||||
abstract MimeHelper #Orange {
|
abstract MimeHelper #Orange {
|
||||||
|
+ {static} extension(string $file) : string
|
||||||
+ {static} mimeType(string $file) : string
|
+ {static} mimeType(string $file) : string
|
||||||
|
# {static} mimeTypeFilename(string $file) : string
|
||||||
+ {static} getFileExtensions(string $target = null, boolean $sorted = false) : array
|
+ {static} getFileExtensions(string $target = null, boolean $sorted = false) : array
|
||||||
}
|
}
|
||||||
|
|
||||||
note right of MimeHelper::mimeType
|
note right of MimeHelper::extension
|
||||||
Get the mime type based on file 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
|
return: string
|
||||||
end note
|
end note
|
||||||
|
|
||||||
|
@ -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**
|
* **DISCLAIMER**
|
||||||
@ -1030,26 +1050,61 @@ abstract class MimeHelper
|
|||||||
* array. It does not guarantee that the file is TRULY that
|
* array. It does not guarantee that the file is TRULY that
|
||||||
* of the extension that this function returns.
|
* of the extension that this function returns.
|
||||||
* https://gist.github.com/Llewellynvdm/74be373357e131b8775a7582c3de508b
|
* https://gist.github.com/Llewellynvdm/74be373357e131b8775a7582c3de508b
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// get the extension form file
|
// Ensure the file exists to avoid unnecessary processing
|
||||||
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
if (!is_file($file) || !is_readable($file))
|
||||||
// check if we have the extension listed
|
{
|
||||||
if (isset(self::$fileExtensionToMimeType[$extension]))
|
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];
|
return self::$fileExtensionToMimeType[$extension];
|
||||||
}
|
}
|
||||||
elseif (function_exists('mime_content_type'))
|
|
||||||
{
|
// Fallback to application/octet-stream if extension is unknown
|
||||||
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;
|
|
||||||
}
|
|
||||||
return 'application/octet-stream';
|
return 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user