diff --git a/app/Http/Controllers/Entity/AssetController.php b/app/Http/Controllers/Entity/AssetController.php index c4db9797f..d422a5eb3 100644 --- a/app/Http/Controllers/Entity/AssetController.php +++ b/app/Http/Controllers/Entity/AssetController.php @@ -99,14 +99,15 @@ protected function storeFile(StoreEntityAsset $request, Campaign $campaign, Enti $service = app()->make(EntityFileService::class); try { - $file = $service + $files = $service ->entity($entity) ->campaign($campaign) - ->upload($request, 'file'); + ->upload($request, 'files'); return redirect() ->route('entities.entity_assets.index', [$campaign, $entity]) - ->with('success', __('entities/files.create.success', ['file' => $file->name])); + ->with('success', trans_choice('entities/files.create.success_plural', count($files), ['count' => count($files), 'name' => $files['0']])); + } catch (TranslatableException $e) { return redirect() ->route('entities.entity_assets.index', [$campaign, $entity]) diff --git a/app/Http/Requests/StoreEntityAsset.php b/app/Http/Requests/StoreEntityAsset.php index 21ac0ebec..cb695d2cf 100644 --- a/app/Http/Requests/StoreEntityAsset.php +++ b/app/Http/Requests/StoreEntityAsset.php @@ -33,7 +33,7 @@ public function rules() return $this->clean([ 'name' => 'required_unless:type_id,' . EntityAsset::TYPE_FILE . '|max:45', 'visibility_id' => 'nullable|integer|exists:visibilities,id', - 'file' => [ + 'files.*' => [ 'required_if:type_id,' . EntityAsset::TYPE_FILE, 'file', 'max:' . Limit::upload(), diff --git a/app/Services/EntityFileService.php b/app/Services/EntityFileService.php index 54dfc6e04..b219f6910 100644 --- a/app/Services/EntityFileService.php +++ b/app/Services/EntityFileService.php @@ -20,63 +20,66 @@ class EntityFileService /** * @throws TranslatableException */ - public function upload(StoreEntityAsset $request, string $field = 'file'): EntityAsset + public function upload(StoreEntityAsset $request, string $field = 'files'): array { /** @var GalleryService $service */ $service = app()->make(GalleryService::class); // Prepare the file for the journey - $uploadedFile = $request->file($field); - - // Already above max capacity? - if ($this->entity->files->count() >= $this->campaign->maxEntityFiles()) { - throw (new TranslatableException('crud.files.errors.max')) - ->setOptions(['max' => $this->campaign->maxEntityFiles()]); - } - if ($service->campaign($this->campaign)->available() < $uploadedFile->getSize() / 1024) { - $key = 'gallery.download.errors.gallery_full_free'; - if ($this->campaign->boosted()) { - $key = 'gallery.download.errors.gallery_full_premium'; + $uploadedFiles = $request->file($field); + $files = []; + foreach ($uploadedFiles as $uploadedFile) { + // Already above max capacity? + if ($this->entity->files->count() >= $this->campaign->maxEntityFiles()) { + throw (new TranslatableException('crud.files.errors.max')) + ->setOptions(['max' => $this->campaign->maxEntityFiles()]); + } + if ($service->campaign($this->campaign)->available() < $uploadedFile->getSize() / 1024) { + $key = 'gallery.download.errors.gallery_full_free'; + if ($this->campaign->boosted()) { + $key = 'gallery.download.errors.gallery_full_premium'; + } + throw new TranslatableException($key); } - throw new TranslatableException($key); - } - $name = $request->get('name'); - if (empty($name)) { - $name = $uploadedFile->getClientOriginalName(); - $name = Str::beforeLast($name, '.'); - } + $name = $request->get('name'); + if (empty($name)) { + $name = $uploadedFile->getClientOriginalName(); + $name = Str::beforeLast($name, '.'); + } - $image = new Image(); - $image->campaign_id = $this->campaign->id; - $image->ext = $uploadedFile->extension(); - $image->size = (int) ceil($uploadedFile->getSize() / 1024); // kb - $image->name = mb_substr($name, 0, 45); - $image->visibility_id = $this->campaign->defaultVisibility(); - $image->save(); + $image = new Image(); + $image->campaign_id = $this->campaign->id; + $image->ext = $uploadedFile->extension(); + $image->size = (int) ceil($uploadedFile->getSize() / 1024); // kb + $image->name = mb_substr($name, 0, 45); + $image->visibility_id = $this->campaign->defaultVisibility(); + $image->save(); - $uploadedFile - ->storePubliclyAs( - $image->folder, - $image->file, - ['disk' => 's3'] - ); + $uploadedFile + ->storePubliclyAs( + $image->folder, + $image->file, + ['disk' => 's3'] + ); - $file = new EntityAsset(); - $file->type_id = EntityAsset::TYPE_FILE; - $file->entity_id = $this->entity->id; - $file->metadata = [ - 'size' => $uploadedFile->getSize(), - 'type' => $uploadedFile->getMimeType(), - ]; - $file->name = $name; - $file->visibility_id = $request->get('visibility_id', 1); - $file->is_pinned = $request->get('is_pinned', 1); - $file->image_uuid = $image->id; - $file->save(); + $file = new EntityAsset(); + $file->type_id = EntityAsset::TYPE_FILE; + $file->entity_id = $this->entity->id; + $file->metadata = [ + 'size' => $uploadedFile->getSize(), + 'type' => $uploadedFile->getMimeType(), + ]; + $file->name = $name; + $file->visibility_id = $request->get('visibility_id', 1); + $file->is_pinned = $request->get('is_pinned', 1); + $file->image_uuid = $image->id; + $file->save(); - Cache::forget('campaign_' . $this->campaign->id . '_gallery'); + Cache::forget('campaign_' . $this->campaign->id . '_gallery'); + $files[] = $file->name; + } - return $file; + return $files; } } diff --git a/lang/en/entities/files.php b/lang/en/entities/files.php index f060945bf..a18531491 100644 --- a/lang/en/entities/files.php +++ b/lang/en/entities/files.php @@ -6,8 +6,8 @@ 'premium' => 'Uploading more files requires a premium campaign.', ], 'create' => [ - 'success' => 'File :file added.', - 'title' => 'New file for :entity', + 'success_plural' => '{1} File :name added.|[2,*] :count files added.', + 'title' => 'New file for :entity', ], 'destroy' => [ 'success' => 'File :name removed.', diff --git a/resources/api-docs/1.0/entity-assets.md b/resources/api-docs/1.0/entity-assets.md index 132649e72..98be0a328 100644 --- a/resources/api-docs/1.0/entity-assets.md +++ b/resources/api-docs/1.0/entity-assets.md @@ -110,10 +110,10 @@ To create an asset, use the following endpoint. | :- | :- | :- | | `name` | `string` | The name of the asset (max 45) | | `type_id` | `int` (Required) | The type of asset being created. -| `file` | `file` | The file to be uploaded as an asset, exlcusive and required for file assets (`type_id: 1`). | +| `files` | `file` | The file or files to be uploaded as an asset/assets, exclusive and required for file assets (`type_id: 1`). | | `visibility_id` | `int` | The visibility id: 1 `all`, 2 `self`, 3 `admin`, 4 `self-admin` or 5 `members`. | | `metadata` | `array` | `metadata.icon` and `metadata.url` are required for `links`. | -| `is_pinned` | `bool` | Controls wether or not an asset is shown and linked to on the pins tab in the overview of the entity, exlcusive to file assets (`type_id: 1`). | +| `is_pinned` | `bool` | Controls wether or not an asset is shown and linked to on the pins tab in the overview of the entity, exclusive to file assets (`type_id: 1`). | ### Results diff --git a/resources/views/entities/pages/files/_form.blade.php b/resources/views/entities/pages/files/_form.blade.php index e930f3159..32cda4b9c 100644 --- a/resources/views/entities/pages/files/_form.blade.php +++ b/resources/views/entities/pages/files/_form.blade.php @@ -4,11 +4,11 @@ @if(!isset($entityAsset)) - +

{{ __('crud.files.hints.limitations', ['formats' => 'jpg, jpeg, png, gif, webp, pdf, xls(x), csv, mp3, ogg, json', 'size' => Limit::readable()->upload()]) }}