From 27530fb69355a63c4295e9afdc0dc95ec7f5db82 Mon Sep 17 00:00:00 2001 From: Seaony Date: Mon, 1 Jan 2018 16:14:26 +0800 Subject: [PATCH 1/2] add feature for article download --- app/Console/Commands/ClearDownloadDatas.php | 26 +++++++ app/Console/Kernel.php | 3 + app/Http/Controllers/UsersController.php | 73 ++++++++++++++----- app/Policies/UserPolicy.php | 5 ++ resources/lang/zh-CN/phphub.php | 1 + .../views/users/partials/basicinfo.blade.php | 7 ++ routes/web.php | 1 + 7 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 app/Console/Commands/ClearDownloadDatas.php diff --git a/app/Console/Commands/ClearDownloadDatas.php b/app/Console/Commands/ClearDownloadDatas.php new file mode 100644 index 00000000..07a9b4cd --- /dev/null +++ b/app/Console/Commands/ClearDownloadDatas.php @@ -0,0 +1,26 @@ +deleteDirectory('zip'); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index f298db4c..00e5528e 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel Commands\CalculateActiveUser::class, Commands\CalculateHotTopic::class, Commands\ClearUserData::class, + Commands\ClearDownloadDatas::class, Commands\SyncUserActivedTime::class, Commands\CalculateMaintainerWorks::class, @@ -55,5 +56,7 @@ protected function schedule(Schedule $schedule) $schedule->command('phphub:calculate-active-user')->everyTenMinutes(); $schedule->command('phphub:calculate-hot-topic')->everyTenMinutes(); $schedule->command('phphub:sync-user-actived-time')->everyTenMinutes(); + + $schedule->command('phphub:clear-download-data')->hourly(); } } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 315c0a0a..e7d2be59 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -1,11 +1,13 @@ middleware('auth', ['except' => [ + $this->middleware('auth', [ + 'except' => [ 'index', 'show', 'replies', - 'topics', 'articles', 'votes', 'following', - 'followers', 'githubCard', 'githubApiProxy', - ]]); + 'topics', 'articles', 'votes', 'following', + 'followers', 'githubCard', 'githubApiProxy', + ], + ]); } public function index() @@ -36,12 +40,12 @@ public function index() public function show($id) { - $user = User::findOrFail($id); - $topics = Topic::whose($user->id)->withoutArticle()->withoutBoardTopics()->recent()->limit(20)->get(); - $articles = Topic::whose($user->id)->onlyArticle()->withoutDraft()->recent()->with('blogs')->limit(20)->get(); - $blog = $user->blogs()->first(); - $replies = Reply::whose($user->id)->recent()->limit(20)->get(); - return view('users.show', compact('user','blog', 'articles', 'topics', 'replies')); + $user = User::findOrFail($id); + $topics = Topic::whose($user->id)->withoutArticle()->withoutBoardTopics()->recent()->limit(20)->get(); + $articles = Topic::whose($user->id)->onlyArticle()->withoutDraft()->recent()->with('blogs')->limit(20)->get(); + $blog = $user->blogs()->first(); + $replies = Reply::whose($user->id)->recent()->limit(20)->get(); + return view('users.show', compact('user', 'blog', 'articles', 'topics', 'replies')); } public function edit($id) @@ -87,7 +91,7 @@ public function articles($id) $user = User::findOrFail($id); $topics = Topic::whose($user->id)->onlyArticle()->withoutDraft()->recent()->with('blogs')->paginate(30); $user->update(['article_count' => $topics->total()]); - return view('users.articles', compact('user','blog', 'topics')); + return view('users.articles', compact('user', 'blog', 'topics')); } public function drafts() @@ -99,7 +103,7 @@ public function drafts() $user->draft_count = $user->topics()->onlyArticle()->draft()->count(); $user->save(); - return view('users.articles', compact('user','blog', 'topics')); + return view('users.articles', compact('user', 'blog', 'topics')); } public function votes($id) @@ -133,11 +137,11 @@ public function accessTokens($id) } $user = User::findOrFail($id); $sessions = OAuthSession::where([ - 'owner_type' => 'user', - 'owner_id' => Auth::id(), - ]) + 'owner_type' => 'user', + 'owner_id' => Auth::id(), + ]) ->with('token') - ->lists('id') ?: []; + ->lists('id') ? : []; $tokens = AccessToken::whereIn('session_id', $sessions)->get(); @@ -216,7 +220,7 @@ public function githubApiProxy($username) { $cache_name = 'github_api_proxy_user_' . $username; - return Cache::remember($cache_name, 1440, function () use ($username) { + return Cache::remember($cache_name, 1440, function() use ($username) { $result = (new GithubUserDataReader())->getDataFromUserName($username); return response()->json($result); @@ -320,4 +324,39 @@ public function emailVerificationRequired() return view('users.emailverificationrequired'); } + + public function downloads(Request $request, $id) + { + $user = User::findOrFail($id); + $this->authorize('download', $user); + + $disk = Storage::disk('local'); + $baseDir = "zip/article_$user->id"; + $zipFile = "$baseDir.zip"; + + if (!$disk->exists($baseDir)) { + Topic::whose($user->id) + ->onlyArticle() + ->withoutDraft() + ->get(['title', 'body']) + ->each( + function($article) use ($disk, $baseDir) { + $disk->put("$baseDir/$article->title.md", $article->body); + } + ); + } + + if (!$disk->exists($zipFile)) { + + ($zip = new ZipArchive())->open(storage_path("app/$zipFile"), ZipArchive::CREATE); + + collect($disk->files($baseDir))->each(function($item) use ($zip, $disk) { + $zip->addFile(storage_path("app/$item"), basename($item)); + }); + + $zip->close(); + } + + return response()->download(storage_path("app/$zipFile")); + } } diff --git a/app/Policies/UserPolicy.php b/app/Policies/UserPolicy.php index 4f35c58d..497e8faa 100644 --- a/app/Policies/UserPolicy.php +++ b/app/Policies/UserPolicy.php @@ -18,4 +18,9 @@ public function delete(User $currentUser, User $user) { return $currentUser->may('manage_users') || $currentUser->id == $user->id; } + + public function download(User $currentUser, User $user) + { + return $currentUser->id == $user->id; + } } diff --git a/resources/lang/zh-CN/phphub.php b/resources/lang/zh-CN/phphub.php index 2ccaff63..a554e547 100644 --- a/resources/lang/zh-CN/phphub.php +++ b/resources/lang/zh-CN/phphub.php @@ -222,4 +222,5 @@ 'Sorry, this socialite account has been registed.' => '绑定失败:你的 :driver 账号已被其他用户使用. T_T', 'Bind Successfully!' => '绑定成功!以后可以使用你的 :driver 账号登录 Laravel China 了 ^_^', 'Actived' => '最近访问', + 'Download Article' => '下载文章', ]; diff --git a/resources/views/users/partials/basicinfo.blade.php b/resources/views/users/partials/basicinfo.blade.php index 676eb62a..10bee5b0 100644 --- a/resources/views/users/partials/basicinfo.blade.php +++ b/resources/views/users/partials/basicinfo.blade.php @@ -145,6 +145,13 @@ @endif + @if ($currentUser && ($currentUser->id == $user->id)) +
+ + {{ lang('Download Article') }} + + @endif + @if(Auth::check() && $currentUser->id != $user->id) diff --git a/routes/web.php b/routes/web.php index eff7e14a..52162dae 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,6 +21,7 @@ Route::get('/users/{id}/votes', 'UsersController@votes')->name('users.votes'); Route::get('/users/{id}/following', 'UsersController@following')->name('users.following'); Route::get('/users/{id}/followers', 'UsersController@followers')->name('users.followers'); +Route::get('/users/{id}/downloads', 'UsersController@downloads')->name('users.downloads'); Route::get('/users/{id}/refresh_cache', 'UsersController@refreshCache')->name('users.refresh_cache'); Route::get('/users/{id}/access_tokens', 'UsersController@accessTokens')->name('users.access_tokens'); From f7ec838fcb54c71bff8a8b24e4675b5ab4b90119 Mon Sep 17 00:00:00 2001 From: Seaony Date: Mon, 1 Jan 2018 16:26:52 +0800 Subject: [PATCH 2/2] update the typesetting --- app/Http/Controllers/UsersController.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index e7d2be59..d5f0432b 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -136,12 +136,11 @@ public function accessTokens($id) return redirect(route('users.show', $id)); } $user = User::findOrFail($id); + $sessions = OAuthSession::where([ - 'owner_type' => 'user', - 'owner_id' => Auth::id(), - ]) - ->with('token') - ->lists('id') ? : []; + 'owner_type' => 'user', + 'owner_id' => Auth::id(), + ])->with('token')->lists('id') ? : []; $tokens = AccessToken::whereIn('session_id', $sessions)->get(); @@ -338,10 +337,10 @@ public function downloads(Request $request, $id) Topic::whose($user->id) ->onlyArticle() ->withoutDraft() - ->get(['title', 'body']) + ->get(['title', 'body_original']) ->each( function($article) use ($disk, $baseDir) { - $disk->put("$baseDir/$article->title.md", $article->body); + $disk->put("$baseDir/$article->title.md", $article->body_original); } ); }