Skip to content
This repository has been archived by the owner on May 7, 2019. It is now read-only.

Commit

Permalink
Totally refactor caching so models pretty much handle their own cachi…
Browse files Browse the repository at this point in the history
…ng with no outside interference
  • Loading branch information
TheReverend403 committed Jul 5, 2016
1 parent ebe73b7 commit 2f5d61c
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 151 deletions.
22 changes: 1 addition & 21 deletions app/Http/Controllers/Account/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,19 @@

namespace App\Http\Controllers\Account;

use App\Helpers;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\Upload;
use App\Models\User;
use Auth;
use Cache;
use App\Helpers;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Mail;
use Teapot\StatusCode;

class AccountController extends Controller
{
public function __construct()
{
parent::__construct();
$userUploadCount = Cache::rememberForever('uploads_count:' . Auth::id(), function () {
return Auth::user()->uploads->count();
});

$userUploadTotalSize = Cache::rememberForever('uploads_size:' . Auth::id(), function () {
return Auth::user()->uploads->sum('size');
});

$userStorageQuota = Helpers::formatBytes(Cache::get('uploads_size:' . Auth::id()));
if (config('upste.user_storage_quota') > 0 && !Auth::user()->isPrivilegedUser()) {
$userStorageQuota = sprintf("%s / %s", $userStorageQuota, Helpers::formatBytes(config('upste.user_storage_quota')));
}

view()->share(compact('userUploadCount', 'userUploadTotalSize', 'userStorageQuota'));
}

public function getIndex()
{
return view('account.index');
Expand Down
42 changes: 5 additions & 37 deletions app/Http/Controllers/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
use App\Helpers;
use Illuminate\Mail\Message;
use Mail;
use Storage;
use View;

class AdminController extends Controller
{
public function __construct()
{
parent::__construct();
if (config('upste.require_user_approval')) {
$requestCount = User::whereEnabled(false)->count();
View::share('requestCount', $requestCount);
view()->share('requestCount', $requestCount);
}
}

Expand All @@ -42,18 +39,7 @@ public function getRequests()

public function getUsers()
{
$users = User::whereEnabled(true)->with('uploads')->paginate(Auth::user()->preferences->pagination_items);

foreach ($users as $user) {
Cache::rememberForever('uploads_count:' . $user->id, function () use ($user) {
return $user->uploads->count();
});

Cache::rememberForever('uploads_size:' . $user->id, function () use ($user) {
return $user->uploads->sum('size');
});
}

$users = User::whereEnabled(true)->paginate(Auth::user()->preferences->pagination_items);
return view('admin.users', compact('users'));
}

Expand All @@ -79,17 +65,6 @@ public function postUserDelete(User $user)
return redirect()->back();
}

// Reimplemented here to allow CASCADE to do it's job, as opposed to using $upload->forceDelete()
foreach ($user->uploads as $upload) {
if (Storage::exists("uploads/" . $upload->name)) {
Storage::delete("uploads/" . $upload->name);
}

if (Storage::exists("thumbnails/" . $upload->name)) {
Storage::delete("thumbnails/" . $upload->name);
}
}

$user->forceDelete();
flash()->success(trans('messages.admin.deleted_user', ['name' => $user->name]));

Expand All @@ -108,20 +83,14 @@ public function getUploads(User $user)
{
$allUploads = $user->uploads();
$uploads = $allUploads->orderBy('created_at', 'desc')->paginate(Auth::user()->preferences->pagination_items);
$uploadsTotalCount = Cache::rememberForever('uploads_count:' . $user->id, function () use ($allUploads) {
return $allUploads->count();
});

$uploadsTotalSize = Helpers::formatBytes(Cache::rememberForever('uploads_size:' . $user->id, function () use ($allUploads) {
return $allUploads->sum('size');
}));

return view('admin.uploads', compact('uploads', 'user', 'uploadsTotalCount', 'uploadsTotalSize'));
return view('admin.uploads', compact('uploads', 'user'));
}

public function postUserAccept(User $user)
{
$user->fill(['enabled' => true])->save();
$user->enabled = true;
$user->save();

$loginRoute = route('login');
Mail::queue(['text' => 'emails.user.account_accepted'], compact('user', 'loginRoute'), function (Message $message) use ($user) {
Expand All @@ -130,7 +99,6 @@ public function postUserAccept(User $user)
});

flash()->success(trans('messages.admin.account_accepted', ['name' => $user->name]));
Helpers::invalidateCache();

return redirect()->back();
}
Expand Down
35 changes: 9 additions & 26 deletions app/Http/Controllers/Api/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ public function post(Request $request)
}

if ($uploadedFile->getSize() >= config('upste.upload_limit')) {
return response()->json([trans(
'messages.upload_too_large',
['limit' => Helpers::formatBytes(config('upste.upload_limit'))]
)], StatusCode::REQUEST_ENTITY_TOO_LARGE);
$responseMsg = trans('messages.upload_too_large', ['limit' => Helpers::formatBytes(config('upste.upload_limit'))]);
return response()->json($responseMsg, StatusCode::REQUEST_ENTITY_TOO_LARGE);
}

// If this upload would hit the quota defined in .env, reject it.
if (config('upste.user_storage_quota') > 0 && !Auth::user()->isPrivilegedUser() &&
(Cache::get('uploads_size:' . Auth::id()) + $uploadedFile->getSize()) >= config('upste.user_storage_quota')) {
return response()->json([trans(
'messages.reached_upload_limit',
['limit' => Helpers::formatBytes(config('upste.user_storage_quota'))]
)], StatusCode::FORBIDDEN);
if (config('upste.user_storage_quota') > 0 && !$request->user()->isPrivilegedUser() && ($request->user()->getUploadsSize() + $uploadedFile->getSize()) >= config('upste.user_storage_quota')) {
$responseMsg = trans('messages.reached_upload_limit', ['limit' => Helpers::formatBytes(config('upste.user_storage_quota'))]);
return response()->json($responseMsg, StatusCode::FORBIDDEN);
}

$ext = strtolower($uploadedFile->getClientOriginalExtension());
Expand All @@ -56,10 +51,11 @@ public function post(Request $request)
$originalName = $uploadedFile->getClientOriginalName();

// Check to see if we already have this file for this user.
$existing = Upload::whereOriginalHash($originalHash)->whereUserId(Auth::id())->first();
$existing = Upload::whereOriginalHash($originalHash)->whereUserId($request->user()->id)->first();
if ($existing) {
$result = [
'url' => route('files.get', $existing)
'url' => route('files.get', $existing),
'delete_url' => route('account.uploads.delete', $existing),
];

$existing->original_name = $originalName;
Expand All @@ -74,7 +70,7 @@ public function post(Request $request)
} while (Upload::whereName($newName)->first() || $newName === 'index.php');

$upload = new Upload([
'user_id' => Auth::id(),
'user_id' => $request->user()->id,
'name' => $newName,
'original_name' => $originalName,
'original_hash' => $originalHash
Expand Down Expand Up @@ -129,17 +125,4 @@ public function post(Request $request)

return response()->json($result, StatusCode::CREATED, [], JSON_UNESCAPED_SLASHES);
}

public function get(Request $request)
{
$user = Auth::user();

if (Cache::get('uploads_count:' . $user->id) !== 0) {
$uploads = $user->uploads->slice(0, $request->input('limit', Helpers::PAGINATION_DEFAULT_ITEMS));

return response()->json($uploads, StatusCode::CREATED, [], JSON_UNESCAPED_SLASHES);
}

return response()->json([trans('messages.no_uploads_found')], StatusCode::NOT_FOUND, [], JSON_UNESCAPED_SLASHES);
}
}
1 change: 0 additions & 1 deletion app/Http/Controllers/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class AuthController extends Controller
*/
public function __construct()
{
parent::__construct();
$this->redirectPath = route('index');
}

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class PasswordController extends Controller
*/
public function __construct()
{
parent::__construct();
$this->redirectTo = route('account');
}

Expand All @@ -55,7 +54,7 @@ public function sendResetLinkEmail(Request $request)
$broker = $this->getBroker();

$passwordRoute = route('account.password.email');
view()->composer('emails.user.password_reset', function($view) use ($passwordRoute) {
view()->composer('emails.user.password_reset', function ($view) use ($passwordRoute) {
$view->with(compact('passwordRoute'));
});

Expand Down
20 changes: 0 additions & 20 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,11 @@

namespace App\Http\Controllers;

use Cache;
use DB;
use Helpers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;

public function __construct()
{
$userCount = Cache::rememberForever('users', function () {
return DB::table('users')->where('enabled', true)->count();
});

$uploadCount = Cache::rememberForever('uploads', function () {
return DB::table('uploads')->count();
});

$uploadTotalSize = Cache::rememberForever('uploads_total_size', function () {
return DB::table('uploads')->sum('size');
});

view()->share(compact('userCount', 'uploadCount', 'uploadTotalSize'));
}
}
10 changes: 7 additions & 3 deletions app/Http/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class Helpers
public static function invalidateCache()
{
if (Auth::check()) {
Cache::forget('uploads_count:' . Auth::id());
Cache::forget('uploads_size:' . Auth::id());
Auth::user()->invalidateCache();
}

Cache::forget('users');
Expand Down Expand Up @@ -127,4 +126,9 @@ public static function shouldThumbnail(UploadedFile $file)

return false;
}
}

public static function properize($string)
{
return $string . ends_with($string, 's') ? '\'' : 's';
}
}
Loading

0 comments on commit 2f5d61c

Please sign in to comment.