ldc-showroom-bo/app/Http/Controllers/Bo/ExportController.php
2025-07-09 15:28:33 +02:00

39 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Jobs\ShowroomExport;
use Illuminate\Support\Facades\File;
class ExportController extends Controller
{
/**
* Export and download zip File
*
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function exportToZip(): \Symfony\Component\HttpFoundation\StreamedResponse
{
$exportFolder = storage_path('app/export');
if (!File::exists($exportFolder)) {
File::makeDirectory($exportFolder, 0700, true);
}
$exportedFiles = File::allFiles($exportFolder);
\dispatch_sync(new ShowroomExport(\tenant('id')));
$exportedFile = collect(File::allFiles($exportFolder))->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())
]);
}
}