From f4f7909a0676924d7142f3608c7cbc1764d7c0ce Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 10 Aug 2018 03:10:05 -0600 Subject: [PATCH 1/2] Update web routes --- app/Http/Controllers/AvatarController.php | 85 +++++++++++++++++++++- app/Jobs/AvatarPipeline/AvatarOptimize.php | 70 ++++++++++++++++++ resources/views/settings/avatar.blade.php | 37 +++++++++- routes/web.php | 1 + 4 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 app/Jobs/AvatarPipeline/AvatarOptimize.php diff --git a/app/Http/Controllers/AvatarController.php b/app/Http/Controllers/AvatarController.php index afb893059..c0fd06cf0 100644 --- a/app/Http/Controllers/AvatarController.php +++ b/app/Http/Controllers/AvatarController.php @@ -3,8 +3,91 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Auth, Log, Storage; +use App\Avatar; +use App\Jobs\AvatarPipeline\AvatarOptimize; class AvatarController extends Controller { - // + public function __construct() + { + return $this->middleware('auth'); + } + + public function store(Request $request) + { + $this->validate($request, [ + 'avatar' => 'required|mimes:jpeg,png|max:1000' + ]); + try { + $user = Auth::user(); + $file = $request->file('avatar'); + $path = $this->getPath($user, $file); + $dir = $path['root']; + $name = $path['name']; + $public = $path['storage']; + $currentAvatar = storage_path('app/'.$user->profile->avatar->media_path); + $loc = $request->file('avatar')->storeAs($public, $name); + + $avatar = Avatar::whereProfileId($user->profile->id)->firstOrFail(); + $opath = $avatar->media_path; + $avatar->media_path = "$public/$name"; + $avatar->thumb_path = null; + $avatar->change_count = ++$avatar->change_count; + $avatar->last_processed_at = null; + $avatar->save(); + + AvatarOptimize::dispatch($user->profile, $currentAvatar); + } catch (Exception $e) { + } + return redirect()->back()->with('status', 'Avatar updated successfully. It may take a few minutes to update across the site.'); + } + + public function getPath($user, $file) + { + $basePath = storage_path('app/public/avatars'); + $this->checkDir($basePath); + + $id = $user->profile->id; + $path = $this->buildPath($id); + $dir = storage_path('app/'.$path); + $this->checkDir($dir); + $name = 'avatar.' . $file->guessExtension(); + $res = ['root' => 'storage/app/' . $path, 'name' => $name, 'storage' => $path]; + + return $res; + } + + public function checkDir($path) + { + if(!is_dir($path)) { + mkdir($path); + } + } + + public function buildPath($id) + { + $padded = str_pad($id, 12, 0, STR_PAD_LEFT); + $parts = str_split($padded, 3); + foreach($parts as $k => $part) { + if($k == 0) { + $prefix = storage_path('app/public/avatars/'.$parts[0]); + $this->checkDir($prefix); + } + if($k == 1) { + $prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1]); + $this->checkDir($prefix); + } + if($k == 2) { + $prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2]); + $this->checkDir($prefix); + } + if($k == 3) { + $avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3]; + $prefix = storage_path('app/'.$avatarpath); + $this->checkDir($prefix); + } + } + return $avatarpath; + } } diff --git a/app/Jobs/AvatarPipeline/AvatarOptimize.php b/app/Jobs/AvatarPipeline/AvatarOptimize.php new file mode 100644 index 000000000..749224784 --- /dev/null +++ b/app/Jobs/AvatarPipeline/AvatarOptimize.php @@ -0,0 +1,70 @@ +profile = $profile; + $this->current = $current; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $avatar = $this->profile->avatar; + $file = storage_path("app/$avatar->media_path"); + + try { + $img = Intervention::make($file)->orientate(); + $img->fit(200, 200, function ($constraint) { + $constraint->upsize(); + }); + $quality = config('pixelfed.image_quality'); + $img->save($file, $quality); + + $avatar = Avatar::whereProfileId($this->profile->id)->firstOrFail(); + $avatar->thumb_path = $avatar->media_path; + $avatar->change_count = ++$avatar->change_count; + $avatar->last_processed_at = Carbon::now(); + $avatar->save(); + $this->deleteOldAvatar($avatar->media_path, $this->current); + } catch (Exception $e) { + + } + } + + protected function deleteOldAvatar($new, $current) + { + if(storage_path('app/' . $new) == $current) { + return; + } + if(is_file($current)) { + @unlink($current); + } + } +} diff --git a/resources/views/settings/avatar.blade.php b/resources/views/settings/avatar.blade.php index 569160214..5ed73ef16 100644 --- a/resources/views/settings/avatar.blade.php +++ b/resources/views/settings/avatar.blade.php @@ -6,8 +6,41 @@

Avatar Settings


-
- Coming Soon +
+ +
+

Current Avatar

+ +
+
+
+
Update Avatar
+
+
+ @csrf +
+
+ + +
+ + Max Size: 1 MB. Supported formats: jpeg, png. + +
+
+
+
+
+
+
+
+ {{-- Restore Default Avatar --}} + +
+
+
+ +
@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 9c5cc5a6f..3a9a0eb2b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -85,6 +85,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware('validemail')->group(fu Route::get('home', 'SettingsController@home')->name('settings'); Route::post('home', 'SettingsController@homeUpdate'); Route::get('avatar', 'SettingsController@avatar')->name('settings.avatar'); + Route::post('avatar', 'AvatarController@store'); Route::get('password', 'SettingsController@password')->name('settings.password'); Route::post('password', 'SettingsController@passwordUpdate'); Route::get('email', 'SettingsController@email')->name('settings.email'); From 6a12f59df19d22c932a9374eb0a42613de6140a6 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 10 Aug 2018 03:22:36 -0600 Subject: [PATCH 2/2] Update settings sidebar view --- .../views/settings/partial/sidebar.blade.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/resources/views/settings/partial/sidebar.blade.php b/resources/views/settings/partial/sidebar.blade.php index ab836ac23..c8af3ce21 100644 --- a/resources/views/settings/partial/sidebar.blade.php +++ b/resources/views/settings/partial/sidebar.blade.php @@ -3,11 +3,42 @@ + + {{-- + --}} +{{-- + + + + + + + --}}
\ No newline at end of file