Fix asset export to create ZIP file with manifest
This commit is contained in:
@@ -9,34 +9,46 @@ use ZipArchive;
|
||||
|
||||
class AssetController extends Controller
|
||||
{
|
||||
public function export(): \Symfony\Component\HttpFoundation\StreamedResponse
|
||||
public function export()
|
||||
{
|
||||
$user = auth()->user();
|
||||
if (!$user || !$user->roles()->where('name', 'ROLE_ADMIN')->exists()) {
|
||||
abort(403, 'Forbidden');
|
||||
}
|
||||
|
||||
$storagePublic = storage_path('app/public');
|
||||
try {
|
||||
$storagePublic = storage_path('app/public');
|
||||
|
||||
if (!is_dir($storagePublic)) {
|
||||
mkdir($storagePublic, 0775, true);
|
||||
if (!is_dir($storagePublic)) {
|
||||
mkdir($storagePublic, 0775, true);
|
||||
}
|
||||
|
||||
// Create ZIP in memory
|
||||
$zip = new ZipArchive();
|
||||
$tempFile = tempnam(sys_get_temp_dir(), 'speedbb_');
|
||||
|
||||
if ($zip->open($tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||
return response()->json(['message' => 'Failed to create ZIP'], 500);
|
||||
}
|
||||
|
||||
// Add a manifest file to ensure ZIP is created even if empty
|
||||
$zip->addFromString('MANIFEST.txt', "SpeedBB Assets Export\nExported: " . date('Y-m-d H:i:s') . "\n");
|
||||
$this->addDirectoryToZip($zip, $storagePublic, '');
|
||||
$zip->close();
|
||||
|
||||
if (!file_exists($tempFile)) {
|
||||
return response()->json(['message' => 'ZIP file was not created'], 500);
|
||||
}
|
||||
|
||||
$filename = 'assets-'.date('YmdHis').'.zip';
|
||||
|
||||
return response()->file($tempFile, [
|
||||
'Content-Type' => 'application/zip',
|
||||
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
|
||||
])->deleteFileAfterSend(true);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['message' => 'Export error: '.$e->getMessage()], 500);
|
||||
}
|
||||
|
||||
$zip = new ZipArchive();
|
||||
$zipPath = storage_path('exports/assets-'.date('YmdHis').'.zip');
|
||||
|
||||
if (!is_dir(storage_path('exports'))) {
|
||||
mkdir(storage_path('exports'), 0775, true);
|
||||
}
|
||||
|
||||
if ($zip->open($zipPath, ZipArchive::CREATE) !== true) {
|
||||
abort(500, 'Failed to create ZIP archive');
|
||||
}
|
||||
|
||||
$this->addDirectoryToZip($zip, $storagePublic, '');
|
||||
$zip->close();
|
||||
|
||||
return response()->download($zipPath)->deleteFileAfterSend(true);
|
||||
}
|
||||
|
||||
public function import(Request $request): JsonResponse
|
||||
|
||||
Reference in New Issue
Block a user