1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace App\Jobs; |
12
|
|
|
|
13
|
|
|
use App\Models\User; |
14
|
|
|
use Illuminate\Bus\Queueable; |
15
|
|
|
use Intervention\Image\ImageManager; |
16
|
|
|
use Illuminate\Queue\SerializesModels; |
17
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Service\ImageUploader\AvatarUploader; |
20
|
|
|
use Service\ImageUploader\Gates\FileSize; |
21
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
22
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
23
|
|
|
use Illuminate\Contracts\Filesystem\Factory as Storage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class UploadAvatarProcess. |
27
|
|
|
*/ |
28
|
|
|
class UploadAvatarProcess implements ShouldQueue |
29
|
|
|
{ |
30
|
|
|
use Queueable; |
31
|
|
|
use Dispatchable; |
32
|
|
|
use SerializesModels; |
33
|
|
|
use InteractsWithQueue; |
34
|
|
|
|
35
|
|
|
private const MAX_AVATAR_FILE_SIZE = 1000 * 1000 * 20; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var User |
39
|
|
|
*/ |
40
|
|
|
private $user; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* UploadAvatarProcess constructor. |
44
|
|
|
* |
45
|
|
|
* @param User $user |
46
|
|
|
*/ |
47
|
|
|
public function __construct(User $user) |
48
|
|
|
{ |
49
|
|
|
$this->user = $user; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ImageManager $manager |
54
|
|
|
* @param Storage $fs |
55
|
|
|
* @param LoggerInterface $logger |
56
|
|
|
*/ |
57
|
|
|
public function handle(ImageManager $manager, Storage $fs, LoggerInterface $logger): void |
58
|
|
|
{ |
59
|
|
|
$fulfilled = function (User $user) use ($logger) { |
60
|
|
|
$logger->info('Queue: Update avatar for user ' . $user->name); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$rejected = function (\Throwable $error) use ($logger) { |
64
|
|
|
$logger->error($error); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
(new AvatarUploader($this->user, $manager)) |
68
|
|
|
->satisfy(new FileSize(self::MAX_AVATAR_FILE_SIZE)) |
69
|
|
|
->withSize(64, 64) |
70
|
|
|
->upload($fs->disk('avatars')) |
71
|
|
|
->then($fulfilled, $rejected); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public static function avatarPathname(): string |
78
|
|
|
{ |
79
|
|
|
return resource_path('images/avatars/' . random_int(1, 4)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|