diff --git a/app/Abstracts/BulkAction.php b/app/Abstracts/BulkAction.php index 87f0e8795..a95e370fe 100644 --- a/app/Abstracts/BulkAction.php +++ b/app/Abstracts/BulkAction.php @@ -304,7 +304,7 @@ abstract class BulkAction $batch[] = new CreateMediableForDownload(user(), $file_name, $translation); - Bus::chain($batch)->onQueue('default')->dispatch(); + Bus::chain($batch)->onQueue('jobs')->dispatch(); $message = trans('messages.success.download_queued', ['type' => $translation]); @@ -314,7 +314,7 @@ abstract class BulkAction } else { $this->dispatch(new CreateZipForDownload($selected, $class, $file_name)); - $folder_path = 'app/temp/' . company_id() . '/bulk_actions/'; + $folder_path = 'app' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . company_id() . DIRECTORY_SEPARATOR . 'bulk_actions' . DIRECTORY_SEPARATOR; return response()->download(get_storage_path($folder_path . $file_name . '.zip'))->deleteFileAfterSend(true); } diff --git a/app/Jobs/Common/CreateMediableForDownload.php b/app/Jobs/Common/CreateMediableForDownload.php index 03413d00d..29ce0455c 100644 --- a/app/Jobs/Common/CreateMediableForDownload.php +++ b/app/Jobs/Common/CreateMediableForDownload.php @@ -60,7 +60,7 @@ class CreateMediableForDownload extends JobShouldQueue public function getQueuedMedia() { - return config('excel.temporary_files.remote_disk') !== null + return config('dompdf.disk') !== null ? $this->getRemoteQueuedMedia() : $this->getLocalQueuedMedia(); } @@ -88,12 +88,15 @@ class CreateMediableForDownload extends JobShouldQueue public function getRemoteQueuedMedia() { - $disk = config('excel.temporary_files.remote_disk'); - $prefix = config('excel.temporary_files.remote_prefix'); + $disk = config('dompdf.disk'); - $content = Storage::disk($disk)->get($this->file_name); + $folder_path = 'app/temp/' . company_id() . '/bulk_actions/'; - $file_name = str_replace([$prefix, '.xlsx', '.xls'], '', $this->file_name); + $source = get_storage_path($folder_path . $this->file_name . '.zip'); + + $content = Storage::disk($disk)->get($source); + + $file_name = str_replace(['.pdf', '.zip'], '', $this->file_name); $destination = $this->getMediaFolder('bulk_actions'); @@ -106,7 +109,7 @@ class CreateMediableForDownload extends JobShouldQueue ->toDirectory($destination) ->upload(); - Storage::disk($disk)->delete($this->file_name); + Storage::disk($disk)->delete($source); return $media; } diff --git a/app/Jobs/Common/CreateZipForDownload.php b/app/Jobs/Common/CreateZipForDownload.php index 6a3102ab7..1c336c481 100644 --- a/app/Jobs/Common/CreateZipForDownload.php +++ b/app/Jobs/Common/CreateZipForDownload.php @@ -4,6 +4,7 @@ namespace App\Jobs\Common; use App\Abstracts\JobShouldQueue; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Storage; use ZipArchive; class CreateZipForDownload extends JobShouldQueue @@ -34,27 +35,76 @@ class CreateZipForDownload extends JobShouldQueue { $zip_archive = new ZipArchive(); - $folder_path = 'app/temp/' . company_id() . '/bulk_actions/'; + $folder_path = 'app' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . company_id() . DIRECTORY_SEPARATOR . 'bulk_actions' . DIRECTORY_SEPARATOR; + File::ensureDirectoryExists(storage_path($folder_path)); File::ensureDirectoryExists(get_storage_path($folder_path)); - $zip_path = get_storage_path($folder_path . $this->file_name . '.zip'); + $zip_path = storage_path($folder_path . $this->file_name . '.zip'); $zip_archive->open($zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE); - - $total = count($this->selected); - $current = 0; foreach ($this->selected as $selected) { - $current++; - - if ($current === $total) { - $this->dispatch(new $this->class($selected, $folder_path, $zip_archive, true)); - } else { - $this->dispatch(new $this->class($selected, $folder_path, $zip_archive)); - } + $pdf_path = $this->dispatch(new $this->class($selected, $folder_path)); + + $fileContent = $this->getQueuedFile($pdf_path); + + $zip_archive->addFromString(basename($pdf_path), $fileContent); + + /* + Storage::disk('local')->put($folder_path . basename($pdf_path), $fileContent); + + $zip->addFile(storage_path($folder_path . basename($pdf_path)), basename($pdf_path)); + */ } + $zip_archive->close(); + + $this->copyQueuedFile($folder_path, $zip_path); + return $zip_path; } + + public function getQueuedFile($pdf_path) + { + return config('dompdf.disk') !== null + ? $this->getRemoteQueuedMedia($pdf_path) + : $this->getLocalQueuedMedia($pdf_path); + } + + public function getLocalQueuedMedia($pdf_path) + { + $content = File::get($pdf_path); + + return $content; + } + + public function getRemoteQueuedMedia($pdf_path) + { + $disk = config('dompdf.disk'); + + $content = Storage::disk($disk)->get($pdf_path); + + return $content; + } + + public function copyQueuedFile($folder_path, $zip_path) + { + return config('dompdf.disk') !== null + ? $this->copyRemoteQueuedMedia($folder_path, $zip_path) + : true; + } + + public function copyRemoteQueuedMedia($folder_path, $zip_path) + { + $disk = config('dompdf.disk'); + + $file_path = get_storage_path($folder_path . basename($zip_path)); + + $content = Storage::disk($disk)->put($file_path, fopen($zip_path, 'r+')); + + report($file_path); + + return $content; + } } diff --git a/app/Jobs/Document/DownloadDocument.php b/app/Jobs/Document/DownloadDocument.php index 9c70f7130..47cb60175 100644 --- a/app/Jobs/Document/DownloadDocument.php +++ b/app/Jobs/Document/DownloadDocument.php @@ -49,28 +49,20 @@ class DownloadDocument extends Job switch ($this->method) { case 'download': - return $pdf->download($file_name); + $response = $pdf->download($file_name); break; default: - if (empty($this->zip_archive)) { - return; - } - $pdf_path = get_storage_path($this->folder_path . $file_name); // Save the PDF file into temp folder $pdf->save($pdf_path); - $this->zip_archive->addFile($pdf_path, $file_name); - - if ($this->close_zip) { - $this->zip_archive->close(); - } - - return; + $response = $pdf_path; break; } + + return $response; } } \ No newline at end of file