2016-08-03 16:08:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-08-03 16:08:30 +01:00
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
2016-08-03 16:08:30 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.
The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.
Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
|
use Illuminate\Http\Client\RequestException;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-09-13 17:56:29 +01:00
|
|
|
use Illuminate\Support\Arr;
|
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.
The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.
Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2016-08-03 16:08:30 +01:00
|
|
|
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
|
|
|
{
|
2019-10-27 19:31:33 +00:00
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
|
|
|
|
use SerializesModels;
|
2016-08-03 16:08:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function __construct(
|
|
|
|
|
protected array $microformats
|
2024-07-13 10:55:19 +01:00
|
|
|
) {}
|
2016-08-03 16:08:30 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function handle(Authorship $authorship): void
|
2016-08-03 16:08:30 +01:00
|
|
|
{
|
|
|
|
|
try {
|
2016-10-20 15:57:11 +01:00
|
|
|
$author = $authorship->findAuthor($this->microformats);
|
2021-03-17 18:38:18 +00:00
|
|
|
} catch (AuthorshipParserException) {
|
2023-02-18 09:34:57 +00:00
|
|
|
return;
|
2016-08-03 16:08:30 +01:00
|
|
|
}
|
2021-03-17 18:38:18 +00:00
|
|
|
|
2020-09-13 17:56:29 +01:00
|
|
|
$photo = Arr::get($author, 'properties.photo.0');
|
|
|
|
|
$home = Arr::get($author, 'properties.url.0');
|
2021-03-17 18:38:18 +00:00
|
|
|
|
2023-06-17 19:48:27 +01:00
|
|
|
if (is_array($photo) && array_key_exists('value', $photo)) {
|
|
|
|
|
$photo = $photo['value'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_array($home)) {
|
|
|
|
|
$home = array_shift($home);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-01 15:00:41 +00:00
|
|
|
// dont save pbs.twimg.com links
|
2019-10-27 19:31:33 +00:00
|
|
|
if (
|
2020-09-13 17:56:29 +01:00
|
|
|
$photo
|
2023-02-18 09:34:57 +00:00
|
|
|
&& parse_url($photo, PHP_URL_HOST) !== 'pbs.twimg.com'
|
|
|
|
|
&& parse_url($photo, PHP_URL_HOST) !== 'twitter.com'
|
2019-10-27 19:31:33 +00:00
|
|
|
) {
|
2016-08-03 16:08:30 +01:00
|
|
|
try {
|
Migrate outbound HTTP calls from Guzzle to the Http facade
Replaces every raw GuzzleHttp\Client usage (Nominatim, web.archive.org,
webmention fetch/discover/send, Bridgy syndication, IndieAuth client_id
lookup, contact avatar/h-card fetch, profile image download, and the
CloudConvert screenshot pipeline) with Illuminate\Support\Facades\Http,
and enables Http::preventStrayRequests() globally in tests so any
un-faked outbound call now fails loudly instead of silently hitting the
network.
The CloudConvert retry-until-finished middleware in AppServiceProvider
is replaced by a plain polling loop in SaveScreenshot, which also fixes
a latent bug where the old middleware decoded a response object instead
of its body. Bridgy syndication jobs keep their tries=1/no-retry
semantics unchanged to avoid duplicate publishes. Guzzle's PSR-7 helpers
(Header, UriResolver, Utils) stay in SendWebMentions since Http has no
equivalent for them.
Every affected test file's Guzzle MockHandler/HandlerStack boilerplate
is replaced with Http::fake()/Http::sequence().
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P8j7cJhCiYDsGUgB7obKNQ
2026-07-26 09:12:01 +01:00
|
|
|
$response = Http::throw()->get($photo);
|
|
|
|
|
$image = $response->body();
|
|
|
|
|
} catch (ConnectionException|RequestException) {
|
2019-10-27 19:31:33 +00:00
|
|
|
// we are opening and reading the default image so that
|
2026-04-07 09:01:19 +01:00
|
|
|
$default = public_path().'/assets/profile-images/default-image';
|
2016-08-03 16:08:30 +01:00
|
|
|
$handle = fopen($default, 'rb');
|
|
|
|
|
$image = fread($handle, filesize($default));
|
|
|
|
|
fclose($handle);
|
|
|
|
|
}
|
2021-03-17 18:38:18 +00:00
|
|
|
|
2026-04-07 09:01:19 +01:00
|
|
|
$path = public_path().'/assets/profile-images/'.parse_url($home, PHP_URL_HOST).'/image';
|
2016-08-03 16:08:30 +01:00
|
|
|
$parts = explode('/', $path);
|
|
|
|
|
$name = array_pop($parts);
|
|
|
|
|
$dir = implode('/', $parts);
|
2023-02-18 09:53:57 +00:00
|
|
|
if (! is_dir($dir) && ! mkdir($dir, 0755, true) && ! is_dir($dir)) {
|
2023-02-18 09:34:57 +00:00
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
|
2016-08-03 16:08:30 +01:00
|
|
|
}
|
|
|
|
|
file_put_contents("$dir/$name", $image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|