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 all 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
9 changes: 9 additions & 0 deletions src/Http/Controllers/CP/Assets/AssetContainersController.php
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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'),
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
25 changes: 13 additions & 12 deletions src/Stache/Stores/AssetContainersStore.php
Expand Up @@ -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'));
}
}
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