1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * AttachmentCollector.php |
||
5 | * Copyright (c) 2017 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III. |
||
8 | * |
||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation, either version 3 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * Firefly III is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License |
||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | namespace FireflyIII\Export\Collector; |
||
24 | |||
25 | use Carbon\Carbon; |
||
26 | use Crypt; |
||
27 | use FireflyIII\Models\Attachment; |
||
28 | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; |
||
29 | use Illuminate\Contracts\Encryption\DecryptException; |
||
30 | use Illuminate\Support\Collection; |
||
31 | use Log; |
||
32 | use Storage; |
||
33 | |||
34 | /** |
||
35 | * Class AttachmentCollector. |
||
36 | */ |
||
37 | class AttachmentCollector extends BasicCollector implements CollectorInterface |
||
38 | { |
||
39 | /** @var Carbon */ |
||
40 | private $end; |
||
41 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
42 | private $exportDisk; |
||
43 | /** @var AttachmentRepositoryInterface */ |
||
44 | private $repository; |
||
45 | /** @var Carbon */ |
||
46 | private $start; |
||
47 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
48 | private $uploadDisk; |
||
49 | |||
50 | /** |
||
51 | * AttachmentCollector constructor. |
||
52 | */ |
||
53 | public function __construct() |
||
54 | { |
||
55 | /** @var AttachmentRepositoryInterface repository */ |
||
56 | $this->repository = app(AttachmentRepositoryInterface::class); |
||
57 | // make storage: |
||
58 | $this->uploadDisk = Storage::disk('upload'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
59 | $this->exportDisk = Storage::disk('export'); |
||
60 | |||
61 | parent::__construct(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function run(): bool |
||
68 | { |
||
69 | // grab all the users attachments: |
||
70 | $attachments = $this->getAttachments(); |
||
71 | |||
72 | /** @var Attachment $attachment */ |
||
73 | foreach ($attachments as $attachment) { |
||
74 | $this->exportAttachment($attachment); |
||
75 | } |
||
76 | |||
77 | return true; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param Carbon $start |
||
82 | * @param Carbon $end |
||
83 | */ |
||
84 | public function setDates(Carbon $start, Carbon $end) |
||
85 | { |
||
86 | $this->start = $start; |
||
87 | $this->end = $end; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param Attachment $attachment |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | private function exportAttachment(Attachment $attachment): bool |
||
96 | { |
||
97 | $file = $attachment->fileName(); |
||
98 | if ($this->uploadDisk->exists($file)) { |
||
99 | try { |
||
100 | $decrypted = Crypt::decrypt($this->uploadDisk->get($file)); |
||
101 | $exportFile = $this->exportFileName($attachment); |
||
102 | $this->exportDisk->put($exportFile, $decrypted); |
||
103 | $this->getEntries()->push($exportFile); |
||
104 | } catch (DecryptException $e) { |
||
105 | Log::error('Catchable error: could not decrypt attachment #' . $attachment->id . ' because: ' . $e->getMessage()); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | return true; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns the new file name for the export file. |
||
114 | * |
||
115 | * @param $attachment |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | private function exportFileName($attachment): string |
||
120 | { |
||
121 | return sprintf('%s-Attachment nr. %s - %s', $this->job->key, (string)$attachment->id, $attachment->filename); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return Collection |
||
126 | */ |
||
127 | private function getAttachments(): Collection |
||
128 | { |
||
129 | $this->repository->setUser($this->user); |
||
130 | |||
131 | return $this->repository->getBetween($this->start, $this->end); |
||
132 | } |
||
133 | } |
||
134 |