mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor for zip files
This commit is contained in:
parent
5259728443
commit
118d2bc214
@ -22,8 +22,6 @@ use App\Utils\Traits\MakesHash;
|
|||||||
use Illuminate\Contracts\View\Factory;
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
|
|
||||||
class DocumentController extends Controller
|
class DocumentController extends Controller
|
||||||
{
|
{
|
||||||
@ -71,25 +69,34 @@ class DocumentController extends Controller
|
|||||||
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
|
public function downloadMultiple(DownloadMultipleDocumentsRequest $request)
|
||||||
{
|
{
|
||||||
$documents = Document::whereIn('id', $this->transformKeys($request->file_hash))
|
$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();
|
->get();
|
||||||
|
|
||||||
$documents->map(function ($document) {
|
$zipFile = new \PhpZip\ZipFile();
|
||||||
if (auth()->guard('contact')->user()->client->id != $document->documentable->id) {
|
|
||||||
abort(401, 'Permission denied');
|
try{
|
||||||
|
|
||||||
|
foreach ($documents as $document) {
|
||||||
|
$zipFile->addFile(TempFile::path($document->filePath()), $document->name);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
$options = new Archive();
|
$filename = now() . '-documents.zip';
|
||||||
|
$filepath = sys_get_temp_dir() . '/' . $filename;
|
||||||
|
|
||||||
$options->setSendHttpHeaders(true);
|
$zipFile->saveAsFile($filepath) // save the archive to a file
|
||||||
|
->close(); // close archive
|
||||||
|
|
||||||
|
return response()->download($filepath, $filename)->deleteFileAfterSend(true);
|
||||||
|
|
||||||
$zip = new ZipStream(now() . '-documents.zip', $options);
|
}
|
||||||
|
catch(\PhpZip\Exception\ZipException $e){
|
||||||
foreach ($documents as $document) {
|
// handle exception
|
||||||
$zip->addFileFromPath(basename($document->diskPath()), TempFile::path($document->filePath()));
|
}
|
||||||
|
finally{
|
||||||
|
$zipFile->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip->finish();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,8 +28,6 @@ use Illuminate\Contracts\View\Factory;
|
|||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class InvoiceController extends Controller
|
class InvoiceController extends Controller
|
||||||
@ -198,9 +196,6 @@ class InvoiceController extends Controller
|
|||||||
* @param array $ids
|
* @param array $ids
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
* @throws \ZipStream\Exception\FileNotFoundException
|
|
||||||
* @throws \ZipStream\Exception\FileNotReadableException
|
|
||||||
* @throws \ZipStream\Exception\OverflowException
|
|
||||||
*/
|
*/
|
||||||
private function downloadInvoicePDF(array $ids)
|
private function downloadInvoicePDF(array $ids)
|
||||||
{
|
{
|
||||||
@ -230,22 +225,6 @@ class InvoiceController extends Controller
|
|||||||
|
|
||||||
return $this->buildZip($invoices);
|
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,8 +28,6 @@ use Illuminate\Contracts\View\Factory;
|
|||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
@ -140,21 +138,38 @@ class QuoteController extends Controller
|
|||||||
}, basename($file), ['Content-Type' => 'application/pdf']);
|
}, basename($file), ['Content-Type' => 'application/pdf']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// enable output of HTTP headers
|
return $this->buildZip($quotes);
|
||||||
$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 ($quotes as $quote) {
|
private function buildZip($quotes)
|
||||||
$zip->addFile(basename($quote->pdf_file_path()), file_get_contents($quote->pdf_file_path(null, 'url', true)));
|
{
|
||||||
|
// 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);
|
||||||
|
|
||||||
// $zip->addFileFromPath(basename($quote->pdf_file_path()), TempFile::path($quote->pdf_file_path()));
|
|
||||||
}
|
}
|
||||||
|
catch(\PhpZip\Exception\ZipException $e){
|
||||||
// finish the zip stream
|
// handle exception
|
||||||
$zip->finish();
|
}
|
||||||
|
finally{
|
||||||
|
$zipFile->close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function approve(array $ids, $process = false)
|
protected function approve(array $ids, $process = false)
|
||||||
|
@ -33,8 +33,6 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
|||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
|
|
||||||
class CompanyExport implements ShouldQueue
|
class CompanyExport implements ShouldQueue
|
||||||
|
@ -74,8 +74,6 @@ use Illuminate\Support\Str;
|
|||||||
use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
use JsonMachine\JsonDecoder\ExtJsonDecoder;
|
||||||
use JsonMachine\JsonMachine;
|
use JsonMachine\JsonMachine;
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
|
|
||||||
use function GuzzleHttp\json_encode;
|
use function GuzzleHttp\json_encode;
|
||||||
|
|
||||||
|
@ -25,8 +25,6 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\Mail;
|
use Illuminate\Support\Facades\Mail;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use ZipStream\Option\Archive;
|
|
||||||
use ZipStream\ZipStream;
|
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
|
|
||||||
class ZipInvoices implements ShouldQueue
|
class ZipInvoices implements ShouldQueue
|
||||||
|
@ -65,7 +65,6 @@
|
|||||||
"league/fractal": "^0.17.0",
|
"league/fractal": "^0.17.0",
|
||||||
"league/omnipay": "^3.1",
|
"league/omnipay": "^3.1",
|
||||||
"livewire/livewire": "^2.6",
|
"livewire/livewire": "^2.6",
|
||||||
"maennchen/zipstream-php": "^1.2",
|
|
||||||
"mollie/mollie-api-php": "^2.36",
|
"mollie/mollie-api-php": "^2.36",
|
||||||
"nelexa/zip": "^4.0",
|
"nelexa/zip": "^4.0",
|
||||||
"nwidart/laravel-modules": "^8.0",
|
"nwidart/laravel-modules": "^8.0",
|
||||||
|
@ -24,6 +24,7 @@ const appendToElement = (parent, value) => {
|
|||||||
_temp.hidden = true;
|
_temp.hidden = true;
|
||||||
|
|
||||||
_parent.append(_temp);
|
_parent.append(_temp);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
window.appendToElement = appendToElement;
|
window.appendToElement = appendToElement;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user