Fixes for downloading invoices

This commit is contained in:
David Bomba 2021-06-30 06:23:23 +10:00
parent 5fb05179ea
commit 8eb5cb71a1
2 changed files with 80 additions and 24 deletions

View File

@ -27,6 +27,7 @@ use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive; use ZipStream\Option\Archive;
use ZipStream\ZipStream; use ZipStream\ZipStream;
use ZipArchive;
class ZipInvoices implements ShouldQueue class ZipInvoices implements ShouldQueue
{ {
@ -68,42 +69,84 @@ class ZipInvoices implements ShouldQueue
* @throws \ZipStream\Exception\FileNotReadableException * @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException * @throws \ZipStream\Exception\OverflowException
*/ */
// public function handle()
// {
// $tempStream = fopen('php://memory', 'w+');
// $options = new Archive();
// $options->setOutputStream($tempStream);
// // create a new zipstream object
// $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
// $invoice = $this->invoices->first();
// $invitation = $invoice->invitations->first();
// $path = $invoice->client->invoice_filepath($invitation);
// $zip = new ZipStream($file_name, $options);
// foreach ($this->invoices as $invoice) {
// //$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
// $zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path());
// }
// $zip->finish();
// Storage::disk('public')->put($path.$file_name, $tempStream);
// fclose($tempStream);
// $nmo = new NinjaMailerObject;
// $nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company);
// $nmo->to_user = $this->user;
// $nmo->settings = $this->settings;
// $nmo->company = $this->company;
// NinjaMailerJob::dispatch($nmo);
// UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1));
// }
public function handle() public function handle()
{ {
$tempStream = fopen('php://memory', 'w+'); # create new zip object
$zip = new ZipArchive();
$options = new Archive(); $invitation = $this->invoices->first()->invitations->first();
$options->setOutputStream($tempStream); $path = $this->invoices->first()->client->invoice_filepath($invitation);
// create a new zipstream object
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip'; $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
$tmp_file = @tempnam('.', '');
$zip->open($tmp_file , ZipArchive::OVERWRITE);
$invoice = $this->invoices->first(); # loop through each file
$invitation = $invoice->invitations->first();
$path = $invoice->client->invoice_filepath($invitation);
$zip = new ZipStream($file_name, $options);
foreach ($this->invoices as $invoice) { foreach ($this->invoices as $invoice) {
//$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
$zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path()); $inv = $invoice->invitations->first();
# download file
$download_file = file_get_contents($invoice->pdf_file_path($inv, 'url', true));
#add it to the zip
$zip->addFromString(basename($invoice->pdf_file_path($inv)), $download_file);
} }
$zip->finish(); # close zip
$zip->close();
Storage::disk('public')->put($path.$file_name, $tempStream);
Storage::put($path.$file_name, file_get_contents($tmp_file));
fclose($tempStream);
$nmo = new NinjaMailerObject; $nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company); $nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company);
$nmo->to_user = $this->user; $nmo->to_user = $this->user;
$nmo->settings = $this->settings; $nmo->settings = $this->settings;
$nmo->company = $this->company; $nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo); NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1)); nlog("sending email");
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
} }
} }

View File

@ -71,10 +71,10 @@ class NinjaMailerJob implements ShouldQueue
public function handle() public function handle()
{ {
/*If we are migrating data we don't want to fire any emails*/
if ($this->nmo->company->is_disabled && !$this->override)
return true;
if($this->preFlightChecksFail())
return;
/*Set the correct database*/ /*Set the correct database*/
MultiDB::setDb($this->nmo->company->db); MultiDB::setDb($this->nmo->company->db);
@ -215,6 +215,19 @@ class NinjaMailerJob implements ShouldQueue
} }
private function preFlightChecksFail()
{
/* If we are migrating data we don't want to fire any emails */
if ($this->nmo->company->is_disabled && !$this->override)
return true;
/* On the hosted platform we set default contacts a @example.com email address - we shouldn't send emails to these types of addresses */
if(Ninja::isHosted() && strpos($this->nmo->to_user->email, '@example.com') !== false)
return true;
return false;
}
private function logMailError($errors, $recipient_object) private function logMailError($errors, $recipient_object)
{ {
SystemLogger::dispatch( SystemLogger::dispatch(