config('file_links.enabled'), ]); } public function uploadWeb(Request $request) { if (!config('file_links.enabled')) { return redirect()->route('files.index')->withErrors(['file' => 'Функционал отключен']); } $data = $this->storeAndBuildTemporaryLink($request); return redirect()->route('files.index')->with([ 'file_link' => $data['url'], 'file_expires_at' => $data['expiresAt'], ]); } public function upload(Request $request): JsonResponse { if (!config('file_links.enabled')) { return response()->json(['message' => 'Функционал отключен'], Response::HTTP_NOT_FOUND); } $data = $this->storeAndBuildTemporaryLink($request); return response()->json([ 'path' => $data['path'], 'expiresAt' => $data['expiresAt'], 'url' => $data['url'], ]); } public function download(string $path) { if (!config('file_links.enabled')) { abort(Response::HTTP_NOT_FOUND); } $disk = config('file_links.disk'); if (!Storage::disk($disk)->exists($path)) { abort(Response::HTTP_NOT_FOUND); } return Storage::disk($disk)->download($path); } private function storeAndBuildTemporaryLink(Request $request): array { $request->validate([ 'file' => 'required|file|max:' . config('file_links.max_size_kb'), ]); $disk = config('file_links.disk'); $directory = config('file_links.directory'); $path = $request->file('file')->store($directory, $disk); $expiresAt = now()->addHour(); return [ 'path' => $path, 'expiresAt' => $expiresAt->toIso8601String(), 'url' => URL::temporarySignedRoute('files.download', $expiresAt, ['path' => $path]), ]; } }