Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

add feature: Download article #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/Console/Commands/ClearDownloadDatas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class ClearDownloadDatas extends Command
{

protected $signature = 'phphub:clear-download-data';


protected $description = 'Clear user download datas';


public function __construct()
{
parent::__construct();
}

public function handle()
{
Storage::disk('local')->deleteDirectory('zip');
}
}
3 changes: 3 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
}
70 changes: 54 additions & 16 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php namespace App\Http\Controllers;

use ZipArchive;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Topic;
use App\Models\Reply;
use Illuminate\Http\Request;
use Storage;
use Phphub\Github\GithubUserDataReader;
use Cache;
use Auth;
Expand All @@ -20,11 +22,13 @@ class UsersController extends Controller
{
public function __construct()
{
$this->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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -132,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') ?: [];
])->with('token')->lists('id') ? : [];

$tokens = AccessToken::whereIn('session_id', $sessions)->get();

Expand Down Expand Up @@ -216,7 +219,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);
Expand Down Expand Up @@ -320,4 +323,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_original'])
->each(
function($article) use ($disk, $baseDir) {
$disk->put("$baseDir/$article->title.md", $article->body_original);
}
);
}

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"));
}
}
5 changes: 5 additions & 0 deletions app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions resources/lang/zh-CN/phphub.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,5 @@
'Sorry, this socialite account has been registed.' => '绑定失败:你的 :driver 账号已被其他用户使用. T_T',
'Bind Successfully!' => '绑定成功!以后可以使用你的 :driver 账号登录 Laravel China 了 ^_^',
'Actived' => '最近访问',
'Download Article' => '下载文章',
];
7 changes: 7 additions & 0 deletions resources/views/users/partials/basicinfo.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@
</a>
@endif

@if ($currentUser && ($currentUser->id == $user->id))
<br>
<a class="btn btn-info btn-block" target="_blank" href="{{ route('users.downloads', $user->id) }}">
<i class="fa fa-file-archive-o"></i> {{ lang('Download Article') }}
</a>
@endif

@if(Auth::check() && $currentUser->id != $user->id)
<!--{{$isFollowing= $currentUser && $currentUser->isFollowing($user->id) ? true : false}}-->

Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down