From 6804cc3776beb73dc02583736c9a80b1501415e4 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 19 Sep 2016 17:18:24 +0100 Subject: [PATCH 1/3] Backup a file before saving the new HTML contents --- app/Jobs/DownloadWebMention.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Jobs/DownloadWebMention.php b/app/Jobs/DownloadWebMention.php index 592b41d3..71894270 100644 --- a/app/Jobs/DownloadWebMention.php +++ b/app/Jobs/DownloadWebMention.php @@ -41,8 +41,13 @@ class DownloadWebMention implements ShouldQueue //Laravel should catch and retry these automatically. if ($response->getStatusCode() == '200') { $filesystem = \Illuminate\FileSystem\FileSystem(); + $filename = $this->createFilenameFromURL($source); + //backup file first + if ($filesystem->exists($filename)) { + $filesystem->copy($filename, $filename . '.' . date('Y-m-d') . '.backup'); + } $filesystem->put( - $this->createFilenameFromURL($source), + $filename, (string) $response->getBody() ); } From 54079e407c69848fa10a46761692f55737795c22 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 19 Sep 2016 17:19:04 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Don=E2=80=99t=20parse=20backups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ParseCachedWebMentions.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/ParseCachedWebMentions.php b/app/Console/Commands/ParseCachedWebMentions.php index d271dd15..f375f213 100644 --- a/app/Console/Commands/ParseCachedWebMentions.php +++ b/app/Console/Commands/ParseCachedWebMentions.php @@ -41,13 +41,15 @@ class ParseCachedWebMentions extends Command { $HTMLfiles = $filesystem->allFiles(storage_path() . '/HTML'); foreach ($HTMLfiles as $file) { - $filepath = $file->getPathname(); - $html = $filesystem->get($filepath); - $url = $this->URLFromFilename($filepath); - $microformats = \Mf2\parse($html, $url); - $webmention = WebMention::where('source', $url)->firstOrFail(); - $webmention->mf2 = json_encode($microformats); - $webmention->save(); + if ($file->getExtension() != 'backup') { //we don’t want to parse.backup files + $filepath = $file->getPathname(); + $html = $filesystem->get($filepath); + $url = $this->URLFromFilename($filepath); + $microformats = \Mf2\parse($html, $url); + $webmention = WebMention::where('source', $url)->firstOrFail(); + $webmention->mf2 = json_encode($microformats); + $webmention->save(); + } } } From cdfb437dd345527957a9699c7c2beb0aaeca1764 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Mon, 19 Sep 2016 17:25:01 +0100 Subject: [PATCH 3/3] Delete unnecesary backups --- app/Jobs/DownloadWebMention.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Jobs/DownloadWebMention.php b/app/Jobs/DownloadWebMention.php index 71894270..63d8d307 100644 --- a/app/Jobs/DownloadWebMention.php +++ b/app/Jobs/DownloadWebMention.php @@ -43,13 +43,19 @@ class DownloadWebMention implements ShouldQueue $filesystem = \Illuminate\FileSystem\FileSystem(); $filename = $this->createFilenameFromURL($source); //backup file first + $filenameBackup = $filename . '.' . date('Y-m-d') . '.backup'; if ($filesystem->exists($filename)) { - $filesystem->copy($filename, $filename . '.' . date('Y-m-d') . '.backup'); + $filesystem->copy($filename, $filenameBackup); } + //save new HTML $filesystem->put( $filename, (string) $response->getBody() ); + //remove backup if the same + if ($filesystem->get($filename) == $filesystem->get($filenameBackup)) { + $filesystem->delete($filenameBackup); + } } }