Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
dash8x committed Feb 11, 2024
2 parents 6448ee7 + 0bf32fd commit 86c6e8f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -42,5 +43,6 @@
]
}
},
"minimum-stability": "stable"
"minimum-stability": "dev",
"prefer-stable": true
}
112 changes: 112 additions & 0 deletions src/Media/AllowedMimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Simple class to check for mimetype
*/

namespace Javaabu\Helpers\Media;

abstract class AllowedMimeTypes
{
/**
* @var array
*/
protected static array $allowed_mime_types = [
'image' => [
'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);
}
}

0 comments on commit 86c6e8f

Please sign in to comment.