-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from pinkary-project/feat/upload-avatar
Feat: Allow users to upload avatar
- Loading branch information
Showing
32 changed files
with
567 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Profile; | ||
|
||
use App\Http\Requests\UpdateUserAvatarRequest; | ||
use App\Jobs\UpdateUserAvatar; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\UploadedFile; | ||
|
||
final readonly class AvatarController | ||
{ | ||
/** | ||
* Handles the verified refresh. | ||
*/ | ||
public function update(UpdateUserAvatarRequest $request): RedirectResponse | ||
{ | ||
$user = type(request()->user())->as(User::class); | ||
|
||
$file = type($request->file('avatar'))->as(UploadedFile::class); | ||
UpdateUserAvatar::dispatchSync($user, $file->getRealPath()); | ||
|
||
return to_route('profile.edit') | ||
->with('flash-message', 'Avatar updated.'); | ||
} | ||
|
||
/** | ||
* Delete the existing avatar. | ||
*/ | ||
public function delete(Request $request): RedirectResponse | ||
{ | ||
$user = type($request->user())->as(User::class); | ||
|
||
UpdateUserAvatar::dispatchSync($user); | ||
|
||
return to_route('profile.edit') | ||
->with('flash-message', 'Avatar deleted.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Contracts\Validation\ValidatorAwareRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Stringable; | ||
|
||
final class UpdateUserAvatarRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, array<int, ValidatorAwareRule|ValidationRule|Stringable|string>> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'avatar' => ['required', 'image', 'mimes:jpg,jpeg,png', 'max:2048'], | ||
]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\User; | ||
use App\Services\Avatar; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Storage; | ||
use Intervention\Image\Drivers; | ||
use Intervention\Image\ImageManager; | ||
use Throwable; | ||
|
||
final class UpdateUserAvatar implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(private User $user, private ?string $file = null) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$disk = Storage::disk('public'); | ||
|
||
if ($this->user->avatar) { | ||
if ($disk->exists(str_replace('storage/', '', $this->user->avatar))) { | ||
$disk->delete(str_replace('storage/', '', $this->user->avatar)); | ||
} | ||
} | ||
|
||
$file = $this->file !== null ? $this->file : (new Avatar($this->user->email))->url(); | ||
|
||
$contents = (string) file_get_contents($file); | ||
|
||
$avatar = 'avatars/'.hash('sha256', random_int(0, PHP_INT_MAX).'@'.$this->user->id).'.png'; | ||
|
||
Storage::disk('public')->put($avatar, $contents, 'public'); | ||
|
||
$this->resizer()->read($disk->path($avatar)) | ||
->resize(200, 200) | ||
->save(); | ||
|
||
$this->user->update([ | ||
'avatar' => "storage/$avatar", | ||
'avatar_updated_at' => now(), | ||
'is_uploaded_avatar' => $this->file !== null, | ||
]); | ||
|
||
$this->ensureFileIsDeleted(); | ||
} | ||
|
||
/** | ||
* Handle a job failure. | ||
*/ | ||
public function failed(?Throwable $exception): void | ||
{ | ||
$this->ensureFileIsDeleted(); | ||
|
||
$this->user->update([ | ||
'avatar' => null, | ||
'avatar_updated_at' => null, | ||
'is_uploaded_avatar' => false, | ||
]); | ||
} | ||
|
||
/** | ||
* Ensure the file is deleted. | ||
*/ | ||
private function ensureFileIsDeleted(): void | ||
{ | ||
if ($this->file !== null) { | ||
File::delete($this->file); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new image resizer. | ||
*/ | ||
private function resizer(): ImageManager | ||
{ | ||
return new ImageManager( | ||
new Drivers\Gd\Driver(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.