2016-08-03 16:08:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
2016-10-20 18:45:50 +01:00
|
|
|
use GuzzleHtt\Client;
|
2016-09-20 13:13:05 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
2016-08-03 16:08:30 +01:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Jonnybarnes\WebmentionsParser\Authorship;
|
|
|
|
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
|
|
|
|
|
2016-09-20 13:13:05 +01:00
|
|
|
class SaveProfileImage implements ShouldQueue
|
2016-08-03 16:08:30 +01:00
|
|
|
{
|
2016-09-20 13:13:05 +01:00
|
|
|
use InteractsWithQueue, Queueable, SerializesModels;
|
2016-08-03 16:08:30 +01:00
|
|
|
|
|
|
|
protected $microformats;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($microformats)
|
|
|
|
{
|
|
|
|
$this->microformats = $microformats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle(Authorship $authorship)
|
|
|
|
{
|
|
|
|
try {
|
2016-10-20 15:57:11 +01:00
|
|
|
$author = $authorship->findAuthor($this->microformats);
|
2016-08-03 16:08:30 +01:00
|
|
|
} catch (AuthorshipParserException $e) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-20 18:16:45 +01:00
|
|
|
$photo = $author['properties']['photo'][0];
|
|
|
|
$home = $author['properties']['url'][0];
|
2016-08-03 16:08:30 +01:00
|
|
|
//dont save pbs.twimg.com links
|
|
|
|
if (parse_url($photo, PHP_URL_HOST) != 'pbs.twimg.com'
|
|
|
|
&& parse_url($photo, PHP_URL_HOST) != 'twitter.com') {
|
|
|
|
$client = new Client();
|
|
|
|
try {
|
|
|
|
$response = $client->get($photo);
|
|
|
|
$image = $response->getBody(true);
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
// we are openning and reading the default image so that
|
|
|
|
$default = public_path() . '/assets/profile-images/default-image';
|
|
|
|
$handle = fopen($default, 'rb');
|
|
|
|
$image = fread($handle, filesize($default));
|
|
|
|
fclose($handle);
|
|
|
|
}
|
|
|
|
$path = public_path() . '/assets/profile-images/' . parse_url($home, PHP_URL_HOST) . '/image';
|
|
|
|
$parts = explode('/', $path);
|
|
|
|
$name = array_pop($parts);
|
|
|
|
$dir = implode('/', $parts);
|
|
|
|
if (! is_dir($dir)) {
|
|
|
|
mkdir($dir, 0755, true);
|
|
|
|
}
|
|
|
|
file_put_contents("$dir/$name", $image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|