change file limitation logic
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

This commit is contained in:
2026-05-01 11:46:47 +03:30
parent 8475ccf873
commit edc7f1955e
7 changed files with 96 additions and 6 deletions

View File

@@ -9,6 +9,7 @@ use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Number;
use Livewire\Attributes\Locked;
use Livewire\Component;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
@@ -41,16 +42,19 @@ class MessageComposer extends Component
{
$this->body = trim($this->body);
$hasAttachments = $this->hasAttachments();
$maxAttachmentFiles = $this->maxAttachmentFiles();
$maxAttachmentFileSizeKilobytes = $this->maxAttachmentFileSizeKilobytes();
$maxAttachmentFileSizeLabel = $this->maxAttachmentFileSizeLabel();
$validated = $this->validate([
'body' => [$hasAttachments ? 'nullable' : 'required', 'string', 'max:4000'],
'attachments' => ['array', 'max:5'],
'attachments.*' => ['file', 'max:10240'],
'attachments' => ['array', 'max:'.$maxAttachmentFiles],
'attachments.*' => ['file', 'max:'.$maxAttachmentFileSizeKilobytes],
], [
'body.required' => __('Write a message or attach a file before sending.'),
'attachments.max' => __('Attach up to :max files at a time.'),
'attachments.max' => __('Attach up to :max files at a time.', ['max' => $maxAttachmentFiles]),
'attachments.*.file' => __('Each attachment must be a valid file.'),
'attachments.*.max' => __('Each attachment must be 10 MB or smaller.'),
'attachments.*.max' => __('Each attachment must be :size or smaller.', ['size' => $maxAttachmentFileSizeLabel]),
]);
$conversation = $this->conversation();
@@ -172,6 +176,14 @@ class MessageComposer extends Component
];
}
public function attachmentLimitSummary(): string
{
return __('Up to :count files, :size each', [
'count' => $this->maxAttachmentFiles(),
'size' => $this->maxAttachmentFileSizeLabel(),
]);
}
private function conversation(): Conversation
{
$conversation = Conversation::query()
@@ -190,6 +202,21 @@ class MessageComposer extends Component
->isNotEmpty();
}
private function maxAttachmentFiles(): int
{
return (int) config('chat.attachments.max_files', 5);
}
private function maxAttachmentFileSizeKilobytes(): int
{
return (int) config('chat.attachments.max_file_size_kilobytes', 2 * 1024 * 1024);
}
private function maxAttachmentFileSizeLabel(): string
{
return Number::fileSize($this->maxAttachmentFileSizeKilobytes() * 1024);
}
private function emojiForCode(string $code): ?string
{
$code = strtoupper($code);

View File

@@ -15,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
$this->configureFileUploads();
}
/**
@@ -47,4 +47,14 @@ class AppServiceProvider extends ServiceProvider
: null,
);
}
protected function configureFileUploads(): void
{
$maxFileSizeKilobytes = (int) config('chat.attachments.max_file_size_kilobytes');
config([
'livewire.temporary_file_upload.rules' => ['required', 'file', 'max:'.$maxFileSizeKilobytes],
'livewire.temporary_file_upload.max_upload_time' => (int) config('chat.attachments.livewire_max_upload_time'),
]);
}
}