ldc-showroom-bo/app/Http/Controllers/Bo/ExportController.php
2024-01-08 15:55:07 +01:00

34 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Jobs\ShowroomExport;
use Illuminate\Support\Facades\File;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ExportController extends Controller
{
/**
* Export and download zip File
*
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function exportToZip(): StreamedResponse
{
$exportedFiles = File::allFiles(storage_path('app/export'));
\dispatch_sync(new ShowroomExport());
$exportedFile = collect(File::allFiles(storage_path('app/export')))->diff($exportedFiles)->first();
if (!$exportedFile) {
response()->redirectToRoute('bo.dashboard')->withErrors([
'Export failed to catch the exported file'
])->throwResponse();
}
/** @var \Symfony\Component\Finder\SplFileInfo $exportedFile */
return response()->streamDownload(function () use ($exportedFile) {
echo $exportedFile->getContents();
}, $exportedFile->getBasename(), [
'Content-Type' => File::mimeType($exportedFile->getRealPath())
]);
}
}