diff --git a/app/Livewire/App/Pfp.php b/app/Livewire/App/Pfp.php new file mode 100644 index 0000000..01e23b1 --- /dev/null +++ b/app/Livewire/App/Pfp.php @@ -0,0 +1,20 @@ + $this->user->getAvatarBase64() + ]); + } +} diff --git a/app/Livewire/Pages/Profile.php b/app/Livewire/Pages/Profile.php index 37e93f0..4401ce3 100644 --- a/app/Livewire/Pages/Profile.php +++ b/app/Livewire/Pages/Profile.php @@ -3,14 +3,38 @@ namespace App\Livewire\Pages; use App\Models\User; +use Illuminate\Support\Facades\Auth; +use Livewire\Attributes\Validate; use Livewire\Component; +use Livewire\WithFileUploads; class Profile extends Component { + use WithFileUploads; + public User $user; + #[Validate('image|max:10240')] + public $avatar = null; + public function render() { return view('livewire.pages.profile')->title($this->user->name); } + + public function updated($name, $value) + { + if (Auth::id() != $this->user->id) + { + abort(401); + } + + $this->validateOnly($name); + + $this->user->avatarExt = $this->avatar->getClientOriginalExtension(); + $this->user->save(); + + $this->avatar->storeAs('avatars', $this->user->id); + return $this->redirect("/profiles/{$this->user->id}"); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 647af6d..529c71c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Storage; use Laravel\Sanctum\HasApiTokens; use MongoDB\Laravel\Auth\User as Authenticatable; use MongoDB\Laravel\Relations\HasMany; @@ -21,6 +22,7 @@ class User extends Authenticatable implements Searchable protected $fillable = [ 'name', 'email', + 'avatarExt', ]; protected $hidden = [ @@ -55,4 +57,12 @@ class User extends Authenticatable implements Searchable $url ); } + + public function getAvatarBase64(): string + { + $ext = $this->avatarExt; + $file = Storage::get("avatars/$this->id"); + $data = base64_encode($file); + return "data:image/$ext;base64,$data"; + } } diff --git a/resources/views/livewire/app/data-card.blade.php b/resources/views/livewire/app/data-card.blade.php index 2a07241..cb4deb3 100644 --- a/resources/views/livewire/app/data-card.blade.php +++ b/resources/views/livewire/app/data-card.blade.php @@ -5,7 +5,7 @@