ldc-showroom-bo/app/Http/Controllers/Bo/ExportController.php

40 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2024-01-08 15:55:07 +01:00
<?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
2024-01-08 15:55:07 +01:00
{
$exportFolder = storage_path('app/export');
if (!File::exists($exportFolder)) {
File::makeDirectory($exportFolder, 0700, true);
}
$exportedFiles = File::allFiles($exportFolder);
2025-05-20 12:51:38 +02:00
\dispatch_sync(new ShowroomExport(\tenant('id')));
$exportedFile = collect(File::allFiles($exportFolder))->diff($exportedFiles)->first();
2024-01-08 15:55:07 +01:00
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())
]);
}
}