add time_added field to FilesBlock; update file upload logic to set timestamp and improve file display in FileBlock with additional metadata
This commit is contained in:
@@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
|
||||
class UploadToUrl extends Page
|
||||
{
|
||||
|
||||
protected static ?string $title = 'Быстрая загрузка файла';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
||||
|
||||
protected static string $view = 'filament.pages.upload-to-url';
|
||||
|
||||
public ?array $data = [];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -242,4 +242,4 @@ class UploadSchedules extends Page
|
||||
{
|
||||
return __('Расписание и группы');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user