Bladeren bron

fix tech docs download

Alexander Musikhin 6 dagen geleden
bovenliggende
commit
dd8c44d79a
1 gewijzigde bestanden met toevoegingen van 41 en 31 verwijderingen
  1. 41 31
      app/Http/Controllers/OrderController.php

+ 41 - 31
app/Http/Controllers/OrderController.php

@@ -29,9 +29,10 @@ use App\Services\FileService;
 use App\Services\NotificationService;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
+use Illuminate\Support\Str;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Storage;
-use Symfony\Component\HttpFoundation\StreamedResponse;
+use Symfony\Component\HttpFoundation\BinaryFileResponse;
 use Throwable;
 use ZipArchive;
 
@@ -657,7 +658,7 @@ class OrderController extends Controller
 
     }
 
-    public function downloadTechDocs(Order $order): StreamedResponse
+    public function downloadTechDocs(Order $order): BinaryFileResponse
     {
         $techDocsPath = base_path('tech-docs');
         $articles = $order->products_sku
@@ -670,46 +671,55 @@ class OrderController extends Controller
             abort(404, 'Нет МАФов на площадке');
         }
 
-        $archiveName = 'Тех.документация - ' . fileName($order->object_address) . '.zip';
+        $tempFile = tempnam(sys_get_temp_dir(), 'tech_docs_');
+        if ($tempFile === false) {
+            abort(500, 'Не удалось создать временный файл архива');
+        }
 
-        return response()->streamDownload(function () use ($techDocsPath, $articles) {
-            $zip = new ZipArchive();
-            $tempFile = tempnam(storage_path('app/temp/'), 'tech_docs_');
-            $zip->open($tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
+        $zip = new ZipArchive();
+        $result = $zip->open($tempFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
+        if ($result !== true) {
+            @unlink($tempFile);
+            abort(500, 'Не удалось создать архив документации');
+        }
 
-            $filesAdded = 0;
-            foreach ($articles as $article) {
-                $articlePath = $techDocsPath . '/' . $article;
-                if (!is_dir($articlePath)) {
-                    continue;
-                }
+        $filesAdded = 0;
+        foreach ($articles as $article) {
+            $articlePath = $techDocsPath . '/' . $article;
+            if (!is_dir($articlePath)) {
+                continue;
+            }
 
-                $files = new \RecursiveIteratorIterator(
-                    new \RecursiveDirectoryIterator($articlePath, \RecursiveDirectoryIterator::SKIP_DOTS),
-                    \RecursiveIteratorIterator::LEAVES_ONLY
-                );
+            $files = new \RecursiveIteratorIterator(
+                new \RecursiveDirectoryIterator($articlePath, \RecursiveDirectoryIterator::SKIP_DOTS),
+                \RecursiveIteratorIterator::LEAVES_ONLY
+            );
 
-                foreach ($files as $file) {
-                    if ($file->isFile()) {
-                        $relativePath = $article . '/' . $file->getFilename();
-                        $zip->addFile($file->getRealPath(), $relativePath);
-                        $filesAdded++;
-                    }
+            foreach ($files as $file) {
+                if ($file->isFile()) {
+                    $relativePath = $article . '/' . $file->getFilename();
+                    $zip->addFile($file->getRealPath(), $relativePath);
+                    $filesAdded++;
                 }
             }
+        }
 
-            if(!$filesAdded) {
-                $zip->addFromString('readme.txt', 'Нет документации для этих МАФ!');
-            }
+        if (!$filesAdded) {
+            $zip->addFromString('readme.txt', 'Нет документации для этих МАФ!');
+        }
 
-            $zip->close();
+        $zip->close();
 
-            readfile($tempFile);
-            unlink($tempFile);
+        // WebView clients are more reliable with ASCII attachment names.
+        $archiveName = 'tech_docs_order_' . $order->id . '_' . Str::slug((string)$order->object_address) . '.zip';
+        $archiveName = trim($archiveName, '._');
+        if ($archiveName === '.zip' || $archiveName === 'zip' || $archiveName === '') {
+            $archiveName = 'tech_docs_order_' . $order->id . '.zip';
+        }
 
-        }, $archiveName, [
+        return response()->download($tempFile, $archiveName, [
             'Content-Type' => 'application/zip',
-        ]);
+        ])->deleteFileAfterSend(true);
     }
 
 }