From 4f5d18cacff021c94b9dd2def5677012296d5d97 Mon Sep 17 00:00:00 2001 From: edalzell Date: Fri, 26 Apr 2024 15:47:46 -0700 Subject: [PATCH 1/7] get it going --- phpunit.xml | 28 +++++++++++++++++++ resources/lang/en/messages.php | 1 + src/Assets/AssetContainer.php | 14 ++++++++++ .../CP/Assets/AssetContainersController.php | 9 ++++++ .../CP/Assets/AssetsController.php | 14 ++++++---- src/Stache/Stores/AssetContainersStore.php | 1 + 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 phpunit.xml diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000000..e0cf4ba75a --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,28 @@ + + + + + ./tests + + + + + + + + + + + + + diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 94435bf19b..361e4cfe01 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 d0185f3c39..e39ade6917 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -28,6 +28,7 @@ public function index(Request $request) 'allow_moving' => $container->allowMoving(), 'allow_renaming' => $container->allowRenaming(), 'allow_uploads' => $container->allowUploads(), + 'maxSize' => $container->maxSize(), 'create_folders' => $container->createFolders(), 'edit_url' => $container->editUrl(), 'delete_url' => $container->deleteUrl(), @@ -57,6 +58,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(), @@ -96,6 +98,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']); @@ -144,6 +147,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']); @@ -240,6 +244,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 e371abac6c..3771815c4a 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 81627841e9..ad39dff3ba 100644 --- a/src/Stache/Stores/AssetContainersStore.php +++ b/src/Stache/Stores/AssetContainersStore.php @@ -24,6 +24,7 @@ public function makeItemFromFile($path, $contents) ->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')) From c793287641b01636ffe42dac7d88b5afa7b2554d Mon Sep 17 00:00:00 2001 From: edalzell Date: Fri, 26 Apr 2024 16:08:51 -0700 Subject: [PATCH 2/7] fix tests --- src/Assets/AugmentedAssetContainer.php | 1 + src/Http/Controllers/CP/Assets/AssetContainersController.php | 2 +- tests/Feature/AssetContainers/ListAssetContainersTest.php | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Assets/AugmentedAssetContainer.php b/src/Assets/AugmentedAssetContainer.php index 9f464783d2..35273f1a4f 100644 --- a/src/Assets/AugmentedAssetContainer.php +++ b/src/Assets/AugmentedAssetContainer.php @@ -17,6 +17,7 @@ public function keys() 'search_index', 'api_url', 'assets', + 'max_size', ]; } diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index e39ade6917..719e8284d7 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -28,7 +28,7 @@ public function index(Request $request) 'allow_moving' => $container->allowMoving(), 'allow_renaming' => $container->allowRenaming(), 'allow_uploads' => $container->allowUploads(), - 'maxSize' => $container->maxSize(), + 'max_size' => $container->maxSize(), 'create_folders' => $container->createFolders(), 'edit_url' => $container->editUrl(), 'delete_url' => $container->deleteUrl(), diff --git a/tests/Feature/AssetContainers/ListAssetContainersTest.php b/tests/Feature/AssetContainers/ListAssetContainersTest.php index bae094e710..1502296318 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 ], ]; } From 054165d13b63f0cbe0f87fb4c14c334a5502582e Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Fri, 26 Apr 2024 17:04:56 -0700 Subject: [PATCH 3/7] Discard changes to phpunit.xml --- phpunit.xml | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 phpunit.xml diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100644 index e0cf4ba75a..0000000000 --- a/phpunit.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - ./tests - - - - - - - - - - - - - From 81a8237b56704cf1f913e51e86abaa79c6349afd Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Wed, 1 May 2024 13:08:45 -0700 Subject: [PATCH 4/7] comma --- tests/Feature/AssetContainers/ListAssetContainersTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/AssetContainers/ListAssetContainersTest.php b/tests/Feature/AssetContainers/ListAssetContainersTest.php index 1502296318..df43eb729a 100644 --- a/tests/Feature/AssetContainers/ListAssetContainersTest.php +++ b/tests/Feature/AssetContainers/ListAssetContainersTest.php @@ -62,7 +62,7 @@ public function containerArray() 'blueprint_url' => 'http://localhost/cp/asset-containers/three/blueprint', 'can_edit' => false, 'can_delete' => false, - 'max_size' => null + 'max_size' => null, ], [ 'id' => 'two', @@ -77,7 +77,7 @@ public function containerArray() 'blueprint_url' => 'http://localhost/cp/asset-containers/two/blueprint', 'can_edit' => false, 'can_delete' => false, - 'max_size' => null + 'max_size' => null, ], ]; } From ccba94a40f1cd00f636bd0792f0e853e769b3756 Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Thu, 2 May 2024 13:24:33 -0700 Subject: [PATCH 5/7] Order of operations --- src/Http/Controllers/CP/Assets/AssetsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index 3771815c4a..0f1bae4698 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -72,7 +72,7 @@ public function store(Request $request) $fileValidation = ['file', new AllowedFile]; - if ($maxSize = $container->maxSize() > 0) { + if (($maxSize = $container->maxSize()) > 0) { $fileValidation[] = 'max:'.$maxSize; } From 8375e4bc253dd788d130e48a81b340da4ecb3c56 Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Thu, 2 May 2024 13:33:16 -0700 Subject: [PATCH 6/7] Shouldn't need this --- src/Assets/AugmentedAssetContainer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Assets/AugmentedAssetContainer.php b/src/Assets/AugmentedAssetContainer.php index 35273f1a4f..9f464783d2 100644 --- a/src/Assets/AugmentedAssetContainer.php +++ b/src/Assets/AugmentedAssetContainer.php @@ -17,7 +17,6 @@ public function keys() 'search_index', 'api_url', 'assets', - 'max_size', ]; } From 82cae51af1bb53c26cae94d13fc39bafaadf23f5 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 8 May 2024 13:13:56 -0700 Subject: [PATCH 7/7] =?UTF-8?q?shouldn=E2=80=99t=20need=20this?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Assets/AugmentedAssetContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assets/AugmentedAssetContainer.php b/src/Assets/AugmentedAssetContainer.php index 35273f1a4f..758536b6b3 100644 --- a/src/Assets/AugmentedAssetContainer.php +++ b/src/Assets/AugmentedAssetContainer.php @@ -17,7 +17,7 @@ public function keys() 'search_index', 'api_url', 'assets', - 'max_size', + // 'max_size', ]; }