Skip to content

Commit

Permalink
Only show file kinds that are actually allowed to be uploaded
Browse files Browse the repository at this point in the history
Resolves #3917
  • Loading branch information
brandonkelly committed Feb 27, 2019
1 parent e94d1c9 commit 97da9fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Unreleased

### Added
- Added `craft\helers\Assets::getAllowedFileKinds()`.

### Changed
- Assets field settings no longer list file kinds that aren’t allowed to be uploaded, per the `allowedFileExtensions` and `extraAllowedFileExtensions` config settings. ([#3917](https://github.com/craftcms/cms/issues/3917))
- The `{% exit %}` tag now throws a more specific exception depending on the status code passed to it (e.g. `yii\web\NotFoundHttpException` for 404s). ([#3915](https://github.com/craftcms/cms/issues/3915))

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function getFileKindOptions(): array
{
$fileKindOptions = [];

foreach (AssetsHelper::getFileKinds() as $value => $kind) {
foreach (AssetsHelper::getAllowedFileKinds() as $value => $kind) {
$fileKindOptions[] = ['value' => $value, 'label' => $kind['label']];
}

Expand Down
36 changes: 36 additions & 0 deletions src/helpers/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ class Assets
// Properties
// =========================================================================

/**
* @var array Supported file kinds
* @see getFileKinds()
*/
private static $_fileKinds;

/**
* @var array Allowed file kinds
* @see getAllowedFileKinds()
*/
private static $_allowedFileKinds;

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -278,6 +288,32 @@ public static function getFileKinds(): array
return self::$_fileKinds;
}

/**
* Returns a list of file kinds that are allowed to be uploaded.
*
* @return array The allowed file kinds
*/
public static function getAllowedFileKinds(): array
{
if (self::$_allowedFileKinds !== null) {
return self::$_allowedFileKinds;
}

self::$_allowedFileKinds = [];
$allowedExtensions = array_flip(Craft::$app->getConfig()->getGeneral()->allowedFileExtensions);

foreach (static::getFileKinds() as $kind => $info) {
foreach ($info['extensions'] as $extension) {
if (isset($allowedExtensions[$extension])) {
self::$_allowedFileKinds[$kind] = $info;
continue 2;
}
}
}

return self::$_allowedFileKinds;
}

/**
* Returns the label of a given file kind.
*
Expand Down

0 comments on commit 97da9fa

Please sign in to comment.