From 02881945f803a5760cb643411a602a601f0c44a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Thu, 24 Aug 2023 15:44:10 +0300 Subject: [PATCH] more storage fixes --- app/Console/Commands/StorageTempClear.php | 3 +- app/Jobs/Install/CopyFiles.php | 3 +- app/Jobs/Install/DownloadFile.php | 3 +- app/Jobs/Install/UnzipFile.php | 3 +- app/Notifications/Banking/Transaction.php | 11 +++--- app/Notifications/Portal/PaymentReceived.php | 11 +++--- app/Notifications/Sale/Invoice.php | 22 +++++++----- app/Traits/Documents.php | 3 +- app/Traits/Transactions.php | 3 +- app/Utilities/helpers.php | 35 ++++++++++++++++++++ config/dompdf.php | 2 ++ config/filesystems.php | 5 +++ 12 files changed, 76 insertions(+), 28 deletions(-) diff --git a/app/Console/Commands/StorageTempClear.php b/app/Console/Commands/StorageTempClear.php index 3e4db1dfb..bb296b2d2 100644 --- a/app/Console/Commands/StorageTempClear.php +++ b/app/Console/Commands/StorageTempClear.php @@ -4,7 +4,6 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; -use Illuminate\Support\Facades\Storage; class StorageTempClear extends Command { @@ -32,7 +31,7 @@ class StorageTempClear extends Command { $filesystem = app(Filesystem::class); - $path = Storage::path('app/temp'); + $path = get_storage_path('app/temp'); foreach ($filesystem->glob("{$path}/*") as $file) { $filesystem->delete($file); diff --git a/app/Jobs/Install/CopyFiles.php b/app/Jobs/Install/CopyFiles.php index 548702d0b..536262c2c 100644 --- a/app/Jobs/Install/CopyFiles.php +++ b/app/Jobs/Install/CopyFiles.php @@ -5,7 +5,6 @@ namespace App\Jobs\Install; use App\Abstracts\Job; use Illuminate\Support\Facades\File; use Illuminate\Support\Str; -use Illuminate\Support\Facades\Storage; class CopyFiles extends Job { @@ -36,7 +35,7 @@ class CopyFiles extends Job throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias])); } - $source = Storage::path('app/temp/' . $this->path); + $source = storage_path('app/temp/' . $this->path); $destination = $this->getDestination($source); diff --git a/app/Jobs/Install/DownloadFile.php b/app/Jobs/Install/DownloadFile.php index b4029d25a..d249329ad 100644 --- a/app/Jobs/Install/DownloadFile.php +++ b/app/Jobs/Install/DownloadFile.php @@ -6,7 +6,6 @@ use App\Abstracts\Job; use App\Traits\SiteApi; use App\Utilities\Info; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Storage; class DownloadFile extends Job { @@ -48,7 +47,7 @@ class DownloadFile extends Job } $path = 'temp-' . md5(mt_rand()); - $temp_path = Storage::path('app/temp/' . $path); + $temp_path = storage_path('app/temp/' . $path); $file_path = $temp_path . '/upload.zip'; diff --git a/app/Jobs/Install/UnzipFile.php b/app/Jobs/Install/UnzipFile.php index a96cb0ff9..5ae79992f 100644 --- a/app/Jobs/Install/UnzipFile.php +++ b/app/Jobs/Install/UnzipFile.php @@ -4,7 +4,6 @@ namespace App\Jobs\Install; use App\Abstracts\Job; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Storage; use ZipArchive; class UnzipFile extends Job @@ -36,7 +35,7 @@ class UnzipFile extends Job throw new \Exception(trans('modules.errors.unzip', ['module' => $this->alias])); } - $temp_path = Storage::path('app/temp/' . $this->path); + $temp_path = storage_path('app/temp/' . $this->path); $file = $temp_path . '/upload.zip'; diff --git a/app/Notifications/Banking/Transaction.php b/app/Notifications/Banking/Transaction.php index d4df72404..48f22be00 100644 --- a/app/Notifications/Banking/Transaction.php +++ b/app/Notifications/Banking/Transaction.php @@ -6,9 +6,9 @@ use App\Abstracts\Notification; use App\Models\Banking\Transaction as Model; use App\Models\Setting\EmailTemplate; use App\Traits\Transactions; +use Illuminate\Mail\Attachment; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Facades\URL; -use Illuminate\Support\Str; class Transaction extends Notification { @@ -63,9 +63,12 @@ class Transaction extends Notification // Attach the PDF file if ($this->attach_pdf) { - $message->attach($this->storeTransactionPdfAndGetPath($this->transaction), [ - 'mime' => 'application/pdf', - ]); + $func = is_local_storage() ? 'fromPath' : 'fromStorage'; + + $path = $this->storeTransactionPdfAndGetPath($this->transaction); + $file = Attachment::$func($path)->withMime('application/pdf'); + + $message->attach($file); } return $message; diff --git a/app/Notifications/Portal/PaymentReceived.php b/app/Notifications/Portal/PaymentReceived.php index 4bc0a7dc9..28ff24ce2 100644 --- a/app/Notifications/Portal/PaymentReceived.php +++ b/app/Notifications/Portal/PaymentReceived.php @@ -7,9 +7,9 @@ use App\Models\Banking\Transaction; use App\Models\Setting\EmailTemplate; use App\Models\Document\Document; use App\Traits\Documents; +use Illuminate\Mail\Attachment; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Facades\URL; -use Illuminate\Support\Str; class PaymentReceived extends Notification { @@ -67,9 +67,12 @@ class PaymentReceived extends Notification // Attach the PDF file if ($this->attach_pdf) { - $message->attach($this->storeDocumentPdfAndGetPath($this->invoice), [ - 'mime' => 'application/pdf', - ]); + $func = is_local_storage() ? 'fromPath' : 'fromStorage'; + + $path = $this->storeDocumentPdfAndGetPath($this->invoice); + $file = Attachment::$func($path)->withMime('application/pdf'); + + $message->attach($file); } return $message; diff --git a/app/Notifications/Sale/Invoice.php b/app/Notifications/Sale/Invoice.php index 9a85fa451..73b48d281 100644 --- a/app/Notifications/Sale/Invoice.php +++ b/app/Notifications/Sale/Invoice.php @@ -6,9 +6,9 @@ use App\Abstracts\Notification; use App\Models\Setting\EmailTemplate; use App\Models\Document\Document; use App\Traits\Documents; +use Illuminate\Mail\Attachment; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Facades\URL; -use Illuminate\Support\Str; class Invoice extends Notification { @@ -69,21 +69,27 @@ class Invoice extends Notification $message = $this->initMailMessage(); + $func = is_local_storage() ? 'fromPath' : 'fromStorage'; + // Attach the PDF file if ($this->attach_pdf) { - $message->attach($this->storeDocumentPdfAndGetPath($this->invoice), [ - 'mime' => 'application/pdf', - ]); + $path = $this->storeDocumentPdfAndGetPath($this->invoice); + $file = Attachment::$func($path)->withMime('application/pdf'); + + $message->attach($file); } // Attach selected attachments if (! empty($this->invoice->attachment)) { foreach ($this->invoice->attachment as $attachment) { - if (in_array($attachment->id, $this->attachments)) { - $message->attach($attachment->getAbsolutePath(), [ - 'mime' => $attachment->mime_type, - ]); + if (! in_array($attachment->id, $this->attachments)) { + continue; } + + $path = is_local_storage() ? $attachment->getAbsolutePath() : $attachment->getDiskPath(); + $file = Attachment::$func($path)->withMime($attachment->mime_type); + + $message->attach($file); } } diff --git a/app/Traits/Documents.php b/app/Traits/Documents.php index a555096c0..b9f069c21 100644 --- a/app/Traits/Documents.php +++ b/app/Traits/Documents.php @@ -13,7 +13,6 @@ use Egulias\EmailValidator\Validation\MultipleValidationWithAnd; use Egulias\EmailValidator\Validation\RFCValidation; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use Illuminate\Support\Facades\Storage; trait Documents { @@ -188,7 +187,7 @@ trait Documents $file_name = $this->getDocumentFileName($document); - $pdf_path = Storage::path('app/temp/' . $file_name); + $pdf_path = get_storage_path('app/temp/' . $file_name); // Save the PDF file into temp folder $pdf->save($pdf_path); diff --git a/app/Traits/Transactions.php b/app/Traits/Transactions.php index 85d754692..c9de6f7d5 100644 --- a/app/Traits/Transactions.php +++ b/app/Traits/Transactions.php @@ -6,7 +6,6 @@ use App\Events\Banking\TransactionPrinting; use App\Models\Banking\Transaction; use App\Interfaces\Utility\TransactionNumber; use Illuminate\Support\Str; -use Illuminate\Support\Facades\Storage; trait Transactions { @@ -162,7 +161,7 @@ trait Transactions $file_name = $this->getTransactionFileName($transaction); - $pdf_path = Storage::path('app/temp/' . $file_name); + $pdf_path = get_storage_path('app/temp/' . $file_name); // Save the PDF file into temp folder $pdf->save($pdf_path); diff --git a/app/Utilities/helpers.php b/app/Utilities/helpers.php index 8d1e995a0..cb5086320 100644 --- a/app/Utilities/helpers.php +++ b/app/Utilities/helpers.php @@ -6,6 +6,7 @@ use App\Traits\Sources; use App\Traits\Modules; use App\Utilities\Date; use App\Utilities\Widgets; +use Illuminate\Support\Facades\Storage; if (! function_exists('user')) { /** @@ -248,3 +249,37 @@ if (! function_exists('env_is_testing')) { return config('app.env') === 'testing'; } } + +if (! function_exists('is_local_storage')) { + /** + * Determine if the storage is local. + */ + function is_local_storage(): bool + { + $driver = config('filesystems.disks.' . config('filesystems.default') . '.driver'); + + return $driver == 'local'; + } +} + +if (! function_exists('is_cloud_storage')) { + /** + * Determine if the storage is cloud. + */ + function is_cloud_storage(): bool + { + return ! is_local_storage(); + } +} + +if (! function_exists('get_storage_path')) { + /** + * Get the path from the storage. + */ + function get_storage_path(string $path = ''): string + { + return is_local_storage() + ? storage_path($path) + : Storage::path($path); + } +} diff --git a/config/dompdf.php b/config/dompdf.php index 814e7cf67..e24168a98 100644 --- a/config/dompdf.php +++ b/config/dompdf.php @@ -2,6 +2,8 @@ return [ + 'disk' => env('DOMPDF_DISK', null), + /* |-------------------------------------------------------------------------- | Settings diff --git a/config/filesystems.php b/config/filesystems.php index d64096a20..c7b18deb5 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -81,6 +81,7 @@ return [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), + 'throw' => false, ], 'public' => [ @@ -88,6 +89,7 @@ return [ 'root' => storage_path('app/public'), 'url' => app()->runningInConsole() ? '' : url('/') . '/storage', 'visibility' => 'public', + 'throw' => false, ], 'temp' => [ @@ -95,6 +97,7 @@ return [ 'root' => storage_path('app/temp'), 'url' => app()->runningInConsole() ? '' : url('/') . '/temp', 'visibility' => 'private', + 'throw' => false, ], 'uploads' => [ @@ -102,6 +105,7 @@ return [ 'root' => storage_path('app/uploads'), 'url' => app()->runningInConsole() ? '' : url('/') . '/uploads', 'visibility' => 'private', + 'throw' => false, ], 's3' => [ @@ -115,6 +119,7 @@ return [ 'endpoint' => env('AWS_ENDPOINT'), 'visibility' => env('AWS_VISIBILITY', 'private'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), + 'throw' => false, ], ],