From 2694ce414878990b80d56c0a2caa2e0847344b96 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 30 Jul 2017 08:22:39 +0200 Subject: [PATCH] Decryption routine for #671 --- app/Console/Commands/DecryptAttachment.php | 102 ++++++++++++++++++ app/Console/Kernel.php | 2 + .../Attachment/AttachmentRepository.php | 30 ++++++ .../AttachmentRepositoryInterface.php | 14 +++ 4 files changed, 148 insertions(+) create mode 100644 app/Console/Commands/DecryptAttachment.php diff --git a/app/Console/Commands/DecryptAttachment.php b/app/Console/Commands/DecryptAttachment.php new file mode 100644 index 0000000000..ed2fdeb7fa --- /dev/null +++ b/app/Console/Commands/DecryptAttachment.php @@ -0,0 +1,102 @@ +argument('id')); + $attachment = $repository->findWithoutUser($attachmentId); + $attachmentName = trim($this->argument('name')); + $storagePath = realpath(trim($this->argument('directory'))); + if (is_null($attachment->id)) { + $this->error(sprintf('No attachment with id #%d', $attachmentId)); + Log::error(sprintf('DecryptAttachment: No attachment with id #%d', $attachmentId)); + + return; + } + + if ($attachmentName !== $attachment->filename) { + $this->error('File name does not match.'); + Log::error('DecryptAttachment: File name does not match.'); + + return; + } + + if (!is_dir($storagePath)) { + $this->error(sprintf('Path "%s" is not a directory.', $storagePath)); + Log::error(sprintf('DecryptAttachment: Path "%s" is not a directory.', $storagePath)); + + return; + } + + if (!is_writable($storagePath)) { + $this->error(sprintf('Path "%s" is not writable.', $storagePath)); + Log::error(sprintf('DecryptAttachment: Path "%s" is not writable.', $storagePath)); + + return; + } + + $fullPath = $storagePath . DIRECTORY_SEPARATOR . $attachment->filename; + $content = $repository->getContent($attachment); + $this->line(sprintf('Going to write content for attachment #%d into file "%s"', $attachment->id, $fullPath)); + $result = file_put_contents($fullPath, $content); + if ($result === false) { + $this->error('Could not write to file.'); + + return; + } + $this->info(sprintf('%d bytes written. Exiting now..', $result)); + + return; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ea6b8598ba..bd85ef2c8e 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace FireflyIII\Console; use FireflyIII\Console\Commands\CreateImport; +use FireflyIII\Console\Commands\DecryptAttachment; use FireflyIII\Console\Commands\EncryptFile; use FireflyIII\Console\Commands\Import; use FireflyIII\Console\Commands\ScanAttachments; @@ -63,6 +64,7 @@ class Kernel extends ConsoleKernel ScanAttachments::class, UpgradeDatabase::class, UseEncryption::class, + DecryptAttachment::class, ]; /** diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php index 91a3dcbd73..7e7561c8d6 100644 --- a/app/Repositories/Attachment/AttachmentRepository.php +++ b/app/Repositories/Attachment/AttachmentRepository.php @@ -62,6 +62,36 @@ class AttachmentRepository implements AttachmentRepositoryInterface return $disk->exists($attachment->fileName()); } + /** + * @param int $id + * + * @return Attachment + */ + public function find(int $id): Attachment + { + $attachment = $this->user->attachments()->find($id); + if (is_null($attachment)) { + return new Attachment; + } + + return $attachment; + } + + /** + * @param int $id + * + * @return Attachment + */ + public function findWithoutUser(int $id): Attachment + { + $attachment = Attachment::find($id); + if (is_null($attachment)) { + return new Attachment; + } + + return $attachment; + } + /** * @return Collection */ diff --git a/app/Repositories/Attachment/AttachmentRepositoryInterface.php b/app/Repositories/Attachment/AttachmentRepositoryInterface.php index caeb3af8b8..6c76e4ebfd 100644 --- a/app/Repositories/Attachment/AttachmentRepositoryInterface.php +++ b/app/Repositories/Attachment/AttachmentRepositoryInterface.php @@ -40,6 +40,20 @@ interface AttachmentRepositoryInterface */ public function exists(Attachment $attachment): bool; + /** + * @param int $id + * + * @return Attachment + */ + public function find(int $id): Attachment; + + /** + * @param int $id + * + * @return Attachment + */ + public function findWithoutUser(int $id):Attachment; + /** * @return Collection */