From aec13f3203df702d785e085654702be9c79b06db Mon Sep 17 00:00:00 2001 From: athphane Date: Thu, 8 Feb 2024 14:28:14 +0500 Subject: [PATCH] - update minimum stability and require javaabu/settings --- composer.json | 6 +- src/Media/AllowedMimeTypes.php | 112 +++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/Media/AllowedMimeTypes.php diff --git a/composer.json b/composer.json index f4a7a56..e3ba197 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ ], "require": { "php": "^8.1", - "illuminate/support": "^9.0 || ^10.0 || ^11.0" + "illuminate/support": "^9.0 || ^10.0 || ^11.0", + "javaabu/settings": "^1.0@dev" }, "require-dev": { "orchestra/testbench": "^7.0 || ^8.0 || ^9.0", @@ -42,5 +43,6 @@ ] } }, - "minimum-stability": "stable" + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/src/Media/AllowedMimeTypes.php b/src/Media/AllowedMimeTypes.php new file mode 100644 index 0000000..dff2b6a --- /dev/null +++ b/src/Media/AllowedMimeTypes.php @@ -0,0 +1,112 @@ + [ + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/tiff', + 'image/x-citrix-png', + 'image/x-png', + ], + + 'icon' => [ + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/tiff', + 'image/x-citrix-png', + 'image/x-png', + 'image/x-icon', + 'image/vnd.microsoft.icon' + ], + + 'document' => [ + 'application/pdf', + 'image/jpeg', + 'image/png', + ], + + 'video' => [ + 'video/webm', + 'video/ogg', + 'video/mp4', + ], + + 'excel' => [ + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', + 'application/vnd.ms-excel.sheet.macroEnabled.12', + 'application/vnd.ms-excel.template.macroEnabled.12', + 'application/vnd.ms-excel.addin.macroEnabled.12', + 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', + 'text/csv', + ] + ]; + + /** + * Get the allowed mime types for the specific type + * + * @param string $type + * @return array + */ + public static function getAllowedMimeTypes(string $type): array + { + return self::$allowed_mime_types[$type] ?? []; + } + + /** + * Generate allowed mime type html string + * + * @param string $type + * @param string $separator + * @return string + */ + public static function getAllowedMimeTypesString(string $type, string $separator = ','): string + { + return implode($separator, self::getAllowedMimeTypes($type)); + } + + /** + * Check if is an allowed mime type for the given type + * + * @param string $mime_type + * @param string $type + * @return boolean + */ + public static function isAllowedMimeType(string $mime_type, string $type): bool + { + return in_array($mime_type, self::getAllowedMimeTypes($type)); + } + + /** + * Get the validation rule for the given type + * + * @param string $type + * @param bool $as_array + * @return string|array + */ + public static function getValidationRule(string $type, bool $as_array = false): array|string + { + $max_size = $type == 'image' ? get_setting('max_image_file_size') : get_setting('max_upload_file_size'); + + $rules = [ + 'nullable', + 'mimetypes:'.AllowedMimeTypes::getAllowedMimeTypesString($type), + 'max:'.$max_size, + ]; + + return $as_array ? $rules : implode('|', $rules); + } +}