Files
firefly-iii/app/Api/V1/Controllers/AttachmentController.php

254 lines
8.4 KiB
PHP
Raw Normal View History

2018-06-24 06:51:22 +02:00
<?php
/**
* AttachmentController.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-24 06:51:22 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-24 06:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-06-24 06:51:22 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-24 06:51:22 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-06-24 06:51:22 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-24 06:51:22 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
2019-08-10 07:04:31 +02:00
use FireflyIII\Api\V1\Requests\AttachmentStoreRequest;
use FireflyIII\Api\V1\Requests\AttachmentUpdateRequest;
2018-06-24 06:51:22 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response as LaravelResponse;
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
2019-10-20 16:17:43 +02:00
use Log;
use function strlen;
2018-06-24 06:51:22 +02:00
/**
* Class AttachmentController.
*
2018-06-24 06:51:22 +02:00
*/
class AttachmentController extends Controller
{
/** @var AttachmentRepositoryInterface The attachment repository */
2018-06-24 06:51:22 +02:00
private $repository;
/**
* AccountController constructor.
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
2018-06-24 06:51:22 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->repository = app(AttachmentRepositoryInterface::class);
$this->repository->setUser($user);
return $next($request);
}
);
}
/**
* Remove the specified resource from storage.
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
2019-09-04 17:39:39 +02:00
*
2018-06-24 08:33:06 +02:00
* @param Attachment $attachment
2018-06-24 06:51:22 +02:00
*
2018-06-24 08:33:06 +02:00
* @return JsonResponse
2018-06-24 06:51:22 +02:00
*/
2018-06-24 08:33:06 +02:00
public function delete(Attachment $attachment): JsonResponse
2018-06-24 06:51:22 +02:00
{
2018-06-24 08:33:06 +02:00
$this->repository->destroy($attachment);
return response()->json([], 204);
2018-06-24 06:51:22 +02:00
}
/**
* Download an attachment.
*
2018-06-24 06:51:22 +02:00
* @param Attachment $attachment
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
2018-06-24 06:51:22 +02:00
* @return LaravelResponse
* @throws FireflyException
2018-06-24 06:51:22 +02:00
*/
public function download(Attachment $attachment): LaravelResponse
{
2018-07-05 21:18:53 +02:00
if (false === $attachment->uploaded) {
2019-10-30 20:02:21 +01:00
throw new FireflyException('200000: File has not been uploaded (yet).');
2018-06-24 06:51:22 +02:00
}
2019-10-20 16:17:43 +02:00
if (0 === $attachment->size) {
2019-10-30 20:02:21 +01:00
throw new FireflyException('200000: File has not been uploaded (yet).');
2019-10-20 16:17:43 +02:00
}
2018-06-24 06:51:22 +02:00
if ($this->repository->exists($attachment)) {
$content = $this->repository->getContent($attachment);
2019-10-20 16:17:43 +02:00
if ('' === $content) {
2019-10-30 20:02:21 +01:00
throw new FireflyException('200002: File is empty (zero bytes).');
2019-10-20 16:17:43 +02:00
}
2018-06-24 06:51:22 +02:00
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
/** @var LaravelResponse $response */
2019-02-13 17:38:41 +01:00
$response = response($content);
2018-06-24 06:51:22 +02:00
$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));
2018-06-24 06:51:22 +02:00
return $response;
}
2019-10-30 20:02:21 +01:00
throw new FireflyException('200003: File does not exist.');
2018-06-24 06:51:22 +02:00
}
/**
* Display a listing of the resource.
*
* @return JsonResponse
2019-09-04 17:39:39 +02:00
* @codeCoverageIgnore
2018-06-24 06:51:22 +02:00
*/
2019-09-04 17:39:39 +02:00
public function index(): JsonResponse
2018-06-24 06:51:22 +02:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-06-24 06:51:22 +02:00
// types to get, page size:
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
// get list of accounts. Count it and split it.
$collection = $this->repository->get();
$count = $collection->count();
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page'));
$paginator->setPath(route('api.v1.attachments.index') . $this->buildParams());
2018-12-15 22:03:05 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments');
2018-06-24 06:51:22 +02:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Display the specified resource.
*
* @param Attachment $attachment
2019-09-04 17:39:39 +02:00
*
2018-06-24 06:51:22 +02:00
* @return JsonResponse
*/
2019-09-04 17:39:39 +02:00
public function show(Attachment $attachment): JsonResponse
2018-06-24 06:51:22 +02:00
{
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-15 22:03:05 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments');
2018-06-24 06:51:22 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Store a newly created resource in storage.
*
2019-08-10 07:04:31 +02:00
* @param AttachmentStoreRequest $request
2018-06-24 06:51:22 +02:00
*
2018-07-01 18:31:02 +02:00
* @return JsonResponse
2018-06-24 06:51:22 +02:00
* @throws FireflyException
*/
2019-08-10 07:04:31 +02:00
public function store(AttachmentStoreRequest $request): JsonResponse
2018-06-24 06:51:22 +02:00
{
$data = $request->getAll();
$attachment = $this->repository->store($data);
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-12-15 22:03:05 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments');
2018-06-24 06:51:22 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Update the specified resource in storage.
*
2019-08-10 07:04:31 +02:00
* @param AttachmentUpdateRequest $request
2019-09-04 17:39:39 +02:00
* @param Attachment $attachment
2018-06-24 06:51:22 +02:00
*
* @return JsonResponse
*/
2019-08-10 07:04:31 +02:00
public function update(AttachmentUpdateRequest $request, Attachment $attachment): JsonResponse
2018-06-24 06:51:22 +02:00
{
$data = $request->getAll();
$this->repository->update($attachment, $data);
2019-09-04 17:39:39 +02:00
$manager = $this->getManager();
2018-06-24 06:51:22 +02:00
2018-12-15 22:03:05 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($attachment, $transformer, 'attachments');
2018-06-24 06:51:22 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Upload an attachment.
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
2019-09-04 17:39:39 +02:00
*
* @param Request $request
2018-06-24 06:51:22 +02:00
* @param Attachment $attachment
*
2018-07-01 18:31:02 +02:00
* @return JsonResponse
2018-06-24 06:51:22 +02:00
*/
2018-07-01 18:31:02 +02:00
public function upload(Request $request, Attachment $attachment): JsonResponse
2018-06-24 06:51:22 +02:00
{
/** @var AttachmentHelperInterface $helper */
$helper = app(AttachmentHelperInterface::class);
$body = $request->getContent();
2019-10-20 16:17:43 +02:00
if ('' === $body) {
Log::error('Body of attachment is empty.');
return response()->json([], 422);
}
2018-06-24 06:51:22 +02:00
$helper->saveAttachmentFromApi($attachment, $body);
2018-07-01 18:31:02 +02:00
return response()->json([], 204);
2018-06-24 06:51:22 +02:00
}
}