2016-09-15 15:54:57 +01:00
|
|
|
<?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)
|
|
|
|
{
|
2016-09-21 14:18:58 +01:00
|
|
|
$response = $guzzle->request('GET', $this->source);
|
2016-09-17 21:03:53 +01:00
|
|
|
//4XX and 5XX responses should get Guzzle to throw an exception,
|
|
|
|
//Laravel should catch and retry these automatically.
|
2016-09-15 15:54:57 +01:00
|
|
|
if ($response->getStatusCode() == '200') {
|
2016-09-21 14:18:58 +01:00
|
|
|
$filesystem = new \Illuminate\FileSystem\FileSystem();
|
|
|
|
$filename = storage_path() . '/HTML/' . $this->createFilenameFromURL($this->source);
|
2016-09-19 17:18:24 +01:00
|
|
|
//backup file first
|
2016-09-19 17:25:01 +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
|
|
|
}
|
2016-09-21 14:18:58 +01: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),
|
|
|
|
0755, //mode
|
|
|
|
true //recursive
|
|
|
|
);
|
|
|
|
}
|
2016-09-19 17:25:01 +01: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()
|
|
|
|
);
|
2016-09-19 17:25:01 +01:00
|
|
|
//remove backup if the same
|
2016-09-21 14:18:58 +01:00
|
|
|
if ($filesystem->exists($filenameBackup)) {
|
|
|
|
if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) {
|
|
|
|
$filesystem->delete($filenameBackup);
|
|
|
|
}
|
2016-09-19 17:25:01 +01:00
|
|
|
}
|
2016-09-15 15:54:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2016-09-21 14:18:58 +01:00
|
|
|
$filepath = str_replace(['https://', 'http://'], ['https/', 'http/'], $url);
|
|
|
|
if (substr($filepath, -1) == '/') {
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|