2016-09-15 15:54:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-09-15 15:54:57 +01:00
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2019-10-27 19:31:33 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2016-09-15 15:54:57 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
2019-10-27 19:31:33 +00:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2016-09-15 15:54:57 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\FileSystem\FileSystem;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2016-09-15 15:54:57 +01:00
|
|
|
|
|
|
|
|
class DownloadWebMention implements ShouldQueue
|
|
|
|
|
{
|
2019-10-27 19:31:33 +00:00
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
|
|
|
|
use SerializesModels;
|
2016-09-15 15:54:57 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function __construct(
|
|
|
|
|
protected string $source
|
2024-07-13 10:55:19 +01:00
|
|
|
) {}
|
2016-09-15 15:54:57 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
2019-10-27 19:31:33 +00:00
|
|
|
* @throws GuzzleException
|
|
|
|
|
* @throws FileNotFoundException
|
2016-09-15 15:54:57 +01:00
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
public function handle(Client $guzzle): void
|
2016-09-15 15:54:57 +01:00
|
|
|
{
|
2016-09-21 14:18:58 +01:00
|
|
|
$response = $guzzle->request('GET', $this->source);
|
2025-03-01 15:00:41 +00:00
|
|
|
// 4XX and 5XX responses should get Guzzle to throw an exception,
|
|
|
|
|
// Laravel should catch and retry these automatically.
|
2023-02-18 09:34:57 +00:00
|
|
|
if ($response->getStatusCode() === 200) {
|
2024-10-25 20:40:52 +01:00
|
|
|
$filesystem = new FileSystem;
|
2026-04-07 09:01:19 +01:00
|
|
|
$filename = storage_path('HTML').'/'.$this->createFilenameFromURL($this->source);
|
2025-03-01 15:00:41 +00:00
|
|
|
// backup file first
|
2026-04-07 09:01:19 +01:00
|
|
|
$filenameBackup = $filename.'.'.date('Y-m-d').'.backup';
|
2016-09-19 17:18:24 +01:00
|
|
|
if ($filesystem->exists($filename)) {
|
2016-09-19 17:25:01 +01:00
|
|
|
$filesystem->copy($filename, $filenameBackup);
|
2016-09-19 17:18:24 +01:00
|
|
|
}
|
2025-03-01 15:00:41 +00:00
|
|
|
// check if base directory exists
|
2016-09-21 14:25:12 +01:00
|
|
|
if (! $filesystem->exists($filesystem->dirname($filename))) {
|
2016-09-21 14:18:58 +01:00
|
|
|
$filesystem->makeDirectory(
|
|
|
|
|
$filesystem->dirname($filename),
|
2025-03-01 15:00:41 +00:00
|
|
|
0755, // mode
|
|
|
|
|
true // recursive
|
2016-09-21 14:18:58 +01:00
|
|
|
);
|
|
|
|
|
}
|
2025-03-01 15:00:41 +00:00
|
|
|
// save new HTML
|
2016-09-15 15:54:57 +01:00
|
|
|
$filesystem->put(
|
2016-09-19 17:18:24 +01:00
|
|
|
$filename,
|
2016-09-17 21:29:32 +01:00
|
|
|
(string) $response->getBody()
|
|
|
|
|
);
|
2025-03-01 15:00:41 +00:00
|
|
|
// remove backup if the same
|
2016-09-21 14:18:58 +01:00
|
|
|
if ($filesystem->exists($filenameBackup)) {
|
2023-02-18 09:34:57 +00:00
|
|
|
if ($filesystem->get($filename) === $filesystem->get($filenameBackup)) {
|
2016-09-21 14:18:58 +01:00
|
|
|
$filesystem->delete($filenameBackup);
|
|
|
|
|
}
|
2016-09-19 17:25:01 +01:00
|
|
|
}
|
2016-09-15 15:54:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-10-27 19:31:33 +00:00
|
|
|
* Create a file path from a URL. This is used when caching the HTML response.
|
2016-09-15 15:54:57 +01:00
|
|
|
*/
|
2023-02-18 09:34:57 +00:00
|
|
|
private function createFilenameFromURL(string $url): string
|
2016-09-15 15:54:57 +01:00
|
|
|
{
|
2016-09-21 14:18:58 +01:00
|
|
|
$filepath = str_replace(['https://', 'http://'], ['https/', 'http/'], $url);
|
2023-02-18 09:34:57 +00:00
|
|
|
if (str_ends_with($filepath, '/')) {
|
2016-09-21 14:18:58 +01:00
|
|
|
$filepath .= 'index.html';
|
2016-09-15 15:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 14:18:58 +01:00
|
|
|
return $filepath;
|
2016-09-15 15:54:57 +01:00
|
|
|
}
|
|
|
|
|
}
|