Feat: Bulk file downloads

This commit is contained in:
Sallu9007
2025-05-11 14:59:00 +05:30
parent 6b84de9472
commit c224497740
3 changed files with 529 additions and 312 deletions

View File

@@ -39,7 +39,7 @@ class ActivitiesAttendedController extends Controller
'activity_type' => 'required|string',
'category' => 'required|string',
'level' => 'required|string',
'proof' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip',
'proof' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip,ppt,pptx|max:5120', // 5MB max
]);
// Combine start date and time
@@ -113,6 +113,73 @@ class ActivitiesAttendedController extends Controller
return response()->json(['success' => 'Record deleted successfully']);
}
public function downloadProofs(Request $request)
{
// Validate the request
$request->validate([
'ids' => 'required|string',
]);
$ids = json_decode($request->input('ids'));
if (empty($ids)) {
return back()->with('error', 'No items selected');
}
// Get the records with proofs
$responses = ActivitiesAttended::whereIn('id', $ids)
->whereNotNull('proof')
->get();
if ($responses->isEmpty()) {
return back()->with('error', 'No valid proofs found');
}
// Create a temporary zip file
$zipFileName = 'proofs_' . time() . '.zip';
$zipFilePath = storage_path('app/public/temp/' . $zipFileName);
// Ensure the temp directory exists
if (!Storage::disk('public')->exists('temp')) {
Storage::disk('public')->makeDirectory('temp');
}
$zip = new \ZipArchive();
if ($zip->open($zipFilePath, \ZipArchive::CREATE) !== TRUE) {
return back()->with('error', 'Could not create zip file');
}
$fileCount = 0;
foreach ($responses as $response) {
if (Storage::disk('public')->exists($response->proof)) {
$fileContent = Storage::disk('public')->get($response->proof);
// Get original filename from path
$originalName = basename($response->proof);
// Add file to the zip with a prefix to avoid duplicate names
$fileNameInZip = $response->id . '_' . $originalName;
$zip->addFromString($fileNameInZip, $fileContent);
$fileCount++;
}
}
$zip->close();
if ($fileCount === 0) {
// Delete the empty zip file
if (file_exists($zipFilePath)) {
unlink($zipFilePath);
}
return back()->with('error', 'No files found to download');
}
// Return the zip file as a download
return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend(true);
}
public function getActivitiesAttendedResponses(Request $request)
{
$user = auth()->user();