From f871e29bdbf8374ecf0016ea8e5cf9cbbd4048a7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 21 Sep 2016 19:16:47 +0200 Subject: [PATCH] Quick fix for broken attachments. --- app/Console/Commands/ScanAttachments.php | 99 ++++++++++++++++++++++++ app/Console/Kernel.php | 2 + 2 files changed, 101 insertions(+) create mode 100644 app/Console/Commands/ScanAttachments.php diff --git a/app/Console/Commands/ScanAttachments.php b/app/Console/Commands/ScanAttachments.php new file mode 100644 index 0000000000..1f9293335f --- /dev/null +++ b/app/Console/Commands/ScanAttachments.php @@ -0,0 +1,99 @@ +fileName(); + + // try to grab file content: + try { + $content = $disk->get($fileName); + } catch (FileNotFoundException $e) { + $this->error(sprintf('Could not find data for attachment #%d', $attachment->id)); + continue; + } + // try to decrypt content. + try { + $decrypted = Crypt::decrypt($content); + } catch (DecryptException $e) { + $this->error(sprintf('Could not decrypt data of attachment #%d', $attachment->id)); + continue; + } + + // make temp file: + $tmpfname = tempnam(sys_get_temp_dir(), 'FireflyIII'); + + // store content in temp file: + file_put_contents($tmpfname, $decrypted); + + // get md5 and mime + $md5 = md5_file($tmpfname); + $mime = mime_content_type($tmpfname); + + // update attachment: + $attachment->md5 = $md5; + $attachment->mime = $mime; + $attachment->save(); + + + $this->line(sprintf('Fixed attachment #%d', $attachment->id)); + + // find file: + + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 11d8974b70..363eccabf5 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,6 +13,7 @@ namespace FireflyIII\Console; use FireflyIII\Console\Commands\EncryptFile; use FireflyIII\Console\Commands\Import; +use FireflyIII\Console\Commands\ScanAttachments; use FireflyIII\Console\Commands\UpgradeFireflyInstructions; use FireflyIII\Console\Commands\VerifyDatabase; use Illuminate\Console\Scheduling\Schedule; @@ -55,6 +56,7 @@ class Kernel extends ConsoleKernel VerifyDatabase::class, Import::class, EncryptFile::class, + ScanAttachments::class, ];