extract pdf use case
This commit is contained in:
parent
b1bee8a16e
commit
bc7937d2b3
7 changed files with 141 additions and 26 deletions
|
|
@ -9,6 +9,8 @@ use App\Element\UseCases\DeleteChildElement\DeleteChildElement;
|
|||
use App\Element\UseCases\DeleteChildElement\DeleteChildElementRequest;
|
||||
use App\Element\UseCases\GetElement\GetElement;
|
||||
use App\Element\UseCases\GetElement\GetElementRequest;
|
||||
use App\Element\UseCases\GetElementPdf\GetElementPdf;
|
||||
use App\Element\UseCases\GetElementPdf\GetElementPdfRequest;
|
||||
use App\Element\UseCases\ReorderChildElements\ReorderChildElements;
|
||||
use App\Element\UseCases\ReorderChildElements\ReorderChildElementsRequest;
|
||||
use App\Element\UseCases\UpdateElement\UpdateElement;
|
||||
|
|
@ -20,18 +22,16 @@ use Illuminate\Http\JsonResponse;
|
|||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ElementController
|
||||
{
|
||||
private const PDF_FOLDERS = ['short', 'long'];
|
||||
|
||||
public function __construct(
|
||||
private GetElement $getElement,
|
||||
private CreateChildElement $createChildElement,
|
||||
private DeleteChildElement $deleteChildElement,
|
||||
private ReorderChildElements $reorderChildElements,
|
||||
private UpdateElement $updateElement,
|
||||
private GetElementPdf $getElementPdf,
|
||||
private FileUploader $fileUploader,
|
||||
) {
|
||||
}
|
||||
|
|
@ -72,31 +72,27 @@ class ElementController
|
|||
string $folder,
|
||||
string $fileName,
|
||||
): Response|JsonResponse {
|
||||
if (!$this->isAllowedPdfFolder($folder)) {
|
||||
try {
|
||||
$result = $this->getElementPdf->execute(
|
||||
new GetElementPdfRequest(
|
||||
folder: $folder,
|
||||
fileName: $fileName,
|
||||
)
|
||||
);
|
||||
} catch (NotFoundException $exception) {
|
||||
return new JsonResponse([
|
||||
'error' => 'PDF not found',
|
||||
], 404);
|
||||
}
|
||||
|
||||
if (!preg_match('/^[A-Za-z0-9._-]+\.pdf$/', $fileName)) {
|
||||
return new JsonResponse([
|
||||
'error' => 'PDF not found',
|
||||
], 404);
|
||||
}
|
||||
|
||||
$path = "element-pdfs/$folder/$fileName";
|
||||
if (!Storage::disk('public')->exists($path)) {
|
||||
return new JsonResponse([
|
||||
'error' => 'PDF not found',
|
||||
'error' => $exception->getMessage(),
|
||||
], 404);
|
||||
}
|
||||
|
||||
return new Response(
|
||||
Storage::disk('public')->get($path),
|
||||
$result->getContents(),
|
||||
200,
|
||||
[
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => "inline; filename=\"$fileName\"",
|
||||
'Content-Type' => $result->getMimeType(),
|
||||
'Content-Disposition' => 'inline; filename="'
|
||||
. $result->getFileName()
|
||||
. '"',
|
||||
],
|
||||
);
|
||||
}
|
||||
|
|
@ -409,9 +405,4 @@ class ElementController
|
|||
|
||||
return $path;
|
||||
}
|
||||
|
||||
private function isAllowedPdfFolder(string $folder): bool
|
||||
{
|
||||
return in_array($folder, self::PDF_FOLDERS, true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
53
backend/app/Element/UseCases/GetElementPdf/GetElementPdf.php
Normal file
53
backend/app/Element/UseCases/GetElementPdf/GetElementPdf.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\GetElementPdf;
|
||||
|
||||
use App\Exceptions\NotFoundException;
|
||||
use App\Shared\Files\StoredFileReader;
|
||||
|
||||
class GetElementPdf
|
||||
{
|
||||
private const PDF_FOLDERS = ['short', 'long'];
|
||||
|
||||
private const PDF_MIME_TYPE = 'application/pdf';
|
||||
|
||||
public function __construct(private StoredFileReader $storedFileReader)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function execute(GetElementPdfRequest $request): GetElementPdfResult
|
||||
{
|
||||
if (!$this->isAllowedPdfFolder($request->folder)) {
|
||||
throw new NotFoundException('PDF not found');
|
||||
}
|
||||
|
||||
if (!$this->isValidPdfFileName($request->fileName)) {
|
||||
throw new NotFoundException('PDF not found');
|
||||
}
|
||||
|
||||
$path = "element-pdfs/$request->folder/$request->fileName";
|
||||
$contents = $this->storedFileReader->read($path);
|
||||
if ($contents === null) {
|
||||
throw new NotFoundException('PDF not found');
|
||||
}
|
||||
|
||||
return new GetElementPdfResult(
|
||||
contents: $contents,
|
||||
fileName: $request->fileName,
|
||||
mimeType: self::PDF_MIME_TYPE,
|
||||
);
|
||||
}
|
||||
|
||||
private function isAllowedPdfFolder(string $folder): bool
|
||||
{
|
||||
return in_array($folder, self::PDF_FOLDERS, true);
|
||||
}
|
||||
|
||||
private function isValidPdfFileName(string $fileName): bool
|
||||
{
|
||||
return preg_match('/^[A-Za-z0-9._-]+\.pdf$/', $fileName) === 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\GetElementPdf;
|
||||
|
||||
class GetElementPdfRequest
|
||||
{
|
||||
public function __construct(
|
||||
public string $folder,
|
||||
public string $fileName,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Element\UseCases\GetElementPdf;
|
||||
|
||||
class GetElementPdfResult
|
||||
{
|
||||
public function __construct(
|
||||
private string $contents,
|
||||
private string $fileName,
|
||||
private string $mimeType,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getContents(): string
|
||||
{
|
||||
return $this->contents;
|
||||
}
|
||||
|
||||
public function getFileName(): string
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,8 @@ use App\Auth\SystemClock;
|
|||
use App\Auth\TokenGenerator;
|
||||
use App\Shared\Files\FileUploader;
|
||||
use App\Shared\Files\LaravelFileUploader;
|
||||
use App\Shared\Files\LaravelStoredFileReader;
|
||||
use App\Shared\Files\StoredFileReader;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
|
|
@ -32,5 +34,9 @@ class AppServiceProvider extends ServiceProvider
|
|||
FileUploader::class,
|
||||
LaravelFileUploader::class,
|
||||
);
|
||||
$this->app->bind(
|
||||
StoredFileReader::class,
|
||||
LaravelStoredFileReader::class,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
17
backend/app/Shared/Files/LaravelStoredFileReader.php
Normal file
17
backend/app/Shared/Files/LaravelStoredFileReader.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Shared\Files;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class LaravelStoredFileReader implements StoredFileReader
|
||||
{
|
||||
public function read(string $path): ?string
|
||||
{
|
||||
if (!Storage::disk('public')->exists($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Storage::disk('public')->get($path);
|
||||
}
|
||||
}
|
||||
8
backend/app/Shared/Files/StoredFileReader.php
Normal file
8
backend/app/Shared/Files/StoredFileReader.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\Shared\Files;
|
||||
|
||||
interface StoredFileReader
|
||||
{
|
||||
public function read(string $path): ?string;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue