From c9d5e6d628bac0100ac8da4135e056f68c56925d Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 21 Sep 2016 14:18:58 +0100 Subject: [PATCH 1/2] Create necessary directories first to save the HTML --- app/Jobs/DownloadWebMention.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app/Jobs/DownloadWebMention.php b/app/Jobs/DownloadWebMention.php index d9ee71ab..0c9ae64b 100644 --- a/app/Jobs/DownloadWebMention.php +++ b/app/Jobs/DownloadWebMention.php @@ -36,25 +36,35 @@ class DownloadWebMention implements ShouldQueue */ public function handle(Client $guzzle) { - $response = $guzzle->request('GET', $source); + $response = $guzzle->request('GET', $this->source); //4XX and 5XX responses should get Guzzle to throw an exception, //Laravel should catch and retry these automatically. if ($response->getStatusCode() == '200') { - $filesystem = \Illuminate\FileSystem\FileSystem(); - $filename = storage_path() . '/HTML/' . $this->createFilenameFromURL($source); + $filesystem = new \Illuminate\FileSystem\FileSystem(); + $filename = storage_path() . '/HTML/' . $this->createFilenameFromURL($this->source); //backup file first $filenameBackup = $filename . '.' . date('Y-m-d') . '.backup'; if ($filesystem->exists($filename)) { $filesystem->copy($filename, $filenameBackup); } + //check if base directory exists + if (!$filesystem->exists($filesystem->dirname($filename))) { + $filesystem->makeDirectory( + $filesystem->dirname($filename), + 0755, //mode + true //recursive + ); + } //save new HTML $filesystem->put( $filename, (string) $response->getBody() ); //remove backup if the same - if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) { - $filesystem->delete($filenameBackup); + if ($filesystem->exists($filenameBackup)) { + if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) { + $filesystem->delete($filenameBackup); + } } } } @@ -68,11 +78,11 @@ class DownloadWebMention implements ShouldQueue */ private function createFilenameFromURL($url) { - $url = str_replace(['https://', 'http://'], ['https/', 'http/'], $url); - if (substr($url, -1) == '/') { - $url = $url . 'index.html'; + $filepath = str_replace(['https://', 'http://'], ['https/', 'http/'], $url); + if (substr($filepath, -1) == '/') { + $filepath .= 'index.html'; } - return $url; + return $filepath; } } From 7e1bf5d91ff62fe716f005c6e5c1eef426baead2 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 21 Sep 2016 14:20:57 +0100 Subject: [PATCH 2/2] Bump version, add fix --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index 4fe132f6..a029bf1e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version 0.0.11.7 (2016-09-21) + - Fix: need to create necessary directories first + ## Version 0.0.11.6 (2016-09-20) - Fix: save webmention HTML to correct location