1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * UploadCollector.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 Crypt; |
||
26 | use Exception; |
||
27 | use Illuminate\Contracts\Encryption\DecryptException; |
||
28 | use Log; |
||
29 | use Storage; |
||
30 | |||
31 | /** |
||
32 | * Class UploadCollector. |
||
33 | */ |
||
34 | class UploadCollector extends BasicCollector implements CollectorInterface |
||
35 | { |
||
36 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
37 | private $exportDisk; |
||
38 | /** @var \Illuminate\Contracts\Filesystem\Filesystem */ |
||
39 | private $uploadDisk; |
||
40 | |||
41 | /** |
||
42 | * AttachmentCollector constructor. |
||
43 | */ |
||
44 | public function __construct() |
||
45 | { |
||
46 | parent::__construct(); |
||
47 | $this->uploadDisk = Storage::disk('upload'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
48 | $this->exportDisk = Storage::disk('export'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Is called from the outside to actually start the export. |
||
53 | * |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function run(): bool |
||
57 | { |
||
58 | Log::debug('Going to collect attachments', ['key' => $this->job->key]); |
||
59 | $this->collectModernUploads(); |
||
60 | |||
61 | return true; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * This method collects all the uploads that are uploaded using the new importer. So after the summer of 2016. |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | private function collectModernUploads(): bool |
||
70 | { |
||
71 | $set = $this->job->user->importJobs()->whereIn('status', ['import_complete', 'finished'])->get(['import_jobs.*']); |
||
72 | Log::debug(sprintf('Found %d import jobs', $set->count())); |
||
73 | $keys = []; |
||
74 | if ($set->count() > 0) { |
||
75 | $keys = $set->pluck('key')->toArray(); |
||
76 | } |
||
77 | |||
78 | foreach ($keys as $key) { |
||
79 | $this->processModernUpload($key); |
||
80 | } |
||
81 | |||
82 | return true; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param string $key |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | private function processModernUpload(string $key): bool |
||
91 | { |
||
92 | // find job associated with import file: |
||
93 | $job = $this->job->user->importJobs()->where('key', $key)->first(); |
||
94 | if (null === $job) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | // find the file for this import: |
||
99 | $content = ''; |
||
100 | try { |
||
101 | $content = Crypt::decrypt($this->uploadDisk->get(sprintf('%s.upload', $key))); |
||
102 | } catch (Exception | DecryptException $e) { |
||
103 | Log::error(sprintf('Could not decrypt old import file "%s". Skipped because: %s', $key, $e->getMessage())); |
||
104 | } |
||
105 | |||
106 | if (strlen($content) > 0) { |
||
107 | // add to export disk. |
||
108 | $date = $job->created_at->format('Y-m-d'); |
||
109 | $file = sprintf('%s-Old %s import dated %s.%s', $this->job->key, strtoupper($job->file_type), $date, $job->file_type); |
||
110 | $this->exportDisk->put($file, $content); |
||
111 | $this->getEntries()->push($file); |
||
112 | } |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | } |
||
117 |