I have a function to download a zip file with or without folders based on the file_type_id. However my local setup is returning a empty 200 response with (failed)net::ERR_FAILED. When i look at the temp folder where i created the zip file it is there and all the files are in it when i unzip it in the command line. So my question would be why this behaviour exists and if someone got a solution?
I also get all the good logs in the laravel.log so no errors or that the file could not be found
public function downloadZip($file_type, $resId)
{
$reservation = Reservation::with('contact.insurer')->where('id', $resId)->first();
if (Auth::user()->hasRole('verzekeraar')) {
$contact = Auth::user()->userable;
if ($contact->insurer_id != $reservation->contact->insurer_id) {
return AppJsonResponse::UnAuthorized('U heeft geen rechten voor dit dossier.');
}
}
$files = [];
$dirs = [];
if ($file_type == 2) {
$files = $reservation->files;
} else {
$dirs = Folder::whereNull('parent_id')->where('reservation_id', $reservation->id)->get();
$files = File::whereNull('parent_id')->where('reservation_id', $reservation->id)->where('file_type_id', 1)->get();
}
$zip_file = storage_path('app/tmp/wrapper.zip');
// Ensure tmp directory exists
if (!file_exists(storage_path('app/tmp'))) {
mkdir(storage_path('app/tmp'), 0755, true);
}
// Initializing PHP class
$zip = new \ZipArchive();
if ($zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== TRUE) {
\Log::error("Cannot open <$zipPath>\n");
return AppJsonResponse::Error('ZIP creation failed.');
}
if ($file_type == 2) {
foreach ($files as $file) {
if ($file->file_type_id == $file_type) {
$zip->addFile(Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path), 'storage/' . $file->name . '.' . strtolower($file->ext));
}
}
} else {
$ultPath = 'storage/';
$this->zipDocuments($dirs, $files, $ultPath, $reservation->id, $zip);
}
if (!$zip->close()) {
\Log::error("Failed to close the zip file.");
} else {
\Log::debug("Successfully closed zip file: $zip_file");
}
if (file_exists($zip_file)) {
Log::info("File size of ZIP: " . filesize($zip_file));
return response()->download($zip_file, 'wrapper.zip', [
'Content-Type' => 'application/zip',
]);
} else{
\Log::error('ZIP file is missing or too small: ' . $zip_file);
return AppJsonResponse::Error('ZIP creation failed.');
}
}
And this is the zipdocuments function
public function zipDocuments($dirs, $files, $ultPath, $resId, $zip)
{
foreach ($dirs as $dir) {
$currentPath = $ultPath . $dir->name . '/';
// Log current directory
\Log::debug("Entering directory: $currentPath");
// Add all files in current directory
$filesInDir = File::where('parent_id', $dir->id)
->where('reservation_id', $resId)
->where('file_type_id', 1)
->get();
foreach ($filesInDir as $file) {
$fullPath = Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path);
if (!file_exists($fullPath)) {
\Log::warning("File not found: $fullPath");
continue;
}
if($zip->addFile($fullPath, $currentPath . $file->name . '.' . strtolower($file->ext)) === true){
}
}
// Recursively process subfolders
$subDirs = Folder::where('parent_id', $dir->id)->where('reservation_id', $resId)->get();
$this->zipDocuments($subDirs, [], $currentPath, $resId, $zip);
}
// Add top-level files (only for first call)
foreach ($files as $file) {
$fullPath = Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path);
if (!file_exists($fullPath)) {
\Log::warning("Top-level file not found: $fullPath");
continue;
}
$zip->addFile($fullPath, $ultPath . $file->name . '.' . strtolower($file->ext));
}
return $zip;
}
I just call the endpoint using my angular application like this:
downloadZip(id, file_type): any {
return this.http.get('/audits/downloadzip/'+file_type+'/'+id, {observe: 'response', responseType: 'blob', headers: new HttpHeaders({'Cache-Control': 'no-cache'})});
}
and this gives the following message in the console and the response is empty
auditor-reservation-…ge.component.ts:174
GET /api/audits/downloadzip/1/2 net::ERR_FAILED 200 (OK)
Also a very weird thing is that when i add a readfile($zip_file) before i return the response i get a cors error but when i then use a browser extension to get rid of the cors error i get the file i expected. I am just stuck why it just isn't working in the first place