1 | <?php |
||
2 | /** |
||
3 | * UploadCollector.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Export\Collector; |
||
25 | |||
26 | use Crypt; |
||
27 | use Exception; |
||
28 | use Illuminate\Contracts\Encryption\DecryptException; |
||
29 | use Log; |
||
30 | use Storage; |
||
31 | |||
32 | /** |
||
33 | * Class UploadCollector. |
||
34 | * |
||
35 | * @codeCoverageIgnore |
||
36 | * @deprecated |
||
37 | */ |
||
38 | class UploadCollector extends BasicCollector implements CollectorInterface |
||
39 | { |
||
40 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
41 | private $exportDisk; |
||
42 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
43 | private $uploadDisk; |
||
44 | |||
45 | /** |
||
46 | * AttachmentCollector constructor. |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | parent::__construct(); |
||
51 | $this->uploadDisk = Storage::disk('upload'); |
||
52 | $this->exportDisk = Storage::disk('export'); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Is called from the outside to actually start the export. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function run(): bool |
||
61 | { |
||
62 | Log::debug('Going to collect attachments', ['key' => $this->job->key]); |
||
63 | $this->collectModernUploads(); |
||
64 | |||
65 | return true; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * This method collects all the uploads that are uploaded using the new importer. So after the summer of 2016. |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | private function collectModernUploads(): bool |
||
74 | { |
||
75 | $set = $this->job->user->importJobs()->whereIn('status', ['import_complete', 'finished'])->get(['import_jobs.*']); |
||
76 | Log::debug(sprintf('Found %d import jobs', $set->count())); |
||
77 | $keys = []; |
||
78 | if ($set->count() > 0) { |
||
79 | $keys = $set->pluck('key')->toArray(); |
||
80 | } |
||
81 | |||
82 | foreach ($keys as $key) { |
||
83 | $this->processModernUpload($key); |
||
84 | } |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | |||
89 | /** @noinspection MultipleReturnStatementsInspection */ |
||
90 | /** |
||
91 | * Process new file uploads. |
||
92 | * |
||
93 | * @param string $key |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | private function processModernUpload(string $key): bool |
||
98 | { |
||
99 | // find job associated with import file: |
||
100 | $job = $this->job->user->importJobs()->where('key', $key)->first(); |
||
101 | if (null === $job) { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | // find the file for this import: |
||
106 | $content = ''; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
107 | try { |
||
108 | $content = Crypt::decrypt($this->uploadDisk->get(sprintf('%s.upload', $key))); |
||
109 | } catch (Exception | DecryptException $e) { |
||
110 | Log::error(sprintf('Could not decrypt old import file "%s". Skipped because: %s', $key, $e->getMessage())); |
||
111 | } |
||
112 | |||
113 | if ('' !== $content) { |
||
114 | // add to export disk. |
||
115 | $date = $job->created_at->format('Y-m-d'); |
||
116 | $file = sprintf('%s-Old %s import dated %s.%s', $this->job->key, strtoupper($job->file_type), $date, $job->file_type); |
||
117 | $this->exportDisk->put($file, $content); |
||
118 | $this->getEntries()->push($file); |
||
119 | } |
||
120 | |||
121 | return true; |
||
122 | } |
||
123 | } |
||
124 |