Files
firefly-iii/app/Http/Controllers/AttachmentController.php

196 lines
6.1 KiB
PHP
Raw Normal View History

2015-07-18 21:32:31 +02:00
<?php
/**
* AttachmentController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2015-07-18 21:32:31 +02:00
namespace FireflyIII\Http\Controllers;
2015-07-25 16:48:32 +02:00
use File;
2016-02-07 07:56:58 +01:00
use FireflyIII\Exceptions\FireflyException;
2015-07-19 09:37:28 +02:00
use FireflyIII\Http\Requests\AttachmentFormRequest;
2015-07-18 21:32:31 +02:00
use FireflyIII\Models\Attachment;
2015-07-19 09:37:28 +02:00
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
2017-02-17 20:14:22 +01:00
use Illuminate\Http\Request;
2017-01-14 19:43:33 +01:00
use Illuminate\Http\Response as LaravelResponse;
2015-07-19 09:37:28 +02:00
use Preferences;
2015-07-25 16:48:32 +02:00
use Response;
2015-07-18 23:51:51 +02:00
use View;
2015-07-18 21:32:31 +02:00
/**
* Class AttachmentController
*
2016-12-28 18:49:30 +01:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) // it's 13.
*
2015-07-18 21:32:31 +02:00
* @package FireflyIII\Http\Controllers
*/
class AttachmentController extends Controller
{
2015-07-18 23:51:51 +02:00
/**
*
2015-07-18 23:51:51 +02:00
*/
public function __construct()
{
parent::__construct();
2016-10-29 07:44:46 +02:00
// translations:
$this->middleware(
function ($request, $next) {
View::share('mainTitleIcon', 'fa-paperclip');
View::share('title', trans('firefly.attachments'));
return $next($request);
}
);
2015-07-18 23:51:51 +02:00
}
2015-07-19 09:53:58 +02:00
/**
2017-02-17 20:14:22 +01:00
* @param Request $request
2015-07-19 09:53:58 +02:00
* @param Attachment $attachment
*
2016-12-28 18:49:30 +01:00
* @return View
2015-07-19 09:53:58 +02:00
*/
2017-02-17 20:14:22 +01:00
public function delete(Request $request, Attachment $attachment)
2015-07-19 09:53:58 +02:00
{
$subTitle = trans('firefly.delete_attachment', ['name' => $attachment->filename]);
// put previous url in session
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('attachments.delete.uri');
2017-02-17 20:14:22 +01:00
$request->session()->flash('gaEventCategory', 'attachments');
$request->session()->flash('gaEventAction', 'delete-attachment');
2015-07-19 09:53:58 +02:00
return view('attachments.delete', compact('attachment', 'subTitle'));
}
/**
2017-02-17 20:14:22 +01:00
* @param Request $request
2015-07-19 09:53:58 +02:00
* @param AttachmentRepositoryInterface $repository
* @param Attachment $attachment
*
2017-02-17 20:14:22 +01:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-07-19 09:53:58 +02:00
*/
2017-02-17 20:14:22 +01:00
public function destroy(Request $request, AttachmentRepositoryInterface $repository, Attachment $attachment)
2015-07-19 09:53:58 +02:00
{
$name = $attachment->filename;
$repository->destroy($attachment);
2017-02-17 20:14:22 +01:00
$request->session()->flash('success', strval(trans('firefly.attachment_deleted', ['name' => $name])));
2015-07-19 09:53:58 +02:00
Preferences::mark();
2017-02-05 08:26:54 +01:00
return redirect($this->getPreviousUri('attachments.delete.uri'));
2015-07-19 09:53:58 +02:00
}
2015-07-18 21:32:31 +02:00
/**
2017-01-05 10:06:46 +01:00
* @param AttachmentRepositoryInterface $repository
* @param Attachment $attachment
2016-01-29 07:08:17 +01:00
*
2017-01-05 10:06:46 +01:00
* @return mixed
2016-02-07 07:56:58 +01:00
* @throws FireflyException
2015-07-18 21:32:31 +02:00
*/
2016-12-25 12:23:36 +01:00
public function download(AttachmentRepositoryInterface $repository, Attachment $attachment)
2015-07-18 21:32:31 +02:00
{
2017-06-14 20:13:19 +02:00
2016-12-25 12:23:36 +01:00
if ($repository->exists($attachment)) {
$content = $repository->getContent($attachment);
2016-08-26 08:21:31 +02:00
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
2016-12-25 12:23:36 +01:00
2017-06-14 20:13:19 +02:00
2017-01-14 19:43:33 +01:00
/** @var LaravelResponse $response */
$response = response($content, 200);
$response
->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')
->header('Content-Length', strlen($content));
2017-01-14 19:43:33 +01:00
return $response;
2015-07-18 21:32:31 +02:00
}
2016-02-07 07:56:58 +01:00
throw new FireflyException('Could not find the indicated attachment. The file is no longer there.');
2015-07-19 09:37:28 +02:00
}
2016-01-24 15:58:16 +01:00
/**
2017-02-17 20:14:22 +01:00
* @param Request $request
2016-01-24 15:58:16 +01:00
* @param Attachment $attachment
*
* @return View
2016-01-24 15:58:16 +01:00
*/
2017-02-17 20:14:22 +01:00
public function edit(Request $request, Attachment $attachment)
2016-01-24 15:58:16 +01: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").
2016-02-04 07:27:03 +01:00
if (session('attachments.edit.fromUpdate') !== true) {
2017-02-05 08:26:54 +01:00
$this->rememberPreviousUri('attachments.edit.uri');
2016-01-24 15:58:16 +01:00
}
2017-02-17 20:14:22 +01:00
$request->session()->forget('attachments.edit.fromUpdate');
2016-01-24 15:58:16 +01:00
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
}
/**
* @param Attachment $attachment
2015-07-25 18:33:19 +02:00
*
* @return \Illuminate\Http\Response
*/
2015-07-25 16:48:32 +02:00
public function preview(Attachment $attachment)
{
2016-04-28 16:30:21 +02:00
$image = 'images/page_green.png';
2017-06-14 20:13:19 +02:00
2017-07-15 16:41:07 +02:00
if ($attachment->mime === 'application/pdf') {
2016-04-28 16:30:21 +02:00
$image = 'images/page_white_acrobat.png';
2015-07-25 19:04:39 +02:00
}
2016-04-28 16:30:21 +02:00
$file = public_path($image);
2015-07-25 16:48:32 +02:00
$response = Response::make(File::get($file));
$response->header('Content-Type', 'image/png');
2015-07-25 16:48:32 +02:00
return $response;
}
2015-07-19 09:37:28 +02: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 22:03:00 +02:00
$data = $request->getAttachmentData();
$repository->update($attachment, $data);
2015-07-19 09:37:28 +02:00
2017-02-17 20:14:22 +01:00
$request->session()->flash('success', strval(trans('firefly.attachment_updated', ['name' => $attachment->filename])));
2015-07-19 09:37:28 +02:00
Preferences::mark();
2016-12-28 13:02:56 +01:00
if (intval($request->get('return_to_edit')) === 1) {
2017-03-18 11:02:02 +01:00
// @codeCoverageIgnoreStart
2017-02-17 20:14:22 +01:00
$request->session()->put('attachments.edit.fromUpdate', true);
2015-07-19 09:37:28 +02:00
2015-07-19 12:21:38 +02:00
return redirect(route('attachments.edit', [$attachment->id]))->withInput(['return_to_edit' => 1]);
2017-03-18 11:02:02 +01:00
// @codeCoverageIgnoreEnd
2015-07-19 09:37:28 +02:00
}
2015-07-18 21:32:31 +02:00
2015-07-19 09:37:28 +02:00
// redirect to previous URL.
2017-02-05 08:26:54 +01:00
return redirect($this->getPreviousUri('attachments.edit.uri'));
2015-07-18 21:32:31 +02:00
}
2015-07-19 09:38:44 +02:00
}