From a3841855e46e0af2d313cdb07b02866e2b682aeb Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 27 Sep 2018 13:54:59 +0200 Subject: [PATCH] Fix for #1737 --- app/Helpers/Attachments/AttachmentHelper.php | 7 +++++++ app/Repositories/ImportJob/ImportJobRepository.php | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index dab8e627ac..637a74045f 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Helpers\Attachments; use Crypt; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Attachment; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Database\Eloquent\Model; @@ -245,6 +246,7 @@ class AttachmentHelper implements AttachmentHelperInterface * * @return Attachment|null * @throws \Illuminate\Contracts\Encryption\EncryptException + * @throws FireflyException */ protected function processFile(UploadedFile $file, Model $model): ?Attachment { @@ -266,6 +268,11 @@ class AttachmentHelper implements AttachmentHelperInterface $fileObject = $file->openFile('r'); $fileObject->rewind(); + + if(0 === $file->getSize()) { + throw new FireflyException('Cannot upload empty or non-existent file.'); + } + $content = $fileObject->fread($file->getSize()); $encrypted = Crypt::encrypt($content); Log::debug(sprintf('Full file length is %d and upload size is %d.', \strlen($content), $file->getSize())); diff --git a/app/Repositories/ImportJob/ImportJobRepository.php b/app/Repositories/ImportJob/ImportJobRepository.php index b3bac14750..a6d6f54e46 100644 --- a/app/Repositories/ImportJob/ImportJobRepository.php +++ b/app/Repositories/ImportJob/ImportJobRepository.php @@ -342,6 +342,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface * @param UploadedFile $file * * @return MessageBag + * @throws FireflyException */ public function storeFileUpload(ImportJob $job, string $name, UploadedFile $file): MessageBag { @@ -374,6 +375,12 @@ class ImportJobRepository implements ImportJobRepositoryInterface $attachment->save(); $fileObject = $file->openFile('r'); $fileObject->rewind(); + + + if(0 === $file->getSize()) { + throw new FireflyException('Cannot upload empty or non-existent file.'); + } + $content = $fileObject->fread($file->getSize()); $encrypted = Crypt::encrypt($content); $this->uploadDisk->put($attachment->fileName(), $encrypted);