AvatarOptimize.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Jobs\AvatarPipeline;
  3. use Cache;
  4. use App\Avatar;
  5. use App\Profile;
  6. use Carbon\Carbon;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Str;
  13. use Image as Intervention;
  14. use Storage;
  15. class AvatarOptimize implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $profile;
  19. protected $current;
  20. /**
  21. * Delete the job if its models no longer exist.
  22. *
  23. * @var bool
  24. */
  25. public $deleteWhenMissingModels = true;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(Profile $profile, $current)
  32. {
  33. $this->profile = $profile;
  34. $this->current = $current;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. $avatar = $this->profile->avatar;
  44. $file = storage_path("app/$avatar->media_path");
  45. try {
  46. $img = Intervention::make($file)->orientate();
  47. $img->fit(200, 200, function ($constraint) {
  48. $constraint->upsize();
  49. });
  50. $quality = config_cache('pixelfed.image_quality');
  51. $img->save($file, $quality);
  52. $avatar = Avatar::whereProfileId($this->profile->id)->firstOrFail();
  53. $avatar->change_count = ++$avatar->change_count;
  54. $avatar->last_processed_at = Carbon::now();
  55. $avatar->save();
  56. Cache::forget('avatar:' . $avatar->profile_id);
  57. $this->deleteOldAvatar($avatar->media_path, $this->current);
  58. if(config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud')) {
  59. $this->uploadToCloud($avatar);
  60. } else {
  61. $avatar->cdn_url = null;
  62. $avatar->save();
  63. }
  64. } catch (Exception $e) {
  65. }
  66. }
  67. protected function deleteOldAvatar($new, $current)
  68. {
  69. if ( storage_path('app/'.$new) == $current ||
  70. Str::endsWith($current, 'avatars/default.png') ||
  71. Str::endsWith($current, 'avatars/default.jpg'))
  72. {
  73. return;
  74. }
  75. if (is_file($current)) {
  76. @unlink($current);
  77. }
  78. }
  79. protected function uploadToCloud($avatar)
  80. {
  81. $base = 'cache/avatars/' . $avatar->profile_id;
  82. $disk = Storage::disk(config('filesystems.cloud'));
  83. $disk->deleteDirectory($base);
  84. $path = $base . '/' . 'avatar_' . strtolower(Str::random(random_int(3,6))) . $avatar->change_count . '.' . pathinfo($avatar->media_path, PATHINFO_EXTENSION);
  85. $url = $disk->put($path, Storage::get($avatar->media_path));
  86. $avatar->media_path = $path;
  87. $avatar->cdn_url = $disk->url($path);
  88. $avatar->save();
  89. Storage::delete($avatar->media_path);
  90. Cache::forget('avatar:' . $avatar->profile_id);
  91. }
  92. }