diff --git a/app/Http/Controllers/DownloadController.php b/app/Http/Controllers/DownloadController.php new file mode 100644 index 0000000..eb22724 --- /dev/null +++ b/app/Http/Controllers/DownloadController.php @@ -0,0 +1,44 @@ +firstOrFail(); + if (!$this->checkDownloadLimits($downloadableProduct) || !$this->authorizeDownload($request->user(), $downloadableProduct)) { + abort(403, 'Download limit reached or not authorized.'); + } + $temporaryUrl = Storage::disk('local')->temporaryUrl( + $downloadableProduct->file_url, now()->addMinutes(5) + ); + return response()->json(['url' => $temporaryUrl]); + } + + private function checkDownloadLimits(DownloadableProduct $downloadableProduct) + { + return $downloadableProduct->download_limit > $downloadableProduct->downloads_count && (!$downloadableProduct->expiration_time || $downloadableProduct->expiration_time->isFuture()); + } + + public function serveFile(Request $request, $productId) + { + $downloadableProduct = DownloadableProduct::where('product_id', $productId)->firstOrFail(); + if (!$this->checkDownloadLimits($downloadableProduct) || !$this->authorizeDownload($request->user(), $downloadableProduct)) { + abort(403, 'Download limit reached or not authorized.'); + } + $downloadableProduct->increment('downloads_count'); + return Storage::disk('local')->download($downloadableProduct->file_url); + } + + private function authorizeDownload($user, DownloadableProduct $downloadableProduct) + { + // Implement logic to verify user's purchase + return true; // Placeholder for purchase verification logic + } +}