1 | <?php |
||
2 | /** |
||
3 | * AttachmentController.php |
||
4 | * Copyright (c) 2017 [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 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Http\Controllers; |
||
24 | |||
25 | use FireflyIII\Exceptions\FireflyException; |
||
26 | use FireflyIII\Http\Requests\AttachmentFormRequest; |
||
27 | use FireflyIII\Models\Attachment; |
||
28 | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; |
||
29 | use Illuminate\Http\Request; |
||
30 | use Illuminate\Http\Response as LaravelResponse; |
||
31 | use Preferences; |
||
32 | use View; |
||
33 | |||
34 | /** |
||
35 | * Class AttachmentController. |
||
36 | * |
||
37 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) // it's 13. |
||
38 | */ |
||
39 | class AttachmentController extends Controller |
||
40 | { |
||
41 | /** @var AttachmentRepositoryInterface */ |
||
42 | private $repository; |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | */ |
||
47 | public function __construct() |
||
48 | { |
||
49 | parent::__construct(); |
||
50 | |||
51 | // translations: |
||
52 | $this->middleware( |
||
53 | function ($request, $next) { |
||
54 | app('view')->share('mainTitleIcon', 'fa-paperclip'); |
||
55 | app('view')->share('title', trans('firefly.attachments')); |
||
56 | $this->repository = app(AttachmentRepositoryInterface::class); |
||
57 | |||
58 | return $next($request); |
||
59 | } |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param Attachment $attachment |
||
65 | * |
||
66 | * @return View |
||
67 | */ |
||
68 | public function delete(Attachment $attachment) |
||
69 | { |
||
70 | $subTitle = trans('firefly.delete_attachment', ['name' => $attachment->filename]); |
||
71 | |||
72 | // put previous url in session |
||
73 | $this->rememberPreviousUri('attachments.delete.uri'); |
||
74 | |||
75 | return view('attachments.delete', compact('attachment', 'subTitle')); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param Request $request |
||
80 | * @param Attachment $attachment |
||
81 | * |
||
82 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
83 | */ |
||
84 | public function destroy(Request $request, Attachment $attachment) |
||
85 | { |
||
86 | $name = $attachment->filename; |
||
87 | |||
88 | $this->repository->destroy($attachment); |
||
89 | |||
90 | $request->session()->flash('success', (string)trans('firefly.attachment_deleted', ['name' => $name])); |
||
91 | Preferences::mark(); |
||
92 | |||
93 | return redirect($this->getPreviousUri('attachments.delete.uri')); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param Attachment $attachment |
||
98 | * |
||
99 | * @return mixed |
||
100 | * |
||
101 | * @throws FireflyException |
||
102 | */ |
||
103 | public function download(Attachment $attachment) |
||
104 | { |
||
105 | if ($this->repository->exists($attachment)) { |
||
106 | $content = $this->repository->getContent($attachment); |
||
107 | $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\')); |
||
108 | |||
109 | /** @var LaravelResponse $response */ |
||
110 | $response = response($content, 200); |
||
111 | $response |
||
112 | ->header('Content-Description', 'File Transfer') |
||
113 | ->header('Content-Type', 'application/octet-stream') |
||
114 | ->header('Content-Disposition', 'attachment; filename=' . $quoted) |
||
115 | ->header('Content-Transfer-Encoding', 'binary') |
||
116 | ->header('Connection', 'Keep-Alive') |
||
117 | ->header('Expires', '0') |
||
118 | ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0') |
||
119 | ->header('Pragma', 'public') |
||
120 | ->header('Content-Length', strlen($content)); |
||
121 | |||
122 | return $response; |
||
123 | } |
||
124 | throw new FireflyException('Could not find the indicated attachment. The file is no longer there.'); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param Request $request |
||
129 | * @param Attachment $attachment |
||
130 | * |
||
131 | * @return View |
||
132 | */ |
||
133 | public function edit(Request $request, Attachment $attachment) |
||
134 | { |
||
135 | $subTitleIcon = 'fa-pencil'; |
||
136 | $subTitle = trans('firefly.edit_attachment', ['name' => $attachment->filename]); |
||
137 | |||
138 | // put previous url in session if not redirect from store (not "return_to_edit"). |
||
139 | if (true !== session('attachments.edit.fromUpdate')) { |
||
140 | $this->rememberPreviousUri('attachments.edit.uri'); |
||
141 | } |
||
142 | $request->session()->forget('attachments.edit.fromUpdate'); |
||
143 | |||
144 | $preFilled['notes'] = $this->repository->getNoteText($attachment); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
145 | $request->session()->flash('preFilled', $preFilled); |
||
146 | |||
147 | return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle')); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param AttachmentFormRequest $request |
||
152 | * @param Attachment $attachment |
||
153 | * |
||
154 | * @return \Illuminate\Http\RedirectResponse |
||
155 | */ |
||
156 | public function update(AttachmentFormRequest $request, Attachment $attachment) |
||
157 | { |
||
158 | $data = $request->getAttachmentData(); |
||
159 | $this->repository->update($attachment, $data); |
||
160 | |||
161 | $request->session()->flash('success', (string)trans('firefly.attachment_updated', ['name' => $attachment->filename])); |
||
162 | Preferences::mark(); |
||
163 | |||
164 | if (1 === (int)$request->get('return_to_edit')) { |
||
165 | // @codeCoverageIgnoreStart |
||
166 | $request->session()->put('attachments.edit.fromUpdate', true); |
||
167 | |||
168 | return redirect(route('attachments.edit', [$attachment->id]))->withInput(['return_to_edit' => 1]); |
||
169 | // @codeCoverageIgnoreEnd |
||
170 | } |
||
171 | |||
172 | // redirect to previous URL. |
||
173 | return redirect($this->getPreviousUri('attachments.edit.uri')); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param Attachment $attachment |
||
178 | * |
||
179 | * @return \Illuminate\Http\Response |
||
180 | * @throws FireflyException |
||
181 | */ |
||
182 | public function view(Attachment $attachment) |
||
183 | { |
||
184 | if ($this->repository->exists($attachment)) { |
||
185 | $content = $this->repository->getContent($attachment); |
||
186 | |||
187 | return response()->make( |
||
188 | $content, 200, [ |
||
189 | 'Content-Type' => $attachment->mime, |
||
190 | 'Content-Disposition' => 'inline; filename="' . $attachment->filename . '"', |
||
191 | ] |
||
192 | ); |
||
193 | } |
||
194 | throw new FireflyException('Could not find the indicated attachment. The file is no longer there.'); |
||
195 | } |
||
196 | } |
||
197 |