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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue