Files
firefly-iii/app/Api/V1/Controllers/Models/Attachment/ShowController.php

171 lines
6.2 KiB
PHP
Raw Normal View History

2018-06-24 06:51:22 +02:00
<?php
2021-03-06 07:20:49 +01:00
/*
* ShowController.php
* Copyright (c) 2021 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
*/
2021-03-28 11:39:26 +02:00
declare(strict_types=1);
2018-06-24 06:51:22 +02:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Models\Attachment;
2018-06-24 06:51:22 +02:00
2021-03-06 07:20:49 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Middleware\ApiDemoUser;
2018-06-24 06:51:22 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Attachment;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
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;
/**
2021-03-06 07:20:49 +01:00
* Class ShowController
2018-06-24 06:51:22 +02:00
*/
2021-03-06 07:20:49 +01:00
class ShowController extends Controller
2018-06-24 06:51:22 +02:00
{
2021-03-06 07:20:49 +01:00
private AttachmentRepositoryInterface $repository;
2018-06-24 06:51:22 +02:00
/**
2021-03-06 07:20:49 +01:00
* ShowController 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(ApiDemoUser::class)->except(['delete', 'download', 'show', 'index']);
2018-06-24 06:51:22 +02:00
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->repository = app(AttachmentRepositoryInterface::class);
$this->repository->setUser($user);
return $next($request);
}
);
}
/**
2021-09-18 10:07:45 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/attachments/downloadAttachment
2021-09-18 10:07:45 +02:00
*
* Download an attachment.
*
2022-12-29 19:41:57 +01:00
* @param Attachment $attachment
2019-09-04 17:39:39 +02:00
*
* @codeCoverageIgnore
* @return LaravelResponse
2020-10-18 08:01:10 +02:00
* @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
}
$quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
2018-06-24 06:51:22 +02:00
/** @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')
2022-12-29 19:41:57 +01:00
->header('Content-Disposition', 'attachment; filename='.$quoted)
2018-06-24 06:51:22 +02:00
->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')
2022-12-29 19:41:57 +01:00
->header('Content-Length', (string)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
}
/**
2021-09-18 10:07:45 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/attachments/listAttachment
2021-09-18 10:07:45 +02:00
*
2018-06-24 06:51:22 +02:00
* Display a listing of the resource.
*
* @return JsonResponse
2021-05-24 08:22:41 +02:00
* @throws FireflyException
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:
2022-12-29 19:41:57 +01:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2018-06-24 06:51:22 +02:00
2021-03-06 07:20:49 +01:00
// get list of attachments. Count it and split it.
2018-06-24 06:51:22 +02:00
$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'));
2022-12-29 19:41:57 +01:00
$paginator->setPath(route('api.v1.attachments.index').$this->buildParams());
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 FractalCollection($attachments, $transformer, 'attachments');
2018-06-24 06:51:22 +02:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-06-24 06:51:22 +02:00
}
/**
2021-09-18 10:07:45 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/attachments/getAttachment
2021-09-18 10:07:45 +02:00
*
2018-06-24 06:51:22 +02:00
* Display the specified resource.
*
2022-12-29 19:41:57 +01:00
* @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
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2018-06-24 06:51:22 +02:00
}
2021-03-28 11:39:26 +02:00
}