RemoteAvatarFetchFromUrl.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Jobs\AvatarPipeline;
  3. use App\Avatar;
  4. use App\Profile;
  5. use App\Services\AccountService;
  6. use App\Services\MediaStorageService;
  7. use Cache;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. class RemoteAvatarFetchFromUrl implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $profile;
  17. protected $url;
  18. /**
  19. * Delete the job if its models no longer exist.
  20. *
  21. * @var bool
  22. */
  23. public $deleteWhenMissingModels = true;
  24. /**
  25. * The number of times the job may be attempted.
  26. *
  27. * @var int
  28. */
  29. public $tries = 1;
  30. public $timeout = 300;
  31. public $maxExceptions = 1;
  32. /**
  33. * Create a new job instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct(Profile $profile, $url)
  38. {
  39. $this->profile = $profile;
  40. $this->url = $url;
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. $profile = $this->profile;
  50. Cache::forget('avatar:'.$profile->id);
  51. AccountService::del($profile->id);
  52. if ((bool) config_cache('pixelfed.cloud_storage') == false && (bool) config_cache('federation.avatars.store_local') == false) {
  53. return 1;
  54. }
  55. if ($profile->domain == null || $profile->private_key) {
  56. return 1;
  57. }
  58. $avatar = Avatar::whereProfileId($profile->id)->first();
  59. if (! $avatar) {
  60. $avatar = new Avatar;
  61. $avatar->profile_id = $profile->id;
  62. $avatar->is_remote = true;
  63. $avatar->remote_url = $this->url;
  64. $avatar->save();
  65. } else {
  66. $avatar->remote_url = $this->url;
  67. $avatar->is_remote = true;
  68. $avatar->save();
  69. }
  70. MediaStorageService::avatar($avatar, (bool) config_cache('pixelfed.cloud_storage') == false, true);
  71. return 1;
  72. }
  73. }