From 16e7b9fe46efd66d5954f083b1529ccb0a206702 Mon Sep 17 00:00:00 2001 From: F4ilji Date: Fri, 25 Jul 2025 12:36:17 +0500 Subject: [PATCH] add time_added field to FilesBlock; update file upload logic to set timestamp and improve file display in FileBlock with additional metadata --- .../Forms/ItemForm/Blocks/FilesBlock.php | 5 +- .../Forms/ItemForm/Blocks/PersonBlock.php | 4 +- app/Filament/Pages/UploadToUrl.php | 123 ++++++++++++++++++ .../Pages/UploadSchedules.php | 2 +- resources/js/assets/icons/svg/pptx.svg | 3 + .../shared/builder/pageBuilder/Builder.vue | 4 +- .../builder/pageBuilder/blocks/FileBlock.vue | 121 +++++++++-------- .../builder/tabsBuilder/PageTabBuilder.vue | 4 +- .../filament/pages/upload-to-url.blade.php | 9 ++ routes/api.php | 2 +- 10 files changed, 211 insertions(+), 66 deletions(-) create mode 100644 app/Filament/Pages/UploadToUrl.php create mode 100644 resources/js/assets/icons/svg/pptx.svg create mode 100644 resources/views/filament/pages/upload-to-url.blade.php diff --git a/app/Filament/Components/Forms/ItemForm/Blocks/FilesBlock.php b/app/Filament/Components/Forms/ItemForm/Blocks/FilesBlock.php index dfdf4d8..8756fad 100644 --- a/app/Filament/Components/Forms/ItemForm/Blocks/FilesBlock.php +++ b/app/Filament/Components/Forms/ItemForm/Blocks/FilesBlock.php @@ -24,6 +24,7 @@ class FilesBlock implements BlockSchema ->schema([ Hidden::make('expansion')->required(), Hidden::make('size')->required(), + Hidden::make('time_added')->required(), TextInput::make('title') ->label('Название файла') ->placeholder('Введите название файла') @@ -51,8 +52,10 @@ class FilesBlock implements BlockSchema ->directory('files') ->downloadable() ->afterStateUpdated(function ($set, $state) { + $set('title', $state?->getClientOriginalName()); $set('expansion', $state?->getClientOriginalExtension()); $set('size', ByteConverter::bytesToHuman($state?->getSize())); + $set('time_added', Carbon::now()->timestamp); }) ->visibility('public') ->preserveFilenames() @@ -63,4 +66,4 @@ class FilesBlock implements BlockSchema ->grid(2), ]; } -} \ No newline at end of file +} diff --git a/app/Filament/Components/Forms/ItemForm/Blocks/PersonBlock.php b/app/Filament/Components/Forms/ItemForm/Blocks/PersonBlock.php index 2da4718..e785a11 100644 --- a/app/Filament/Components/Forms/ItemForm/Blocks/PersonBlock.php +++ b/app/Filament/Components/Forms/ItemForm/Blocks/PersonBlock.php @@ -28,13 +28,11 @@ class PersonBlock implements BlockSchema FileUpload::make('photo') ->label('Фотография') ->image() - ->helperText('Рекомендуемый формат: WebP') ->optimize('webp') ->resize(50) ->disk('public') ->directory('images') ->imageEditor() - ->required() ->downloadable() ->openable(), Repeater::make('info') @@ -60,4 +58,4 @@ class PersonBlock implements BlockSchema ->itemLabel(fn (array $state): ?string => $state['column'] ?? null), ]; } -} \ No newline at end of file +} diff --git a/app/Filament/Pages/UploadToUrl.php b/app/Filament/Pages/UploadToUrl.php new file mode 100644 index 0000000..a7757dd --- /dev/null +++ b/app/Filament/Pages/UploadToUrl.php @@ -0,0 +1,123 @@ +form->fill(); + } + + public function form(Form $form): Form + { + return $form + ->schema([ + Section::make('Загрузка файла') + ->description('Загрузите один файл. После загрузки вы сможете скопировать его путь.') + ->schema([ + FileUpload::make('data.uploadedFile') // Bind directly to data.uploadedFile + ->label('Файл для загрузки') + ->required() + ->acceptedFileTypes(['application/pdf', 'image/*', 'video/*', 'audio/*', 'text/plain', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']) + ->maxSize(20000) // 20MB + ->disk('public') + ->directory('files') + ->visibility('public') + ->helperText('Загрузите файл. Максимальный размер 20MB.') + ->afterStateUpdated(function ($state) { + $this->data['uploadedFile'] = $state; // Assign to data array + $this->save(); + }), + TextInput::make('uploadedFileUrl') + ->label('Путь к файлу') + ->columnSpan('full') + ->suffixAction( + Action::make('copy') + ->icon('heroicon-s-clipboard-document-check') + ->action(function ($livewire, $state, $record) { + $livewire->js( + 'window.navigator.clipboard.writeText("'. url('/') . $state.'"); +$tooltip("'.__('Copied to clipboard').'", { timeout: 1500 });' + ); + })), + ]), + ]) + ->statePath('data'); + } + + public function save(): void + { + if (empty($this->data['uploadedFile'])) { // Check data property + Notification::make() + ->title('Нет файла для загрузки') + ->warning() + ->send(); + return; + } + + try { + $originalFileNameWithExtension = $this->data['uploadedFile']->getClientOriginalName(); + $originalFileName = pathinfo($originalFileNameWithExtension, PATHINFO_FILENAME); + $extension = $this->data['uploadedFile']->getClientOriginalExtension(); + + $sluggedFileName = Str::slug($originalFileName); + $uniqueFileName = $sluggedFileName . '-' . md5(uniqid(rand(), true)) . '.' . $extension; + $path = $this->data['uploadedFile']->storeAs('files', $uniqueFileName, 'public'); + + $this->data['uploadedFileUrl'] = Storage::url($path); + $this->uploadedFileUrl = $this->data['uploadedFileUrl']; // Assign to data array + + Notification::make() + ->title('Файл успешно загружен') + ->body("Файл `{$originalFileNameWithExtension}` загружен. Путь: `{$this->data['uploadedFileUrl']}`") + ->success() + ->send(); + + $this->data['uploadedFile'] = null; // Clear the file upload field + $this->form->fill($this->data); // Force form re-render, passing current data + + + } catch (\Throwable $e) { + $originalFileNameWithExtension = $this->data['uploadedFile']->getClientOriginalName() ?? 'Unknown'; + Notification::make() + ->title('Ошибка при загрузке файла') + ->body("Не удалось загрузить файл `{$originalFileNameWithExtension}`: " . $e->getMessage()) + ->danger() + ->send(); + report($e); + } + } + + protected function getFormActions(): array + { + return [ + // Action::make('save') + // ->label('Загрузить файл') + // ->submit('save') + // ->color('primary') + // ->icon('heroicon-o-cloud-arrow-up'), + ]; + } +} diff --git a/app/Filament/Resources/ScheduleResource/Pages/ScheduleResource/Pages/UploadSchedules.php b/app/Filament/Resources/ScheduleResource/Pages/ScheduleResource/Pages/UploadSchedules.php index caa2adc..de96842 100644 --- a/app/Filament/Resources/ScheduleResource/Pages/ScheduleResource/Pages/UploadSchedules.php +++ b/app/Filament/Resources/ScheduleResource/Pages/ScheduleResource/Pages/UploadSchedules.php @@ -242,4 +242,4 @@ class UploadSchedules extends Page { return __('Расписание и группы'); } -} \ No newline at end of file +} diff --git a/resources/js/assets/icons/svg/pptx.svg b/resources/js/assets/icons/svg/pptx.svg new file mode 100644 index 0000000..4f01b70 --- /dev/null +++ b/resources/js/assets/icons/svg/pptx.svg @@ -0,0 +1,3 @@ + + + diff --git a/resources/js/componentss/shared/builder/pageBuilder/Builder.vue b/resources/js/componentss/shared/builder/pageBuilder/Builder.vue index 466c35a..76015df 100644 --- a/resources/js/componentss/shared/builder/pageBuilder/Builder.vue +++ b/resources/js/componentss/shared/builder/pageBuilder/Builder.vue @@ -1,7 +1,7 @@