diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php index 126a689b3d..8cf513f267 100644 --- a/app/Http/Controllers/AttachmentController.php +++ b/app/Http/Controllers/AttachmentController.php @@ -95,16 +95,12 @@ class AttachmentController extends Controller * @throws FireflyException * */ - public function download(Attachment $attachment) + public function download(AttachmentRepositoryInterface $repository, Attachment $attachment) { - // create a disk. - $disk = Storage::disk('upload'); - $file = $attachment->fileName(); - - if ($disk->exists($file)) { - + if ($repository->exists($attachment)) { + $content = $repository->getContent($attachment); $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\')); - $content = Crypt::decrypt($disk->get($file)); + Log::debug('Send file to user', ['file' => $quoted, 'size' => strlen($content)]); @@ -118,8 +114,8 @@ class AttachmentController extends Controller ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0') ->header('Pragma', 'public') ->header('Content-Length', strlen($content)); - } + throw new FireflyException('Could not find the indicated attachment. The file is no longer there.'); } diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php index 065b5c0167..b1e7bbbe78 100644 --- a/app/Repositories/Attachment/AttachmentRepository.php +++ b/app/Repositories/Attachment/AttachmentRepository.php @@ -18,6 +18,7 @@ use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Models\Attachment; use FireflyIII\User; use Illuminate\Support\Collection; +use Storage; /** * Class AttachmentRepository @@ -56,6 +57,19 @@ class AttachmentRepository implements AttachmentRepositoryInterface return true; } + /** + * @param Attachment $attachment + * + * @return bool + */ + public function exists(Attachment $attachment): bool + { + /** @var Storage $disk */ + $disk = Storage::disk('upload'); + + return $disk->exists($attachment->fileName()); + } + /** * @return Collection */ @@ -82,6 +96,26 @@ class AttachmentRepository implements AttachmentRepositoryInterface return $query; } + /** + * @param Attachment $attachment + * + * @return string + */ + public function getContent(Attachment $attachment): string + { + // create a disk. + $disk = Storage::disk('upload'); + $file = $attachment->fileName(); + + if ($disk->exists($file)) { + $content = Crypt::decrypt($disk->get($file)); + + return $content; + } + + return ''; + } + /** * @param Attachment $attachment * @param array $data diff --git a/app/Repositories/Attachment/AttachmentRepositoryInterface.php b/app/Repositories/Attachment/AttachmentRepositoryInterface.php index 912dc67190..733e3bd59f 100644 --- a/app/Repositories/Attachment/AttachmentRepositoryInterface.php +++ b/app/Repositories/Attachment/AttachmentRepositoryInterface.php @@ -37,6 +37,20 @@ interface AttachmentRepositoryInterface */ public function get(): Collection; + /** + * @param Attachment $attachment + * + * @return bool + */ + public function exists(Attachment $attachment): bool; + + /** + * @param Attachment $attachment + * + * @return string + */ + public function getContent(Attachment $attachment): string; + /** * @param Carbon $start * @param Carbon $end diff --git a/tests/acceptance/Controllers/AttachmentControllerTest.php b/tests/acceptance/Controllers/AttachmentControllerTest.php index 6f96cf8db5..be401ef04d 100644 --- a/tests/acceptance/Controllers/AttachmentControllerTest.php +++ b/tests/acceptance/Controllers/AttachmentControllerTest.php @@ -58,6 +58,10 @@ class AttachmentControllerTest extends TestCase */ public function testDownload() { + $repository = $this->mock(AttachmentRepositoryInterface::class); + $repository->shouldReceive('exists')->once()->andReturn(true); + $repository->shouldReceive('getContent')->once()->andReturn('This is attachment number one.'); + $this->be($this->user()); $this->call('GET', route('attachments.download', [1])); $this->assertResponseStatus(200);