Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Ability to set max file size on an asset container #9966

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en/messages.php
Expand Up @@ -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`',
Expand Down
14 changes: 14 additions & 0 deletions src/Assets/AssetContainer.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Assets/AugmentedAssetContainer.php
Expand Up @@ -17,6 +17,7 @@ public function keys()
'search_index',
'api_url',
'assets',
'max_size',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonvarga I don't understand why this is needed, but without it the tests don't pass. With it, unrelated tests fail.

];
}

Expand Down
9 changes: 9 additions & 0 deletions src/Http/Controllers/CP/Assets/AssetContainersController.php
Expand Up @@ -28,6 +28,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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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'),
Expand Down
14 changes: 9 additions & 5 deletions src/Http/Controllers/CP/Assets/AssetsController.php
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions src/Stache/Stores/AssetContainersStore.php
Expand Up @@ -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'))
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/AssetContainers/ListAssetContainersTest.php
Expand Up @@ -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',
Expand All @@ -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
],
];
}
Expand Down