-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from appoly/releases/5.1.0
v5.1.0 - Storing of email attachments, and handle pruning of old mails via command
- Loading branch information
Showing
9 changed files
with
289 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div> | ||
<x-mailweb::button type="button" wire:click="download" wire:loading.attr="disabled" wire:loading.class="opacity-50"> | ||
<span wire:loading.remove>{{ $attachment->name }}</span> | ||
<span wire:loading>Downloading...</span> | ||
</x-mailweb::button> | ||
@if ($errorMessage) | ||
<div class="text-sm text-red-600"> | ||
{{ $errorMessage }} | ||
</div> | ||
@endif | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Appoly\MailWeb\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Storage; | ||
use Appoly\MailWeb\Http\Models\MailwebEmail; | ||
|
||
class PruneMailwebMails extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'mailweb:prune {remaining?}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Prune old mailweb emails up to the limit passed, or the default set in config'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
// If remaining is not provided, use the config default | ||
$remaining = $this->argument('remaining'); | ||
if ($remaining === null) { | ||
$remaining = config('MailWeb.MAILWEB_LIMIT', 30); | ||
} | ||
|
||
$remaining = (int) $remaining; | ||
|
||
$emailIdsToDeleteKeyedById = MailwebEmail::latest() | ||
->withCount(['attachments as hasFileAttachments' => fn($q) => $q->whereNotNull('path')]) | ||
->limit(5_000) // We need a limit to use offset, and to avoid memory issues. 5k is... probably enough, as this can just run more often if not | ||
->offset($remaining) // We keep only the amount specified, so offset by that number | ||
->pluck('hasFileAttachments', 'id') // Key of email ID, val of count of attachments | ||
->toArray(); | ||
|
||
$emailIdsWithAttachments = array_keys(array_filter($emailIdsToDeleteKeyedById, fn($count) => $count > 0)); | ||
|
||
Log::info('Pruning ' . count($emailIdsToDeleteKeyedById) . ' emails, of which ' . count($emailIdsWithAttachments) . ' have attachments'); | ||
|
||
// Attachment cleanup needs to be done slowly | ||
if (count($emailIdsToDeleteKeyedById) > 0) { | ||
dispatch(function () use ($emailIdsWithAttachments) { | ||
// Chunk into batches of 100 for deletion | ||
$storageDisk = config('MailWeb.MAILWEB_ATTACHMENTS.DISK'); | ||
if ($storageDisk) { | ||
$basePath = config('MailWeb.MAILWEB_ATTACHMENTS.PATH'); | ||
|
||
foreach ($emailIdsWithAttachments as $emailId) { | ||
Storage::disk($storageDisk)->deleteDirectory($basePath . '/' . $emailId); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
MailwebEmail::whereIn('id', array_keys($emailIdsToDeleteKeyedById))->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.