2015-07-18 14:32:31 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* AttachmentController.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-07-18 14:32:31 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
|
|
|
|
2015-07-25 09:48:32 -05:00
|
|
|
use File;
|
2016-02-07 00:56:58 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2015-07-19 02:37:28 -05:00
|
|
|
use FireflyIII\Http\Requests\AttachmentFormRequest;
|
2015-07-18 14:32:31 -05:00
|
|
|
use FireflyIII\Models\Attachment;
|
2015-07-19 02:37:28 -05:00
|
|
|
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
|
2017-02-17 13:14:22 -06:00
|
|
|
use Illuminate\Http\Request;
|
2017-01-14 12:43:33 -06:00
|
|
|
use Illuminate\Http\Response as LaravelResponse;
|
2015-07-19 02:37:28 -05:00
|
|
|
use Preferences;
|
2015-07-25 09:48:32 -05:00
|
|
|
use Response;
|
2015-07-18 16:51:51 -05:00
|
|
|
use View;
|
|
|
|
|
2015-07-18 14:32:31 -05:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AttachmentController.
|
2015-07-18 14:32:31 -05:00
|
|
|
*
|
2016-12-28 11:49:30 -06:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) // it's 13.
|
2015-07-18 14:32:31 -05:00
|
|
|
*/
|
|
|
|
class AttachmentController extends Controller
|
|
|
|
{
|
2015-07-18 16:51:51 -05:00
|
|
|
/**
|
2016-02-03 04:46:28 -06:00
|
|
|
*
|
2015-07-18 16:51:51 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
// translations:
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-paperclip');
|
|
|
|
app('view')->share('title', trans('firefly.attachments'));
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-07-18 16:51:51 -05:00
|
|
|
}
|
|
|
|
|
2015-07-19 02:53:58 -05:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
2016-12-28 11:49:30 -06:00
|
|
|
* @return View
|
2015-07-19 02:53:58 -05:00
|
|
|
*/
|
2017-12-29 02:05:35 -06:00
|
|
|
public function delete(Attachment $attachment)
|
2015-07-19 02:53:58 -05:00
|
|
|
{
|
|
|
|
$subTitle = trans('firefly.delete_attachment', ['name' => $attachment->filename]);
|
|
|
|
|
|
|
|
// put previous url in session
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('attachments.delete.uri');
|
2015-07-19 02:53:58 -05:00
|
|
|
|
|
|
|
return view('attachments.delete', compact('attachment', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2015-07-19 02:53:58 -05:00
|
|
|
* @param AttachmentRepositoryInterface $repository
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-07-19 02:53:58 -05:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function destroy(Request $request, AttachmentRepositoryInterface $repository, Attachment $attachment)
|
2015-07-19 02:53:58 -05:00
|
|
|
{
|
|
|
|
$name = $attachment->filename;
|
|
|
|
|
|
|
|
$repository->destroy($attachment);
|
|
|
|
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.attachment_deleted', ['name' => $name])));
|
2015-07-19 02:53:58 -05:00
|
|
|
Preferences::mark();
|
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('attachments.delete.uri'));
|
2015-07-19 02:53:58 -05:00
|
|
|
}
|
|
|
|
|
2015-07-18 14:32:31 -05:00
|
|
|
/**
|
2017-01-05 03:06:46 -06:00
|
|
|
* @param AttachmentRepositoryInterface $repository
|
|
|
|
* @param Attachment $attachment
|
2016-01-29 00:08:17 -06:00
|
|
|
*
|
2017-01-05 03:06:46 -06:00
|
|
|
* @return mixed
|
2017-11-15 05:25:49 -06:00
|
|
|
*
|
2016-02-07 00:56:58 -06:00
|
|
|
* @throws FireflyException
|
2015-07-18 14:32:31 -05:00
|
|
|
*/
|
2016-12-25 05:23:36 -06:00
|
|
|
public function download(AttachmentRepositoryInterface $repository, Attachment $attachment)
|
2015-07-18 14:32:31 -05:00
|
|
|
{
|
2016-12-25 05:23:36 -06:00
|
|
|
if ($repository->exists($attachment)) {
|
|
|
|
$content = $repository->getContent($attachment);
|
2016-08-26 01:21:31 -05:00
|
|
|
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
|
2016-12-25 05:23:36 -06:00
|
|
|
|
2017-01-14 12:43:33 -06:00
|
|
|
/** @var LaravelResponse $response */
|
|
|
|
$response = response($content, 200);
|
|
|
|
$response
|
2016-02-03 04:46:28 -06:00
|
|
|
->header('Content-Description', 'File Transfer')
|
|
|
|
->header('Content-Type', 'application/octet-stream')
|
|
|
|
->header('Content-Disposition', 'attachment; filename=' . $quoted)
|
|
|
|
->header('Content-Transfer-Encoding', 'binary')
|
|
|
|
->header('Connection', 'Keep-Alive')
|
|
|
|
->header('Expires', '0')
|
|
|
|
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
|
|
|
|
->header('Pragma', 'public')
|
2016-07-04 05:37:33 -05:00
|
|
|
->header('Content-Length', strlen($content));
|
2017-01-14 12:43:33 -06:00
|
|
|
|
|
|
|
return $response;
|
2015-07-18 14:32:31 -05:00
|
|
|
}
|
2016-02-07 00:56:58 -06:00
|
|
|
throw new FireflyException('Could not find the indicated attachment. The file is no longer there.');
|
2015-07-19 02:37:28 -05:00
|
|
|
}
|
|
|
|
|
2016-01-24 08:58:16 -06:00
|
|
|
/**
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2016-01-24 08:58:16 -06:00
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
2016-08-26 02:30:52 -05:00
|
|
|
* @return View
|
2016-01-24 08:58:16 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function edit(Request $request, Attachment $attachment)
|
2016-01-24 08:58:16 -06:00
|
|
|
{
|
|
|
|
$subTitleIcon = 'fa-pencil';
|
|
|
|
$subTitle = trans('firefly.edit_attachment', ['name' => $attachment->filename]);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2017-11-15 05:25:49 -06:00
|
|
|
if (true !== session('attachments.edit.fromUpdate')) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('attachments.edit.uri');
|
2016-01-24 08:58:16 -06:00
|
|
|
}
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->forget('attachments.edit.fromUpdate');
|
2016-01-24 08:58:16 -06:00
|
|
|
|
|
|
|
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
2015-07-19 11:37:29 -05:00
|
|
|
/**
|
|
|
|
* @param Attachment $attachment
|
2015-07-25 11:33:19 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
2015-07-19 11:37:29 -05:00
|
|
|
*/
|
2015-07-25 09:48:32 -05:00
|
|
|
public function preview(Attachment $attachment)
|
2015-07-19 11:37:29 -05:00
|
|
|
{
|
2016-04-28 09:30:21 -05:00
|
|
|
$image = 'images/page_green.png';
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if ('application/pdf' === $attachment->mime) {
|
2016-04-28 09:30:21 -05:00
|
|
|
$image = 'images/page_white_acrobat.png';
|
2015-07-25 12:04:39 -05:00
|
|
|
}
|
2016-04-28 09:30:21 -05:00
|
|
|
$file = public_path($image);
|
2015-07-25 09:48:32 -05:00
|
|
|
$response = Response::make(File::get($file));
|
|
|
|
$response->header('Content-Type', 'image/png');
|
2015-07-19 11:37:29 -05:00
|
|
|
|
2015-07-25 09:48:32 -05:00
|
|
|
return $response;
|
2015-07-19 11:37:29 -05:00
|
|
|
}
|
|
|
|
|
2015-07-19 02:37:28 -05:00
|
|
|
/**
|
|
|
|
* @param AttachmentFormRequest $request
|
|
|
|
* @param AttachmentRepositoryInterface $repository
|
|
|
|
* @param Attachment $attachment
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function update(AttachmentFormRequest $request, AttachmentRepositoryInterface $repository, Attachment $attachment)
|
|
|
|
{
|
2016-10-22 15:03:00 -05:00
|
|
|
$data = $request->getAttachmentData();
|
|
|
|
$repository->update($attachment, $data);
|
2015-07-19 02:37:28 -05:00
|
|
|
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->flash('success', strval(trans('firefly.attachment_updated', ['name' => $attachment->filename])));
|
2015-07-19 02:37:28 -05:00
|
|
|
Preferences::mark();
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (1 === intval($request->get('return_to_edit'))) {
|
2017-03-18 05:02:02 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->put('attachments.edit.fromUpdate', true);
|
2015-07-19 02:37:28 -05:00
|
|
|
|
2015-07-19 05:21:38 -05:00
|
|
|
return redirect(route('attachments.edit', [$attachment->id]))->withInput(['return_to_edit' => 1]);
|
2017-03-18 05:02:02 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-07-19 02:37:28 -05:00
|
|
|
}
|
2015-07-18 14:32:31 -05:00
|
|
|
|
2015-07-19 02:37:28 -05:00
|
|
|
// redirect to previous URL.
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('attachments.edit.uri'));
|
2015-07-18 14:32:31 -05:00
|
|
|
}
|
2015-07-19 02:38:44 -05:00
|
|
|
}
|