From d786c1a0728928b7bc9bc02b181327036de24a7c Mon Sep 17 00:00:00 2001 From: Peter Sasburg Date: Tue, 18 Jun 2024 17:32:41 +0200 Subject: [PATCH 1/2] Fixed resend & delete with new storage config --- src/Filament/Resources/EmailResource.php | 44 ++++++++++++------------ src/Listeners/FilamentEmailLogger.php | 10 +++--- src/Mail/ResendMail.php | 5 +-- src/Models/Email.php | 35 ++++++++++--------- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/Filament/Resources/EmailResource.php b/src/Filament/Resources/EmailResource.php index 65f3f2f..8a809ba 100644 --- a/src/Filament/Resources/EmailResource.php +++ b/src/Filament/Resources/EmailResource.php @@ -92,7 +92,7 @@ public static function form(Form $form): Form ->label(__('filament-email::filament-email.created_at')), ])->columns(4), Fieldset::make('attachments') - ->hidden(fn(): bool => !config('filament-email.store_attachments')) + ->hidden(fn (): bool => ! config('filament-email.store_attachments')) ->label(__('filament-email::filament-email.attachments')) ->schema([ View::make('filament-email::attachments') @@ -137,7 +137,7 @@ public static function table(Table $table): Table ->icon('heroicon-o-eye') ->iconSize(IconSize::Medium) ->modalFooterActions( - fn($action): array => [ + fn ($action): array => [ $action->getModalCancelAction(), ]) ->fillForm(function ($record) { @@ -194,7 +194,7 @@ public static function table(Table $table): Table ->nestedRecursiveRules([ 'email', ]) - ->default(fn($record): array => !empty($record->to) ? explode(',', $record->to) : []) + ->default(fn ($record): array => ! empty($record->to) ? explode(',', $record->to) : []) ->required(), TagsInput::make('cc') ->label(__('filament-email::filament-email.cc')) @@ -202,21 +202,21 @@ public static function table(Table $table): Table ->nestedRecursiveRules([ 'email', ]) - ->default(fn($record): array => !empty($record->cc) ? explode(',', $record->cc) : []), + ->default(fn ($record): array => ! empty($record->cc) ? explode(',', $record->cc) : []), TagsInput::make('bcc') ->label(__('filament-email::filament-email.bcc')) ->placeholder(__('filament-email::filament-email.insert_multiple_email_placelholder')) ->nestedRecursiveRules([ 'email', ]) - ->default(fn($record): array => !empty($record->bcc) ? explode(',', $record->bcc) : []), + ->default(fn ($record): array => ! empty($record->bcc) ? explode(',', $record->bcc) : []), Toggle::make('attachments') ->label(__('filament-email::filament-email.add_attachments')) ->onColor('success') ->offColor('danger') ->inline(false) - ->disabled(fn($record): bool => empty($record->attachments)) - ->default(fn($record): bool => !empty($record->attachments)) + ->disabled(fn ($record): bool => empty($record->attachments)) + ->default(fn ($record): bool => ! empty($record->attachments)) ->required(), ]) ->action(function (Email $record, array $data) { @@ -243,10 +243,10 @@ public static function table(Table $table): Table ]) ->columns([ TextColumn::make('from') - ->prefix(__('filament-email::filament-email.from') . ': ') - ->suffix(fn(Email $record): string => !empty($record->attachments) ? ' (' . trans_choice('filament-email::filament-email.attachments_number', count($record->attachments)) . ')' : '') + ->prefix(__('filament-email::filament-email.from').': ') + ->suffix(fn (Email $record): string => ! empty($record->attachments) ? ' ('.trans_choice('filament-email::filament-email.attachments_number', count($record->attachments)).')' : '') ->label(__('filament-email::filament-email.header')) - ->description(fn(Email $record): string => Str::limit(__('filament-email::filament-email.to') . ': ' . $record->to, 40)) + ->description(fn (Email $record): string => Str::limit(__('filament-email::filament-email.to').': '.$record->to, 40)) ->searchable(), TextColumn::make('subject') ->label(__('filament-email::filament-email.subject')) @@ -293,16 +293,16 @@ public static function table(Table $table): Table $indicators = []; $format = config('filament-email.resource.filter_date_format'); - if (!empty($data['created_from'])) { + if (! empty($data['created_from'])) { $from = Carbon::parse($data['created_from'])->format($format); - $indicators['created'] = __('filament-email::filament-email.from_filter') . " $from"; + $indicators['created'] = __('filament-email::filament-email.from_filter')." $from"; } - if (!empty($data['created_until'])) { + if (! empty($data['created_until'])) { $to = Carbon::parse($data['created_until'])->format($format); $toText = __('filament-email::filament-email.to_filter'); - if (!empty($indicators['created'])) { - $indicators['created'] .= ' ' . strtolower($toText) . " $to"; + if (! empty($indicators['created'])) { + $indicators['created'] .= ' '.strtolower($toText)." $to"; } else { $indicators['created'] = "$toText $to"; } @@ -324,30 +324,30 @@ public static function table(Table $table): Table return $query ->when( $data['to'], - fn(Builder $query, $value): Builder => $query->where('to', 'like', "%$value%"), + fn (Builder $query, $value): Builder => $query->where('to', 'like', "%$value%"), ) ->when( $data['cc'], - fn(Builder $query, $value): Builder => $query->where('cc', 'like', "%$value%"), + fn (Builder $query, $value): Builder => $query->where('cc', 'like', "%$value%"), ) ->when( $data['bcc'], - fn(Builder $query, $value): Builder => $query->where('bcc', 'like', "%$value%"), + fn (Builder $query, $value): Builder => $query->where('bcc', 'like', "%$value%"), ) ->when( $data['created_from'], - fn(Builder $query, $value): Builder => $query->where('created_at', '>=', $value), + fn (Builder $query, $value): Builder => $query->where('created_at', '>=', $value), ) ->when( $data['created_until'], - fn(Builder $query, $value): Builder => $query->where('created_at', '<=', $value), + fn (Builder $query, $value): Builder => $query->where('created_at', '<=', $value), ); }), ]) ->paginationPageOptions(function (Table $table) { $options = config('filament-email.pagination_page_options'); - return !empty($options) && is_array($options) ? $options : $table->getPaginationPageOptions(); + return ! empty($options) && is_array($options) ? $options : $table->getPaginationPageOptions(); }); } @@ -363,7 +363,7 @@ public static function canAccess(): bool { $roles = config('filament-email.can_access.role', []); - if (method_exists(auth()->user(), 'hasRole') && !empty($roles)) { + if (method_exists(auth()->user(), 'hasRole') && ! empty($roles)) { return auth()->user()->hasRole($roles); } diff --git a/src/Listeners/FilamentEmailLogger.php b/src/Listeners/FilamentEmailLogger.php index 91aba0b..ea31295 100644 --- a/src/Listeners/FilamentEmailLogger.php +++ b/src/Listeners/FilamentEmailLogger.php @@ -33,12 +33,12 @@ public function handle(object $event): void $model = config('filament-email.resource.model') ?? Email::class; $attachments = []; - $savePath = 'filament-email-log' . DIRECTORY_SEPARATOR . date('YmdHis') . '_' . Str::random(5) . DIRECTORY_SEPARATOR; + $savePath = 'filament-email-log'.DIRECTORY_SEPARATOR.date('YmdHis').'_'.Str::random(5).DIRECTORY_SEPARATOR; if ($storeAttachments) { foreach ($event->message->getAttachments() as $attachment) { - $filePath = $savePath . Str::random(5) . '_' . $attachment->getFilename(); + $filePath = $savePath.Str::random(5).'_'.$attachment->getFilename(); Storage::disk($attachmentsDisk) ->put($filePath, $attachment->getBody()); $attachments[] = [ @@ -48,7 +48,7 @@ public function handle(object $event): void ]; } - $savePathRaw = $savePath . $rawMessage->getMessageId() . '.eml'; + $savePathRaw = $savePath.$rawMessage->getMessageId().'.eml'; Storage::disk($attachmentsDisk) ->put($savePathRaw, $rawMessage->getMessage()->toString()); @@ -67,7 +67,7 @@ public function handle(object $event): void 'text_body' => $email->getTextBody(), 'raw_body' => $savePathRaw, 'sent_debug_info' => $rawMessage->getDebug(), - 'attachments' => !empty($attachments) ? $attachments : null, + 'attachments' => ! empty($attachments) ? $attachments : null, ]); } @@ -77,7 +77,7 @@ private function recipientsToString(array $recipients): string return implode( ',', array_map(function ($email) { - return "{$email->getAddress()}" . ($email->getName() ? " <{$email->getName()}>" : ''); + return "{$email->getAddress()}".($email->getName() ? " <{$email->getName()}>" : ''); }, $recipients) ); } diff --git a/src/Mail/ResendMail.php b/src/Mail/ResendMail.php index 772a8bc..6257a3f 100644 --- a/src/Mail/ResendMail.php +++ b/src/Mail/ResendMail.php @@ -40,12 +40,13 @@ public function content(): Content public function attachments(): array { $attachments = []; + $storageDisk = config('filament-email.attachments_disk', 'local'); if ($this->addAttachments) { $modelAttachments = $this->email->attachments; - if (! empty($modelAttachments)) { + if (!empty($modelAttachments)) { foreach ($modelAttachments as $attachment) { - $attachments[] = Attachment::fromPath(storage_path('app'.DIRECTORY_SEPARATOR.$attachment['path'])) + $attachments[] = Attachment::fromStorageDisk($storageDisk, $attachment['path']) ->as($attachment['name']); } } diff --git a/src/Models/Email.php b/src/Models/Email.php index 2bb0dc2..295885c 100644 --- a/src/Models/Email.php +++ b/src/Models/Email.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Storage; /** * Email @@ -65,39 +66,41 @@ public static function boot() parent::boot(); self::deleting(function ($record) { + $storageDisk = config('filament-email.attachments_disk', 'local'); $folderPath = null; - if (! empty($record->attachments)) { + if (!empty($record->attachments)) { foreach ($record->attachments as $attachment) { - $filePath = storage_path('app'.DIRECTORY_SEPARATOR.$attachment['path']); + $filePath = Storage::disk($storageDisk)->path($attachment['path']); if (empty($folderPath)) { - $parts = explode(DIRECTORY_SEPARATOR, $filePath); + $parts = explode(DIRECTORY_SEPARATOR, $attachment['path']); array_pop($parts); $folderPath = implode(DIRECTORY_SEPARATOR, $parts); } - if (! is_dir($filePath) && file_exists($filePath)) { - unlink($filePath); + if (!Storage::directoryExists($folderPath) && Storage::disk($storageDisk)->exists($attachment['path'])) { + Storage::disk($storageDisk)->delete($attachment['path']); } } } - $savePathRaw = storage_path('app'.DIRECTORY_SEPARATOR.$record->raw_body); - if (! is_dir($savePathRaw) && file_exists($savePathRaw)) { - if (empty($folderPath)) { - $parts = explode(DIRECTORY_SEPARATOR, $savePathRaw); - array_pop($parts); - $folderPath = implode(DIRECTORY_SEPARATOR, $parts); + if (!empty($record->raw_body)) { + if (!Storage::disk($storageDisk)->directoryExists($record->raw_body) && Storage::disk($storageDisk)->exists($record->raw_body)) { + if (empty($folderPath)) { + $parts = explode(DIRECTORY_SEPARATOR, $record->raw_body); + array_pop($parts); + $folderPath = implode(DIRECTORY_SEPARATOR, $parts); + } + Storage::disk($storageDisk)->delete($record->raw_body); + } + if (Storage::disk($storageDisk)->directoryExists($folderPath)) { + Storage::disk($storageDisk)->deleteDirectory($folderPath); } - unlink($savePathRaw); - } - if (is_dir($folderPath) && file_exists($folderPath)) { - rmdir($folderPath); } }); } public function prunable() { - return static::where('created_at', '<=', now()->subDays(config('filament-email.keep_email_for_days', 60))); + return static::where('created_at', '<=', now())->subDays(config('filament-email.keep_email_for_days', 60)); } private function getTableColumns() From 9b00e140171d3f65fefcd7b5b1ce125866f3f44d Mon Sep 17 00:00:00 2001 From: Peter Sasburg Date: Tue, 18 Jun 2024 17:43:58 +0200 Subject: [PATCH 2/2] Bugfix prunable --- src/Models/Email.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Email.php b/src/Models/Email.php index 295885c..d3cbf3d 100644 --- a/src/Models/Email.php +++ b/src/Models/Email.php @@ -100,7 +100,7 @@ public static function boot() public function prunable() { - return static::where('created_at', '<=', now())->subDays(config('filament-email.keep_email_for_days', 60)); + return static::where('created_at', '<=', now()->subDays(config('filament-email.keep_email_for_days', 60))); } private function getTableColumns()