RemoteAvatarFetchFromUrl.php 1.9 KB

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