Browse Source

add new dowload_cv route

olinox14 1 year ago
parent
commit
127cdb2cfb

+ 1 - 0
composer.json

@@ -14,6 +14,7 @@
         "doctrine/doctrine-migrations-bundle": "^3.3",
         "doctrine/orm": "^3.2",
         "nelmio/cors-bundle": "^2.5",
+        "olinox14/path-php": "*",
         "phpdocumentor/reflection-docblock": "^5.4",
         "phpstan/phpdoc-parser": "^1.30",
         "symfony/asset": "7.1.*",

+ 48 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "14dfdea1b3dbfd56223d2868ae94f194",
+    "content-hash": "21152a7eb226c68bbb5f0af91881838b",
     "packages": [
         {
             "name": "altcha-org/altcha",
@@ -1588,6 +1588,53 @@
             },
             "time": "2024-06-24T21:25:28+00:00"
         },
+        {
+            "name": "olinox14/path-php",
+            "version": "0.1.8",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/olinox14/path-php.git",
+                "reference": "cbc29f6f8e344d3a0c1b6fd343e26f2919b939fc"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/olinox14/path-php/zipball/cbc29f6f8e344d3a0c1b6fd343e26f2919b939fc",
+                "reference": "cbc29f6f8e344d3a0c1b6fd343e26f2919b939fc",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=8.0"
+            },
+            "require-dev": {
+                "ext-posix": "*",
+                "friendsofphp/php-cs-fixer": "^3.52",
+                "php-coveralls/php-coveralls": "^2.7",
+                "phpstan/phpstan": "^1.10",
+                "phpunit/phpunit": "^9.6"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Path\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Olivier Massot",
+                    "email": "olinox14@tuta.io"
+                }
+            ],
+            "description": "Object-oriented file and path operations, inspired by the path.py python library",
+            "support": {
+                "issues": "https://github.com/olinox14/path-php/issues",
+                "source": "https://github.com/olinox14/path-php/tree/0.1.8"
+            },
+            "time": "2024-04-25T20:56:34+00:00"
+        },
         {
             "name": "phpdocumentor/reflection-common",
             "version": "2.2.0",

+ 1 - 0
config/services.yaml

@@ -11,6 +11,7 @@ services:
         autowire: true      # Automatically injects dependencies in your services.
         autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
         bind:
+            $projectDir: '%kernel.project_dir%'
             $fromEmail: '%env(FROM_EMAIL)%'
             $contactEmail: '%env(CONTACT_EMAIL)%'
             $hmacKey: '%env(HMAC_KEY)%'

+ 31 - 0
src/ApiResource/CvPdfRequest.php

@@ -0,0 +1,31 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResource;
+
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\Post;
+use App\State\Processor\CvPdfRequestProcessor;
+
+#[ApiResource(
+    operations: [
+        new Post(
+            uriTemplate: '/download-cv',
+        ),
+    ],
+    provider: CvPdfRequestProcessor::class
+)]
+class CvPdfRequest
+{
+    protected string $altchaPayload;
+
+    public function getAltchaPayload(): string
+    {
+        return $this->altchaPayload;
+    }
+
+    public function setAltchaPayload(string $altchaPayload): void
+    {
+        $this->altchaPayload = $altchaPayload;
+    }
+}

+ 64 - 0
src/State/Processor/CvPdfRequestProcessor.php

@@ -0,0 +1,64 @@
+<?php
+declare(strict_types=1);
+
+namespace App\State\Processor;
+
+use AltchaOrg\Altcha\Altcha;
+use ApiPlatform\Metadata\Operation;
+use ApiPlatform\Metadata\Post;
+use ApiPlatform\State\ProcessorInterface;
+use App\ApiResource\CvPdfRequest;
+use Symfony\Component\HttpFoundation\HeaderUtils;
+use Symfony\Component\HttpFoundation\Response;
+use Path\Path;
+
+class CvPdfRequestProcessor implements ProcessorInterface
+{
+    public function __construct(
+        private readonly string $projectDir,
+        private readonly string $hmacKey
+    )
+    {}
+
+    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): CvPdfRequest
+    {
+        if (!$operation instanceof Post) {
+            throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
+        }
+
+        /** @var CvPdfRequest $cvPdfRequest */
+        $cvPdfRequest = $data;
+
+//        try {
+//            $valid = Altcha::verifySolution(
+//                $cvPdfRequest->getAltchaPayload(),
+//                $this->hmacKey,
+//                true
+//            );
+//        } catch (\Throwable) {
+//            $valid = false;
+//        }
+//
+//        if (!$valid) {
+//            throw new \RuntimeException('Invalid payload');
+//        }
+
+        $content = (new Path($this->projectDir))
+            ->append('var', 'files', 'CV_Olivier_Massot.pdf')
+            ->getContent();
+
+        // Build the response and attach the file to it
+        // @see https://symfony.com/doc/current/components/http_foundation.html#serving-files
+        $response = new Response($content);
+
+        $response->headers->set('Charset', 'UTF-8');
+        $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');
+        $response->headers->set('Content-Type', 'application/pdf');
+        $response->headers->set(
+            'Content-Disposition',
+            HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, 'CV_Olivier_Massot.pdf')
+        );
+
+        return $cvPdfRequest;
+    }
+}