2021-01-26 05:03:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\AvatarPipeline;
|
|
|
|
|
|
|
|
use App\Avatar;
|
|
|
|
use App\Profile;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use App\Util\ActivityPub\Helpers;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Zttp\Zttp;
|
|
|
|
use App\Http\Controllers\AvatarController;
|
|
|
|
use Storage;
|
|
|
|
use Log;
|
|
|
|
use Illuminate\Http\File;
|
|
|
|
use App\Services\MediaStorageService;
|
|
|
|
use App\Services\ActivityPubFetchService;
|
|
|
|
|
|
|
|
class RemoteAvatarFetch implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
protected $profile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the job if its models no longer exist.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Profile $profile)
|
|
|
|
{
|
|
|
|
$this->profile = $profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$profile = $this->profile;
|
|
|
|
|
2021-12-31 04:05:50 +00:00
|
|
|
if(config_cache('pixelfed.cloud_storage') == false && config_cache('federation.avatars.store_local') == false) {
|
2021-01-30 16:47:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-26 05:03:27 +00:00
|
|
|
if($profile->domain == null || $profile->private_key) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-31 04:05:50 +00:00
|
|
|
$avatar = Avatar::whereProfileId($profile->id)->first();
|
|
|
|
|
|
|
|
if(!$avatar) {
|
|
|
|
$avatar = new Avatar;
|
|
|
|
$avatar->profile_id = $profile->id;
|
|
|
|
$avatar->save();
|
|
|
|
}
|
2021-01-26 05:03:27 +00:00
|
|
|
|
|
|
|
if($avatar->media_path == null && $avatar->remote_url == null) {
|
|
|
|
$avatar->media_path = 'public/avatars/default.jpg';
|
|
|
|
$avatar->is_remote = true;
|
|
|
|
$avatar->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$person = Helpers::fetchFromUrl($profile->remote_url);
|
|
|
|
|
|
|
|
if(!$person || !isset($person['@context'])) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-19 04:45:04 +00:00
|
|
|
if( !isset($person['icon']) ||
|
2021-01-26 05:03:27 +00:00
|
|
|
!isset($person['icon']['type']) ||
|
|
|
|
!isset($person['icon']['url'])
|
|
|
|
) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($person['icon']['type'] !== 'Image') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Helpers::validateUrl($person['icon']['url'])) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$icon = $person['icon'];
|
|
|
|
|
|
|
|
$avatar->remote_url = $icon['url'];
|
|
|
|
$avatar->save();
|
|
|
|
|
2021-12-31 04:05:50 +00:00
|
|
|
if(config_cache('pixelfed.cloud_storage')) {
|
|
|
|
MediaStorageService::avatar($avatar);
|
|
|
|
}
|
2021-01-26 05:03:27 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2021-05-19 04:45:04 +00:00
|
|
|
}
|