json([ 'error' => 'Invalid Token!', ], 500); } // Check if permitted if (Carbon::now() > Carbon::parse($time)) { return response()->json([ 'error' => 'Link has expired!', ], 404); } if (! Storage::exists($file)) { return response()->json([ 'error' => 'File not found!', ], 404); } return response()->json([ 'size' => Storage::size($file), ]); } /** * Check if a storage path exists */ public function checkPath(string $folderhash, string $timehash): \Illuminate\Http\JsonResponse { try { $path = Crypt::decryptString($folderhash); $time = Crypt::decryptString($timehash); } catch (DecryptException $e) { return response()->json(['valid' => false, 'error' => 'Invalid Token!'], 422); } if (Carbon::now() > Carbon::parse($time)) { return response()->json(['valid' => false, 'error' => 'Link has expired!'], 410); } // Defensive: reject traversal; only allow the known prefixes / year dirs. if (str_contains($path, '..')) { return response()->json(['valid' => false, 'error' => 'Invalid path!'], 422); } if (! Storage::exists($path)) { return response()->json(['valid' => false, 'error' => 'File not found!'], 404); } return response()->json([ 'valid' => true, 'type' => Storage::directoryExists($path) ? 'directory' : 'file', 'size' => Storage::directoryExists($path) ? null : Storage::size($path), ]); } /** * Download File */ public function download(string $folderhash, string $timehash) { try { // Decrypt Hash $file = Crypt::decryptString($folderhash); $time = Crypt::decryptString($timehash); // Check if permitted if (Carbon::now() > Carbon::parse($time)) { return view('error', ['error' => 'Link has expired!']); } if (! Storage::exists($file)) { return view('error', ['error' => 'File not found!']); } $filename = explode('/', $file); $filePath = storage_path('app/'.$file); $headers = [ 'Content-Type' => Storage::mimeType($file), 'Content-Length' => Storage::size($file), 'Content-Disposition' => 'attachment; filename="'.end($filename).'"', ]; // Stream the file in chunks to prevent memory issues return response()->stream( function () use ($filePath) { $fd = fopen($filePath, 'rb'); while (! feof($fd)) { echo fread($fd, 2048); } }, 200, $headers ); } catch (DecryptException $e) { } return view('error', ['error' => 'Invalid Token!']); } }