diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 5c2abdf154..161e317498 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -9,6 +9,7 @@ 'addon_list_loading_error' => 'Something went wrong while loading addons. Try again later.', 'addon_uninstall_command' => 'To uninstall this addon, run the following command', 'asset_container_allow_uploads_instructions' => 'When enabled will give users the ability upload files into this container.', + 'asset_container_max_size_instructions' => 'The max allowable file size (in KB) for assets in this container.', 'asset_container_blueprint_instructions' => 'Blueprints define additional custom fields available when editing assets.', 'asset_container_create_folder_instructions' => 'When enabled will give users the ability to create folders in this container.', 'asset_container_disk_instructions' => 'Filesystem disks specify where files are stored — either locally or in a remote location like Amazon S3. They can be configured in `config/filesystems.php`', diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 020d6f7833..ca63a4947a 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -547,6 +547,19 @@ public function allowUploads($allowUploads = null) ->args(func_get_args()); } + /** + * The max file size in KB you can upload into this container. + * + * @param int|null $allowUploads + * @return int|null|$this + */ + public function maxSize($maxSize = null) + { + return $this + ->fluentlyGetOrSet('maxSize') + ->args(func_get_args()); + } + /** * The ability to create folders within this container. * @@ -619,6 +632,7 @@ public function fileData() 'disk' => $this->disk, 'search_index' => $this->searchIndex, 'allow_uploads' => $this->allowUploads, + 'max_size' => $this->maxSize, 'allow_downloading' => $this->allowDownloading, 'allow_renaming' => $this->allowRenaming, 'allow_moving' => $this->allowMoving, diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index 57f0a50fa5..67be5ff3f4 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -29,6 +29,7 @@ public function index(Request $request) 'allow_moving' => $container->allowMoving(), 'allow_renaming' => $container->allowRenaming(), 'allow_uploads' => $container->allowUploads(), + 'max_size' => $container->maxSize(), 'create_folders' => $container->createFolders(), 'edit_url' => $container->editUrl(), 'delete_url' => $container->deleteUrl(), @@ -58,6 +59,7 @@ public function edit($container) 'handle' => $container->handle(), 'disk' => $container->diskHandle(), 'allow_uploads' => $container->allowUploads(), + 'max_size' => $container->maxSize(), 'allow_downloading' => $container->allowDownloading(), 'allow_renaming' => $container->allowRenaming(), 'allow_moving' => $container->allowMoving(), @@ -97,6 +99,7 @@ public function update(Request $request, $container) ->allowRenaming($values['allow_renaming']) ->allowMoving($values['allow_moving']) ->allowUploads($values['allow_uploads']) + ->maxSize($values['max_size']) ->createFolders($values['create_folders']) ->sourcePreset($values['source_preset']) ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']); @@ -145,6 +148,7 @@ public function store(Request $request) ->title($values['title']) ->disk($values['disk']) ->allowUploads($values['allow_uploads']) + ->maxSize($values['max_size']) ->createFolders($values['create_folders']) ->sourcePreset($values['source_preset']) ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']); @@ -241,6 +245,11 @@ protected function formBlueprint($container = null) 'instructions' => __('statamic::messages.asset_container_allow_uploads_instructions'), 'default' => true, ], + 'max_size' => [ + 'type' => 'integer', + 'display' => __('Max Size'), + 'instructions' => __('statamic::messages.asset_container_max_size_instructions'), + ], 'create_folders' => [ 'type' => 'toggle', 'display' => __('Create Folders'), diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index 1a76894d29..fca9e92da9 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -66,14 +66,18 @@ public function update(Request $request, $asset) public function store(Request $request) { - $request->validate([ - 'container' => 'required', - 'folder' => 'required', - 'file' => ['file', new AllowedFile], - ]); + $request->validate(['container' => 'required']); $container = AssetContainer::find($request->container); + $fileValidation = ['file', new AllowedFile]; + + if (($maxSize = $container->maxSize()) > 0) { + $fileValidation[] = 'max:'.$maxSize; + } + + $request->validate(['folder' => 'required', 'file' => $fileValidation]); + abort_unless($container->allowUploads(), 403); $this->authorize('store', [AssetContract::class, $container]); diff --git a/src/Stache/Stores/AssetContainersStore.php b/src/Stache/Stores/AssetContainersStore.php index 536ea7b000..16e58cbf7d 100644 --- a/src/Stache/Stores/AssetContainersStore.php +++ b/src/Stache/Stores/AssetContainersStore.php @@ -19,17 +19,18 @@ public function makeItemFromFile($path, $contents) $data = YAML::file($path)->parse($contents); return AssetContainer::make($handle) - ->disk(Arr::get($data, 'disk')) - ->title(Arr::get($data, 'title')) - ->allowDownloading(Arr::get($data, 'allow_downloading')) - ->allowMoving(Arr::get($data, 'allow_moving')) - ->allowRenaming(Arr::get($data, 'allow_renaming')) - ->allowUploads(Arr::get($data, 'allow_uploads')) - ->createFolders(Arr::get($data, 'create_folders')) - ->sourcePreset(Arr::get($data, 'source_preset')) - ->warmPresets(Arr::get($data, 'warm_presets')) - ->searchIndex(Arr::get($data, 'search_index')) - ->sortField(Arr::get($data, 'sort_by')) - ->sortDirection(Arr::get($data, 'sort_dir')); + ->disk(array_get($data, 'disk')) + ->title(array_get($data, 'title')) + ->allowDownloading(array_get($data, 'allow_downloading')) + ->allowMoving(array_get($data, 'allow_moving')) + ->allowRenaming(array_get($data, 'allow_renaming')) + ->allowUploads(array_get($data, 'allow_uploads')) + ->maxSize(array_get($data, 'max_size')) + ->createFolders(array_get($data, 'create_folders')) + ->sourcePreset(array_get($data, 'source_preset')) + ->warmPresets(array_get($data, 'warm_presets')) + ->searchIndex(array_get($data, 'search_index')) + ->sortField(array_get($data, 'sort_by')) + ->sortDirection(array_get($data, 'sort_dir')); } } diff --git a/tests/Feature/AssetContainers/ListAssetContainersTest.php b/tests/Feature/AssetContainers/ListAssetContainersTest.php index bae094e710..df43eb729a 100644 --- a/tests/Feature/AssetContainers/ListAssetContainersTest.php +++ b/tests/Feature/AssetContainers/ListAssetContainersTest.php @@ -62,6 +62,7 @@ public function containerArray() 'blueprint_url' => 'http://localhost/cp/asset-containers/three/blueprint', 'can_edit' => false, 'can_delete' => false, + 'max_size' => null, ], [ 'id' => 'two', @@ -76,6 +77,7 @@ public function containerArray() 'blueprint_url' => 'http://localhost/cp/asset-containers/two/blueprint', 'can_edit' => false, 'can_delete' => false, + 'max_size' => null, ], ]; }