From b340d5a07637f40b77acd30d54f7ac97f5eedf95 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Thu, 15 Sep 2016 15:54:57 +0100 Subject: [PATCH] Add job that downloads and saves the HTML of a webmention --- app/Jobs/DownloadWebMention.php | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 app/Jobs/DownloadWebMention.php diff --git a/app/Jobs/DownloadWebMention.php b/app/Jobs/DownloadWebMention.php new file mode 100644 index 00000000..ac7779df --- /dev/null +++ b/app/Jobs/DownloadWebMention.php @@ -0,0 +1,65 @@ +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; + } +}