Refactor for zip files

This commit is contained in:
David Bomba 2022-02-18 21:45:01 +11:00
parent 5259728443
commit 118d2bc214
8 changed files with 50 additions and 55 deletions

View File

@ -22,8 +22,6 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class DocumentController extends Controller
{
@ -71,25 +69,34 @@ class DocumentController extends Controller
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
{
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
->where('company_id', auth()->guard('contact')->user()->company->id)
->where('company_id', auth()->guard('contact')->user()->company_id)
->get();
$documents->map(function ($document) {
if (auth()->guard('contact')->user()->client->id != $document->documentable->id) {
abort(401, 'Permission denied');
}
});
$zipFile = new \PhpZip\ZipFile();
$options = new Archive();
$options->setSendHttpHeaders(true);
$zip = new ZipStream(now() . '-documents.zip', $options);
try{
foreach ($documents as $document) {
$zip->addFileFromPath(basename($document->diskPath()), TempFile::path($document->filePath()));
$zipFile->addFile(TempFile::path($document->filePath()), $document->name);
}
$zip->finish();
$filename = now() . '-documents.zip';
$filepath = sys_get_temp_dir() . '/' . $filename;
$zipFile->saveAsFile($filepath) // save the archive to a file
->close(); // close archive
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
}
catch(\PhpZip\Exception\ZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
}
}

View File

@ -28,8 +28,6 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use Illuminate\Http\Request;
class InvoiceController extends Controller
@ -198,9 +196,6 @@ class InvoiceController extends Controller
* @param array $ids
*
* @return void
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
private function downloadInvoicePDF(array $ids)
{
@ -230,22 +225,6 @@ class InvoiceController extends Controller
return $this->buildZip($invoices);
// // enable output of HTTP headers
// $options = new Archive();
// $options->setSendHttpHeaders(true);
// // create a new zipstream object
// $zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options);
// foreach ($invoices as $invoice) {
// #add it to the zip
// $zip->addFile(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true)));
// }
// // finish the zip stream
// $zip->finish();
}

View File

@ -28,8 +28,6 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
@ -140,21 +138,38 @@ class QuoteController extends Controller
}, basename($file), ['Content-Type' => 'application/pdf']);
}
// enable output of HTTP headers
$options = new Archive();
$options->setSendHttpHeaders(true);
return $this->buildZip($quotes);
// create a new zipstream object
$zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options);
foreach ($quotes as $quote) {
$zip->addFile(basename($quote->pdf_file_path()), file_get_contents($quote->pdf_file_path(null, 'url', true)));
// $zip->addFileFromPath(basename($quote->pdf_file_path()), TempFile::path($quote->pdf_file_path()));
}
// finish the zip stream
$zip->finish();
private function buildZip($quotes)
{
// create new archive
$zipFile = new \PhpZip\ZipFile();
try{
foreach ($quotes as $quote) {
#add it to the zip
$zipFile->addFromString(basename($quote->pdf_file_path()), file_get_contents($quote->pdf_file_path(null, 'url', true)));
}
$filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.quotes')).'.zip';
$filepath = sys_get_temp_dir() . '/' . $filename;
$zipFile->saveAsFile($filepath) // save the archive to a file
->close(); // close archive
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
}
catch(\PhpZip\Exception\ZipException $e){
// handle exception
}
finally{
$zipFile->close();
}
}
protected function approve(array $ids, $process = false)

View File

@ -33,8 +33,6 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use Illuminate\Support\Facades\App;
class CompanyExport implements ShouldQueue

View File

@ -74,8 +74,6 @@ use Illuminate\Support\Str;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonMachine;
use ZipArchive;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use function GuzzleHttp\json_encode;

View File

@ -25,8 +25,6 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
use ZipArchive;
class ZipInvoices implements ShouldQueue

View File

@ -65,7 +65,6 @@
"league/fractal": "^0.17.0",
"league/omnipay": "^3.1",
"livewire/livewire": "^2.6",
"maennchen/zipstream-php": "^1.2",
"mollie/mollie-api-php": "^2.36",
"nelexa/zip": "^4.0",
"nwidart/laravel-modules": "^8.0",

View File

@ -24,6 +24,7 @@ const appendToElement = (parent, value) => {
_temp.hidden = true;
_parent.append(_temp);
};
window.appendToElement = appendToElement;