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

194 lines
5.6 KiB
PHP
Raw Normal View History

2015-07-18 14:32:31 -05: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.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-07-18 14:32:31 -05:00
namespace FireflyIII\Http\Controllers;
2015-07-18 16:51:51 -05:00
use Crypt;
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;
use Input;
use Log;
2015-07-19 02:37:28 -05:00
use Preferences;
2015-07-25 09:48:32 -05:00
use Response;
2015-07-19 02:37:28 -05:00
use Session;
2016-02-25 07:53:26 -06:00
use Storage;
2015-07-19 02:53:58 -05:00
use URL;
2015-07-18 16:51:51 -05:00
use View;
2015-07-18 14:32:31 -05:00
/**
* Class AttachmentController
*
* @package FireflyIII\Http\Controllers
*/
class AttachmentController extends Controller
{
2015-07-18 16:51:51 -05: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) {
View::share('mainTitleIcon', 'fa-paperclip');
View::share('title', trans('firefly.attachments'));
return $next($request);
}
);
2015-07-18 16:51:51 -05:00
}
2015-07-19 02:53:58 -05:00
/**
* @param Attachment $attachment
*
* @return View
2015-07-19 02:53:58 -05:00
*/
public function delete(Attachment $attachment)
{
$subTitle = trans('firefly.delete_attachment', ['name' => $attachment->filename]);
// put previous url in session
2015-07-19 05:21:38 -05:00
Session::put('attachments.delete.url', URL::previous());
2015-07-19 02:53:58 -05:00
Session::flash('gaEventCategory', 'attachments');
Session::flash('gaEventAction', 'delete-attachment');
return view('attachments.delete', compact('attachment', 'subTitle'));
}
/**
* @param AttachmentRepositoryInterface $repository
* @param Attachment $attachment
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(AttachmentRepositoryInterface $repository, Attachment $attachment)
{
$name = $attachment->filename;
$repository->destroy($attachment);
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.attachment_deleted', ['name' => $name])));
2015-07-19 02:53:58 -05:00
Preferences::mark();
2016-02-04 00:27:03 -06:00
return redirect(session('attachments.delete.url'));
2015-07-19 02:53:58 -05:00
}
2015-07-18 14:32:31 -05:00
/**
2016-03-19 10:51:52 -05:00
* @param Attachment $attachment
2016-01-29 00:08:17 -06:00
*
2016-02-07 00:56:58 -06:00
* @throws FireflyException
*
2015-07-18 14:32:31 -05:00
*/
2016-03-14 14:47:13 -05:00
public function download(Attachment $attachment)
2015-07-18 14:32:31 -05:00
{
2016-02-25 07:53:26 -06:00
// create a disk.
$disk = Storage::disk('upload');
$file = $attachment->fileName();
2015-07-18 14:32:31 -05:00
2016-02-25 07:53:26 -06:00
if ($disk->exists($file)) {
2015-07-18 14:32:31 -05:00
2016-08-26 01:21:31 -05:00
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
$content = Crypt::decrypt($disk->get($file));
2015-07-18 14:32:31 -05:00
2016-08-26 01:21:31 -05:00
Log::debug('Send file to user', ['file' => $quoted, 'size' => strlen($content)]);
return response($content, 200)
->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));
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
/**
* @param Attachment $attachment
*
* @return View
2016-01-24 08:58:16 -06:00
*/
public function edit(Attachment $attachment)
{
$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 00:27:03 -06:00
if (session('attachments.edit.fromUpdate') !== true) {
2016-01-24 08:58:16 -06:00
Session::put('attachments.edit.url', URL::previous());
}
Session::forget('attachments.edit.fromUpdate');
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
}
/**
* @param Attachment $attachment
2015-07-25 11:33:19 -05:00
*
* @return \Illuminate\Http\Response
*/
2015-07-25 09:48:32 -05:00
public function preview(Attachment $attachment)
{
2016-04-28 09:30:21 -05:00
$image = 'images/page_green.png';
2015-07-26 08:51:07 -05:00
if ($attachment->mime == 'application/pdf') {
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-25 09:48:32 -05:00
return $response;
}
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
2016-03-20 05:38:01 -05:00
Session::flash('success', strval(trans('firefly.attachment_updated', ['name' => $attachment->filename])));
2015-07-19 02:37:28 -05:00
Preferences::mark();
if (intval(Input::get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
2015-07-19 05:21:38 -05:00
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]);
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.
2016-02-04 00:27:03 -06:00
return redirect(session('attachments.edit.url'));
2015-07-18 14:32:31 -05:00
}
2015-07-19 02:38:44 -05:00
}