Add sockets for async downloads
This commit is contained in:
parent
b42c35d4c3
commit
763aa4ae29
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Events\Socket;
|
||||
|
||||
use App\Models\User;
|
||||
use League\Fractal\Manager;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use App\Utils\Traits\Invoice\Broadcasting\DefaultResourceBroadcast;
|
||||
|
||||
/**
|
||||
* Class DownloadAvailable.
|
||||
*/
|
||||
class DownloadAvailable implements ShouldBroadcast
|
||||
{
|
||||
use SerializesModels;
|
||||
use InteractsWithSockets;
|
||||
|
||||
public function __construct(public string $url, public string $message, public User $user)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return [
|
||||
new PrivateChannel("user-{$this->user->account->key}-{$this->user->id}"),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
|
||||
ctrans('texts.document_download_subject');
|
||||
|
||||
return [
|
||||
'message' => $this->message,
|
||||
'url' => $this->url,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -11,20 +11,22 @@
|
|||
|
||||
namespace App\Jobs\Invoice;
|
||||
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\DownloadInvoices;
|
||||
use App\Models\User;
|
||||
use App\Models\Company;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\User;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Jobs\Util\UnlinkFile;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use App\Mail\DownloadInvoices;
|
||||
use App\Jobs\Mail\NinjaMailerJob;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use App\Jobs\Mail\NinjaMailerObject;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Events\Socket\DownloadAvailable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
|
||||
class ZipInvoices implements ShouldQueue
|
||||
{
|
||||
|
|
@ -55,11 +57,10 @@ class ZipInvoices implements ShouldQueue
|
|||
public function handle(): void
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
|
||||
App::setLocale($this->company->locale());
|
||||
|
||||
$settings = $this->company->settings;
|
||||
|
||||
nlog(count($this->invoices));
|
||||
|
||||
$this->invoices = Invoice::withTrashed()
|
||||
->where('company_id', $this->company->id)
|
||||
->whereIn('id', $this->invoices)
|
||||
|
|
@ -99,9 +100,10 @@ class ZipInvoices implements ShouldQueue
|
|||
}
|
||||
|
||||
Storage::put($path.$file_name, $zipFile->outputAsString());
|
||||
$storage_url = Storage::url($path.$file_name);
|
||||
|
||||
$nmo = new NinjaMailerObject();
|
||||
$nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
|
||||
$nmo->mailable = new DownloadInvoices($storage_url, $this->company);
|
||||
$nmo->to_user = $this->user;
|
||||
$nmo->settings = $settings;
|
||||
$nmo->company = $this->company;
|
||||
|
|
@ -110,6 +112,10 @@ class ZipInvoices implements ShouldQueue
|
|||
|
||||
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
|
||||
|
||||
$message = count($this->invoices). " ". ctrans('texts.invoices'). " " . ctrans('texts.download_files');
|
||||
|
||||
broadcast(new DownloadAvailable($storage_url, $message, $this->company->locale(), $this->user));
|
||||
|
||||
} catch (\PhpZip\Exception\ZipException $e) {
|
||||
nlog('could not make zip => '.$e->getMessage());
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -14,3 +14,8 @@
|
|||
Broadcast::channel('company-{company_key}', function (\App\Models\User $user, string $company_key) {
|
||||
return $user->company()->company_key === $company_key;
|
||||
});
|
||||
|
||||
Broadcast::channel('user-{account_key}-{user_id}', function (\App\Models\User $user, string $account_key, string $user_id) {
|
||||
return $user->account->key === $account_key && $user->id === (int)$user_id;
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue