Add job that downloads and saves the HTML of a webmention
This commit is contained in:
parent
bd6af02170
commit
b340d5a076
1 changed files with 65 additions and 0 deletions
65
app/Jobs/DownloadWebMention.php
Normal file
65
app/Jobs/DownloadWebMention.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class DownloadWebMention implements ShouldQueue
|
||||
{
|
||||
use InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* The webmention source URL.
|
||||
*
|
||||
* @var
|
||||
*/
|
||||
protected $source;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(string $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Client $guzzle)
|
||||
{
|
||||
$response = $guzzle->request('GET', $source);
|
||||
if ($response->getStatusCode() == '200') {
|
||||
$filesystem = \Illuminate\FileSystem\FileSystem();
|
||||
$filesystem->put(
|
||||
$this->createFilenameFromURL($source),
|
||||
(string) $response->getBody())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a file path from a URL. This is used when caching the HTML
|
||||
* response.
|
||||
*
|
||||
* @param string The URL
|
||||
* @return string The path name
|
||||
*/
|
||||
private function createFilenameFromURL($url)
|
||||
{
|
||||
$url = str_replace(['https://', 'http://'], ['', ''], $url);
|
||||
if (substr($url, -1) == '/') {
|
||||
$url = $url . 'index.html';
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue